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

Use ordered map to guarantee consistency of JSON ordering #50

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 12 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,52 +82,52 @@ func Example() {
// Version 1 output:
// [
// {
// "name": "Alice",
// "username": "alice"
// "username": "alice",
// "name": "Alice"
// },
// {
// "name": "Bob",
// "username": "bob"
// "username": "bob",
// "name": "Bob"
// }
// ]
//
// Version 2 output:
// [
// {
// "username": "alice",
// "name": "Alice",
// "roles": [
// "user",
// "admin"
// ],
// "username": "alice"
// ]
// },
// {
// "username": "bob",
// "name": "Bob",
// "roles": [
// "user"
// ],
// "username": "bob"
// ]
// }
// ]
//
// Version 2 output with personal group too:
// [
// {
// "username": "alice",
// "email": "[email protected]",
// "name": "Alice",
// "roles": [
// "user",
// "admin"
// ],
// "username": "alice"
// ]
// },
// {
// "username": "bob",
// "email": "[email protected]",
// "name": "Bob",
// "roles": [
// "user"
// ],
// "username": "bob"
// ]
// }
// ]
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/hashicorp/go-version v1.6.0
github.com/stretchr/testify v1.9.0
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a h1:DxppxFKRqJ8WD6oJ3+ZXKDY0iMONQDl5UTg2aTyHh8k=
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a/go.mod h1:NREvu3a57BaK0R1+ztrEzHWiZAihohNLQ6trPxlIqZI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
20 changes: 13 additions & 7 deletions sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/hashicorp/go-version"
"gitlab.com/c0b/go-ordered-json"
)

// Options determine which struct fields are being added to the output map.
Expand Down Expand Up @@ -82,7 +83,7 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
return marshalValue(options, v)
}

dest := make(map[string]interface{})
dest := ordered.NewOrderedMap()

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
Expand Down Expand Up @@ -197,13 +198,18 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {

// when a composition field we want to bring the child
// nodes to the top
nestedVal, ok := v.(map[string]interface{})
nestedVal, ok := v.(*ordered.OrderedMap)
if isEmbeddedField && ok {
for key, value := range nestedVal {
dest[key] = value
iter := nestedVal.EntriesIter()
for {
pair, ok := iter()
if !ok {
break
}
dest.Set(pair.Key, pair.Value)
}
} else {
dest[jsonTag] = v
dest.Set(jsonTag, v)
}
}

Expand Down Expand Up @@ -268,13 +274,13 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) {
if mapKeys[0].Kind() != reflect.String {
return nil, MarshalInvalidTypeError{t: mapKeys[0].Kind(), data: val}
}
dest := make(map[string]interface{})
dest := ordered.NewOrderedMap()
for _, key := range mapKeys {
d, err := marshalValue(options, v.MapIndex(key))
if err != nil {
return nil, err
}
dest[key.String()] = d
dest.Set(key.String(), d)
}
return dest, nil
}
Expand Down
Loading
Loading