Skip to content

Commit

Permalink
fix empty slice of structs issue
Browse files Browse the repository at this point in the history
  • Loading branch information
m7shapan committed Aug 7, 2021
1 parent 184dde1 commit db98614
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.14

require (
github.com/google/go-cmp v0.5.4
github.com/stretchr/testify v1.6.1 // indirect
github.com/tidwall/gjson v1.6.0
github.com/tidwall/gjson v1.8.1
github.com/tidwall/pretty v1.2.0 // indirect
)
25 changes: 8 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/gjson v1.8.1 h1:8j5EE9Hrh3l9Od1OIEDAb7IpezNA20UdRngNAj5N0WU=
github.com/tidwall/gjson v1.8.1/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func unmarshalSlice(results []gjson.Result, field reflect.Type) interface{} {
for i := 0; i < len(results); i++ {

var value interface{}
if isStructureType(field.Elem().String()) {
if isStructureType(field.Elem().Kind().String()) {
value = parseStructureType(results[i], field.Elem())
} else {
// set field value depend on it's data type
Expand Down
37 changes: 29 additions & 8 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ func TestUnmarshalSmall(t *testing.T) {
"name": {"first": "Mohamed", "last": "Shapan"},
"age": 26,
"friends": [
{"name": "Asma", "age": 26},
{"name": "Ahmed", "age": 25},
{"name": "Mahmoud", "age": 30}
{"first": "Asma", "age": 26},
{"first": "Ahmed", "age": 25},
{"first": "Mahmoud", "age": 30}
]
}`

type Name struct {
First string `njson:"first"`
Last string `njson:"last"`
}

type User struct {
Name string `njson:"name.last"`
Age int `njson:"age"`
Friends []string `njson:"friends.#.name"`
Name Name `njson:"name"`
Age int `njson:"age"`
Friends []Name `njson:"friends"`
}

actual := User{}
Expand All @@ -32,10 +37,26 @@ func TestUnmarshalSmall(t *testing.T) {
t.Error(err)
}

var friends []Name
friends = append(friends, Name{
First: "Asma",
})

friends = append(friends, Name{
First: "Ahmed",
})

friends = append(friends, Name{
First: "Mahmoud",
})

expected := User{
Name: "Shapan",
Name: Name{
First: "Mohamed",
Last: "Shapan",
},
Age: 26,
Friends: []string{"Asma", "Ahmed", "Mahmoud"},
Friends: friends,
}

diff := cmp.Diff(expected, actual)
Expand Down

0 comments on commit db98614

Please sign in to comment.