From 67a2147ae513f7712eb9d9d4decd9609ebf25754 Mon Sep 17 00:00:00 2001 From: Parth Tyagi Date: Wed, 20 Jan 2021 22:28:44 +0530 Subject: [PATCH] Add LabelsToTagsMap and EnsureStringArray functions to sdk plugin. closes #3 --- plugin/transform/primitives.go | 31 +++++++++++++++++++++++++ plugin/transform/primitives_test.go | 35 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/plugin/transform/primitives.go b/plugin/transform/primitives.go index 83708ef7..b69c9b0d 100644 --- a/plugin/transform/primitives.go +++ b/plugin/transform/primitives.go @@ -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 +} diff --git a/plugin/transform/primitives_test.go b/plugin/transform/primitives_test.go index 60a50386..c8b52a31 100644 --- a/plugin/transform/primitives_test.go +++ b/plugin/transform/primitives_test.go @@ -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) {