Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialize test #35

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ func Serialize(in map[string]any) (map[string]any, error) {
}

// cel supports time.Time and time.Duration natively - save original and then replace it after decomposition
nativeTypes := make(map[*jp.Expr]any)
nativeTypes := make(map[string]any, len(in))
jp.Walk(in, func(path jp.Expr, value any) {
switch v := value.(type) {
case time.Duration, time.Time:
nativeTypes[&path] = v
nativeTypes[path.String()] = v
}
})

out := alt.Alter(in, &opts).(map[string]any)

for path, v := range nativeTypes {
if err := path.SetOne(out, v); err != nil {
expr, err := jp.ParseString(path)
if err != nil {
return nil, err
}

if err := expr.SetOne(out, v); err != nil {
return nil, err
}
}

return out, nil
}
169 changes: 134 additions & 35 deletions tests/serialize_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package tests

import (
"reflect"
"fmt"
"testing"
"time"

"github.com/flanksource/gomplate/v3"
"github.com/flanksource/gomplate/v3/data"
_ "github.com/flanksource/gomplate/v3/js"
"github.com/google/go-cmp/cmp"
_ "github.com/robertkrimen/otto/underscore"
)

type myTime struct {
Time time.Time `json:"time"`
Duration time.Duration `json:"duration"`
}

func Test_serialize(t *testing.T) {
t.Parallel()

tests := []struct {
name string
in map[string]any
skip bool
want map[string]any
wantErr bool
}{
Expand All @@ -23,9 +32,11 @@ func Test_serialize(t *testing.T) {
name: "duration",
in: map[string]any{
"r": time.Second * 100,
"a": time.Minute,
},
want: map[string]any{
"r": (time.Second * 100).String(),
"r": (time.Second * 100),
"a": time.Minute,
},
},
{
Expand All @@ -34,7 +45,7 @@ func Test_serialize(t *testing.T) {
"r": testDateTime,
},
want: map[string]any{
"r": testDate,
"r": testDateTime,
},
},

Expand All @@ -56,15 +67,6 @@ func Test_serialize(t *testing.T) {
"r": "hello world",
},
},
{
name: "duration",
in: map[string]any{
"r": 75 * time.Millisecond,
},
want: map[string]any{
"r": "hello world",
},
},
{
name: "dates",
in: map[string]any{
Expand All @@ -81,20 +83,20 @@ func Test_serialize(t *testing.T) {
},
want: map[string]any{
"r": map[string]any{
"newest": map[string]any{
"mode": "drwxr-xr-x",
"modified": testDateTime,
"name": "test",
"size": 10,
},
"files": []map[string]any{
{
"files": []any{
map[string]any{
"name": "test",
"size": 10,
"size": int64(10),
"mode": "drwxr-xr-x",
"modified": testDateTime,
"modified": testDate,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moshloop do we want this to be time.Time? I don't think any nested time.Time or time.Duration are preserved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@adityathebe adityathebe Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
},
"newest": map[string]any{
"mode": "drwxr-xr-x",
"modified": testDate,
"name": "test",
"size": int64(10),
},
},
},
},
Expand Down Expand Up @@ -122,9 +124,9 @@ func Test_serialize(t *testing.T) {
},
},
want: map[string]any{
"r": []map[string]any{
{"city_name": "Kathmandu"},
{"city_name": "Lalitpur"},
"r": []any{
map[string]any{"city_name": "Kathmandu"},
map[string]any{"city_name": "Lalitpur"},
},
},
wantErr: false,
Expand All @@ -143,9 +145,9 @@ func Test_serialize(t *testing.T) {
want: map[string]any{
"r": map[string]any{
"name": "Aditya",
"addresses": []map[string]any{
{"city_name": "Kathmandu"},
{"city_name": "Lalitpur"},
"addresses": []any{
map[string]any{"city_name": "Kathmandu"},
map[string]any{"city_name": "Lalitpur"},
},
},
},
Expand All @@ -164,22 +166,119 @@ func Test_serialize(t *testing.T) {
},
},
},
{
name: "nested time.Duration",
skip: true, // TODO:
in: map[string]any{
"a": &myTime{
Time: time.Now(),
Duration: time.Second,
},
},
want: map[string]any{
"a": map[string]any{
"time": time.Now(),
"duration": time.Second,
},
},
},
{
name: "canary checker ctx.Environment",
in: map[string]any{
"canary": map[string]any{
"id": "3ac56bb3-fbf6-4479-bf3b-5f2758e9fdbe",
"labels": map[string]string{
"Expected-Fail": "true",
"kustomize.toolkit.fluxcd.io/name": "canaries",
"kustomize.toolkit.fluxcd.io/namespace": "default",
},
"name": "http-fail",
"namespace": "canaries",
},
"check": map[string]any{
"description": "",
"endpoint": "",
"id": "018c0096-7342-6f80-5ea5-6df245079b7a",
"labels": map[string]string(nil),
"name": "http fail test expr check",
},
"code": 200,
"content": "",
"duration": 681,
"elapsed": 681153615,
"headers": map[string]string{
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Content-Length": "0",
"Content-Type": "text/html; charset=utf-8",
"Date": "Wed,29 Nov 2023 06:19:30 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
},
"json": map[string]any{},
"results": map[string]any{},
"sslAge": 7004023369313149,
},
want: map[string]any{
"canary": map[string]any{
"id": "3ac56bb3-fbf6-4479-bf3b-5f2758e9fdbe",
"labels": map[string]any{
"Expected-Fail": "true",
"kustomize.toolkit.fluxcd.io/name": "canaries",
"kustomize.toolkit.fluxcd.io/namespace": "default",
},
"name": "http-fail",
"namespace": "canaries",
},
"check": map[string]any{
"description": "",
"endpoint": "",
"id": "018c0096-7342-6f80-5ea5-6df245079b7a",
"labels": map[string]any{},
"name": "http fail test expr check",
},
"code": int64(200),
"content": "",
"duration": int64(681),
"elapsed": int64(681153615),
"headers": map[string]any{
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Content-Length": "0",
"Content-Type": "text/html; charset=utf-8",
"Date": "Wed,29 Nov 2023 06:19:30 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
},
"json": map[string]any{},
"results": map[string]any{},
"sslAge": int64(7004023369313149),
},
},
}

for _, tt := range tests {
for i := range tests {
tt := tests[i]
if tt.skip {
fmt.Printf("Skipping %s\n", tt.name)
continue
}

t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := gomplate.Serialize(tt.in)
if (err != nil) != tt.wantErr {
t.Errorf("serialize() error = %v, wantErr %v", err, tt.wantErr)
return
}

if reflect.DeepEqual(got, tt.want) {
_got, _ := data.ToJSONPretty(" ", got)
_want, _ := data.ToJSONPretty(" ", tt.want)
if _got != _want {
t.Errorf("serialize() = \n%s\nwant\n %v", _got, _want)
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("%v", diff)
return
}

_got, _ := data.ToJSONPretty(" ", got)
_want, _ := data.ToJSONPretty(" ", tt.want)
if _got != _want {
t.Errorf("serialize() = \n%s\nwant\n %v", _got, _want)
}
})
}
Expand Down
Loading