Skip to content

Commit

Permalink
Add transform to get a field value from two different paths. closes #55
Browse files Browse the repository at this point in the history
  • Loading branch information
Parth Tyagi committed Apr 28, 2021
1 parent ac4d3e2 commit fcbc91e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plugin/transform/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
// return a field value of either the hydrate call result (if present) or the root item if not
// the field name is in the 'Param'
func FieldValue(_ context.Context, d *TransformData) (interface{}, error) {

var item = d.HydrateItem

propertyPath, ok := d.Param.(string)
Expand All @@ -41,6 +42,33 @@ func FieldValue(_ context.Context, d *TransformData) (interface{}, error) {
return fieldValue, nil
}

func FieldsValue(_ context.Context, d *TransformData) (interface{}, error) {
var item = d.HydrateItem

propertyPath, ok := d.Param.([]string)

if !ok {
return nil, fmt.Errorf("'FieldValue' requires a string parameter containing property path but received %v", d.Param)
}

for _, value := range propertyPath {
hydrateItem := reflect.ValueOf(item)
if hydrateItem.Type().Kind() != reflect.Ptr {
hydrateItem = reflect.New(reflect.TypeOf(item))
}
property := hydrateItem.Elem().FieldByName(value)
if property.IsValid() {
fieldValue, ok := helpers.GetNestedFieldValueFromInterface(item, value)
if !ok {
log.Printf("[TRACE] failed to retrieve property path %s\n", value)
}
return fieldValue, nil
}

}
return nil, nil
}

// FieldValueCamelCase :: intended for the start of a transform chain
// convert the column name to camel case and call FieldValue
func FieldValueCamelCase(ctx context.Context, d *TransformData) (interface{}, error) {
Expand Down
24 changes: 24 additions & 0 deletions plugin/transform/primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,30 @@ Statement:
function: FieldValueTag,
expected: "ERROR",
},
"FieldsValue string first value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: []string{"GitURL", "GetColumn"},
},
function: FieldsValue,
expected: taggedStructInstance.GitURL,
},
"FieldsValue string second value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: []string{"GetColumn", "NodeID"},
},
function: FieldsValue,
expected: taggedStructInstance.NodeID,
},
"FieldsValue Invalid Value": {
d: &TransformData{
HydrateItem: taggedStructInstance,
Param: []string{"GetColumn", "ListColumn"},
},
function: FieldsValue,
expected: nil,
},
"UnixToTimestamp time conversion int64": {
d: &TransformData{
Value: 1611061921,
Expand Down
4 changes: 4 additions & 0 deletions plugin/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func FromField(field string) *ColumnTransforms {
return &ColumnTransforms{Transforms: []*TransformCall{{Transform: FieldValue, Param: field}}}
}

func FromFields(fields []string) *ColumnTransforms {
return &ColumnTransforms{Transforms: []*TransformCall{{Transform: FieldsValue, Param: fields}}}
}

// Generate a value by returning the raw hydrate item
func FromValue() *ColumnTransforms {
return &ColumnTransforms{Transforms: []*TransformCall{{Transform: RawValue}}}
Expand Down

0 comments on commit fcbc91e

Please sign in to comment.