Skip to content

Commit

Permalink
Update FromField transform to accept multiple arguments, which are tr…
Browse files Browse the repository at this point in the history
…ied in order. Closes #55
  • Loading branch information
tyagiparth authored May 5, 2021
1 parent ca52a1a commit eb334d3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/acceptance-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.16

- name: Checkout Steampipe
uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/turbot/steampipe-plugin-sdk

go 1.15
go 1.16

require (
github.com/dgraph-io/ristretto v0.0.3
github.com/gertd/go-pluralize v0.1.7 // indirect
github.com/ghodss/yaml v1.0.0
github.com/golang/protobuf v1.4.3
github.com/hashicorp/go-hclog v0.14.1
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/gertd/go-pluralize v0.1.7 h1:RgvJTJ5W7olOoAks97BOwOlekBFsLEyh00W48Z6ZEZY=
github.com/gertd/go-pluralize v0.1.7/go.mod h1:O4eNeeIf91MHh1GJ2I47DNtaesm66NYvjYgAahcqSDQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
Expand Down Expand Up @@ -87,7 +89,6 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 h1:7GoSOOW2jpsfkntVKaS2rAr1TJqfcxotyaUcuxoZSzg=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
Expand All @@ -99,6 +100,7 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -113,7 +115,6 @@ github.com/stevenle/topsort v0.0.0-20130922064739-8130c1d7596b/go.mod h1:YIyOMT1
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
26 changes: 19 additions & 7 deletions plugin/transform/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/iancoleman/strcase"
"github.com/turbot/go-kit/types"

pluralize "github.com/gertd/go-pluralize"
"github.com/turbot/go-kit/helpers"
)

Expand All @@ -27,18 +28,29 @@ import (
// the field name is in the 'Param'
func FieldValue(_ context.Context, d *TransformData) (interface{}, error) {
var item = d.HydrateItem
var fieldNames []string

propertyPath, ok := d.Param.(string)
if !ok {
return nil, fmt.Errorf("'FieldValue' requires a string parameter containing property path but received %v", d.Param)
switch p := d.Param.(type) {
case []string:
fieldNames = p
case string:
fieldNames = []string{p}
default:
return nil, fmt.Errorf("'FieldValue' requires one or more string parameters containing property path but received %v", d.Param)
}

fieldValue, ok := helpers.GetNestedFieldValueFromInterface(item, propertyPath)
if !ok {
log.Printf("[TRACE] failed to retrieve property path %s\n", propertyPath)
for _, propertyPath := range fieldNames {
fieldValue, ok := helpers.GetNestedFieldValueFromInterface(item, propertyPath)
if ok {
return fieldValue, nil

}

}
pluralize := pluralize.NewClient()
log.Printf("[TRACE] failed to retrieve value for property %s %s\n", pluralize.Pluralize("path", len(fieldNames), false), fmt.Sprintf(strings.Join(fieldNames[:], " or ")))

return fieldValue, nil
return nil, nil
}

// FieldValueCamelCase :: intended for the start of a transform chain
Expand Down
25 changes: 24 additions & 1 deletion plugin/transform/primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ Statement:
"support:*",
},
},

"FieldValueTag string": {
d: &TransformData{
Value: "TRUE",
Expand Down Expand Up @@ -578,6 +577,30 @@ Statement:
function: FieldValueTag,
expected: "ERROR",
},
"FieldValue string single value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: "GitURL",
},
function: FieldValue,
expected: taggedStructInstance.GitURL,
},
"FieldValue string double value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: []string{"GetColumn", "NodeID"},
},
function: FieldValue,
expected: taggedStructInstance.NodeID,
},
"FieldValue Invalid Value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: []string{"GetColumn", "ListColumn"},
},
function: FieldValue,
expected: "ERROR",
},
"UnixToTimestamp time conversion int64": {
d: &TransformData{
Value: 1611061921,
Expand Down
8 changes: 6 additions & 2 deletions plugin/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ func FromMethod(methodName string) *ColumnTransforms {
}

// Generate a value by retrieving a field from the source item
func FromField(field string) *ColumnTransforms {
return &ColumnTransforms{Transforms: []*TransformCall{{Transform: FieldValue, Param: field}}}
func FromField(fieldNames ...string) *ColumnTransforms {
var fieldNameArray []string
for _, fieldName := range fieldNames {
fieldNameArray = append(fieldNameArray, fieldName)
}
return &ColumnTransforms{Transforms: []*TransformCall{{Transform: FieldValue, Param: fieldNameArray}}}
}

// Generate a value by returning the raw hydrate item
Expand Down

0 comments on commit eb334d3

Please sign in to comment.