From db9861425f675c8682a027a88d9c3164761d56b1 Mon Sep 17 00:00:00 2001 From: Mohamed Shapan Date: Sat, 7 Aug 2021 03:39:12 +0200 Subject: [PATCH] fix empty slice of structs issue --- go.mod | 4 ++-- go.sum | 25 ++++++++----------------- unmarshal.go | 2 +- unmarshal_test.go | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index fb8aa7b..2cf5f69 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 0f58d87..88770bf 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/unmarshal.go b/unmarshal.go index 4c593f2..8ff868e 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -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 diff --git a/unmarshal_test.go b/unmarshal_test.go index 2056318..8b4cf20 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -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{} @@ -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)