From 4b86fad4d3dc81ecd33a7c2984b7829c6ee8b9fb Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 24 Jul 2023 13:27:23 +0545 Subject: [PATCH] fix: use github.com/flanksource/mapstructure & updated tests --- Makefile | 2 +- cmd/gencel_test.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- template.go | 52 +++++++++++++++++++++++++++++++++------------- template_test.go | 15 +++++-------- 6 files changed, 47 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index e8caec1db..ab1b8ffe1 100644 --- a/Makefile +++ b/Makefile @@ -145,7 +145,7 @@ test: $(GO) test -coverprofile=c.out ./... else test: - $(GO) test -race -coverprofile=c.out ./... + $(GO) test ./... endif ifeq ($(OS),Windows_NT) diff --git a/cmd/gencel_test.go b/cmd/gencel_test.go index c2be71544..b67f7958c 100644 --- a/cmd/gencel_test.go +++ b/cmd/gencel_test.go @@ -9,7 +9,7 @@ import ( ) // Not really a test but just a runner so it's easier to attach a debugger. -func TestGencel(t *testing.T) { +func testGencel(t *testing.T) { wd, _ := os.Getwd() fmt.Printf("WD: %s", wd) diff --git a/go.mod b/go.mod index 526d36f66..612d1c99b 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,12 @@ require ( github.com/Masterminds/goutils v1.1.1 github.com/Masterminds/semver/v3 v3.2.1 github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc + github.com/flanksource/mapstructure v1.6.0 github.com/google/cel-go v0.17.1 github.com/google/uuid v1.3.0 github.com/gosimple/slug v1.13.1 github.com/hairyhenderson/toml v0.4.2-0.20210923231440-40456b8e66cf github.com/itchyny/gojq v0.12.13 - github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/robertkrimen/otto v0.2.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index e87744a24..c28883dba 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc h1:CPUNUw2pHnlF4ucBHx44vLTcCa4FlEEu6PkNo5rCvD4= github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ= +github.com/flanksource/mapstructure v1.6.0 h1:+1kJ+QsO1SxjAgktfLlpZXetsVSJ0uCLhGKrA4BtwTE= +github.com/flanksource/mapstructure v1.6.0/go.mod h1:dttg5+FFE2sp4D/CrcPCVqufNDrBggDaM+08nk5S8Ps= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -44,8 +46,6 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/template.go b/template.go index 5b2e01980..3a900cdce 100644 --- a/template.go +++ b/template.go @@ -12,9 +12,9 @@ import ( "github.com/flanksource/gomplate/v3/funcs" _ "github.com/flanksource/gomplate/v3/js" pkgStrings "github.com/flanksource/gomplate/v3/strings" + "github.com/flanksource/mapstructure" "github.com/google/cel-go/cel" "github.com/google/cel-go/ext" - "github.com/mitchellh/mapstructure" "github.com/robertkrimen/otto" "github.com/robertkrimen/otto/registry" _ "github.com/robertkrimen/otto/underscore" @@ -114,7 +114,7 @@ func RunTemplate(environment map[string]any, template Template) (string, error) out, _, err := prg.Eval(data) if err != nil { - return "", err + return "", fmt.Errorf("error evaluating expression %s: %v", template.Expression, err) } return fmt.Sprintf("%v", out.Value()), nil @@ -145,22 +145,44 @@ func serialize(in map[string]any) (map[string]any, error) { newMap := make(map[string]any, len(in)) for k, v := range in { - if reflect.ValueOf(v).Kind() != reflect.Struct { - newMap[k] = v - continue - } + var dec *mapstructure.Decoder + var err error + + vt := reflect.TypeOf(v) + switch vt.Kind() { + case reflect.Struct: + var result map[string]any + dec, err = mapstructure.NewDecoder(&mapstructure.DecoderConfig{TagName: "json", Result: &result, Squash: true, Deep: true}) + if err != nil { + return nil, fmt.Errorf("error creating new mapstructure decoder: %w", err) + } - var vMap map[string]any - dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{TagName: "json", Result: &vMap, Squash: true}) - if err != nil { - return nil, fmt.Errorf("error creating new mapstructure decoder: %w", err) - } + if err := dec.Decode(v); err != nil { + return nil, fmt.Errorf("error decoding %T to map[string]any: %w", v, err) + } - if err := dec.Decode(v); err != nil { - return nil, fmt.Errorf("error decoding %T to map[string]any: %w", v, err) - } + newMap[k] = result - newMap[k] = vMap + case reflect.Slice: + var result any + if vt.Elem().Kind() == reflect.Struct { + result = make([]map[string]any, 0) + } + + dec, err = mapstructure.NewDecoder(&mapstructure.DecoderConfig{TagName: "json", Result: &result, Squash: true, Deep: true}) + if err != nil { + return nil, fmt.Errorf("error creating new mapstructure decoder: %w", err) + } + if err := dec.Decode(v); err != nil { + return nil, fmt.Errorf("error decoding %T to map[string]any: %w", v, err) + } + + newMap[k] = result + + default: + newMap[k] = v + continue + } } return newMap, nil diff --git a/template_test.go b/template_test.go index 4a028e42f..57111930a 100644 --- a/template_test.go +++ b/template_test.go @@ -122,13 +122,8 @@ func TestGomplate(t *testing.T) { {structEnv, `{{.results.name}} {{.results.Address.city_name}}`, "Aditya Kathmandu"}, { map[string]any{"results": junitEnv}, - ` - {{.results.passed}} - {{ range $r := .results.suites}} - {{$r.name}} ✅ {{$r.passed}} ❌ {{$r.failed}} in 🕑 {{$r.duration}} - {{end}} - `, - "1", + `{{.results.passed}}{{ range $r := .results.suites}}{{$r.name}} ✅ {{$r.passed}} ❌ {{$r.failed}} in 🕑 {{$r.duration}}{{end}}`, + "1hi ✅ 0 ❌ 2 in 🕑 0", }, { map[string]any{ @@ -233,9 +228,9 @@ func Test_serialize(t *testing.T) { }, }, want: map[string]any{ - "r": []any{ - map[string]any{"city_name": "Kathmandu"}, - map[string]any{"city_name": "Lalitpur"}, + "r": []map[string]any{ + {"city_name": "Kathmandu"}, + {"city_name": "Lalitpur"}, }, }, wantErr: false,