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: keep time objects as time.Time in serialize #86

Merged
merged 2 commits into from
Aug 30, 2024
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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
linters-settings:
govet:
check-shadowing: true
disable:
# printf: non-constant format string in call to fmt.Errorf (govet)
# showing up since golangci-lint version 1.60.1
- printf
# enable:
# - fieldalignment FIXME:
golint:
Expand Down
9 changes: 4 additions & 5 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var opts = oj.Options{
KeyExact: true,
NestEmbed: false,
BytesAs: ojg.BytesAsString,
TimeFormat: time.RFC3339Nano,
TimeFormat: "time",
WriteLimit: 1024,
}

Expand All @@ -31,11 +31,12 @@ func Serialize(in map[string]any) (map[string]any, error) {
return nil, nil
}

// cel supports time.Time and time.Duration natively - save original and then replace it after decomposition
// cel supports time.Duration natively - save original and then replace it after decomposition
// FIXME: This does not work for anything inside Structs
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:
case time.Duration:
nativeTypes[path.String()] = v
}
})
Expand All @@ -47,11 +48,9 @@ func Serialize(in map[string]any) (map[string]any, error) {
if err != nil {
return nil, err
}

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

return out, nil
}
2 changes: 1 addition & 1 deletion structtemplater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestStructTemplater(t *testing.T) {
if err := test.StructTemplater.Walk(i); err != nil {
t.Error(err)
} else if diff := cmp.Diff(i, test.Output); diff != "" {
t.Errorf(diff)
t.Error(diff)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestCelData(t *testing.T) {
Address: &Address{City: "Kathmandu"},
}
runTests(t, []Test{
{map[string]interface{}{"i": newFolderCheck(1)}, "i.files[0].modified", testDate},
{map[string]interface{}{"i": newFolderCheck(1)}, "i.files[0].modified", testDateTime.String()},
{map[string]interface{}{"i": person}, "YAML(toYAML(i)).name", "Aditya"},
// csv
{nil, `CSV(["Alice,30", "Bob,31"])[0][0]`, "Alice"},
Expand Down
4 changes: 2 additions & 2 deletions tests/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func Test_serialize(t *testing.T) {
"name": "test",
"size": int64(10),
"mode": "drwxr-xr-x",
"modified": testDate,
"modified": testDateTime,
},
},
"newest": map[string]any{
"mode": "drwxr-xr-x",
"modified": testDate,
"modified": testDateTime,
"name": "test",
"size": int64(10),
},
Expand Down
Loading