Skip to content

Commit

Permalink
Add LabelsToTagsMap and EnsureStringArray functions to sdk plugin. cl…
Browse files Browse the repository at this point in the history
…oses #3
  • Loading branch information
Parth Tyagi committed Jan 20, 2021
1 parent d26cb8a commit 67a2147
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
31 changes: 31 additions & 0 deletions plugin/transform/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,34 @@ func UnixMsToTimestamp(_ context.Context, d *TransformData) (interface{}, error)
}
return nil, nil
}

// EnsureStringArray :: converts string into an array format
func EnsureStringArray(_ context.Context, d *TransformData) (interface{}, error) {
if d.Value != nil {
switch v := d.Value.(type) {
case []string:
return v, nil
case string:
return []string{v}, nil
default:
str := fmt.Sprintf("%v", d.Value)
return []string{str}, nil
}

}
return nil, nil
}

// LabelsToTagsMap :: converts raw labels/tags into Tags map supported by steampipe
func LabelsToTagsMap(_ context.Context, d *TransformData) (interface{}, error) {
labels := d.Value.([]string)
result := map[string]bool{}

if labels == nil {
return result, nil
}
for _, i := range labels {
result[i] = true
}
return result, nil
}
35 changes: 35 additions & 0 deletions plugin/transform/primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,41 @@ Statement:
function: UnixMsToTimestamp,
expected: nil,
},
"EnsureStringArray nil": {
d: &TransformData{
Value: nil,
},
function: EnsureStringArray,
expected: nil,
},
"EnsureStringArray string": {
d: &TransformData{
Value: "arn:aws:acm:us-east-2:123456789012:certificate/ec12345a-6121-47c3-9cd2-29fc7298889d",
},
function: EnsureStringArray,
expected: []string{"arn:aws:acm:us-east-2:123456789012:certificate/ec12345a-6121-47c3-9cd2-29fc7298889d"},
},
"EnsureStringArray array": {
d: &TransformData{
Value: []string{"arn:aws:acm:us-east-2:123456789012:certificate/ec12345a-6121-47c3-9cd2-29fc7298889d"},
},
function: EnsureStringArray,
expected: []string{"arn:aws:acm:us-east-2:123456789012:certificate/ec12345a-6121-47c3-9cd2-29fc7298889d"},
},
"LabelsToTagsMap array": {
d: &TransformData{
Value: []string{"foo", "bar"},
},
function: LabelsToTagsMap,
expected: map[string]bool{"foo": true, "bar": true},
},
"LabelsToTagsMap nil": {
d: &TransformData{
Value: []string{},
},
function: LabelsToTagsMap,
expected: map[string]bool{},
},
}

func TestTransformFunctions(t *testing.T) {
Expand Down

0 comments on commit 67a2147

Please sign in to comment.