Closed leslie-qiwa closed 1 year ago
Thanks for making this great tool. It is very useful for GraphQL query which requires dynamic struct sometimes.
One feature seems missing is not supported embedded dynamic struct. For example,
func main() { childStruct := dynamicstruct.NewStruct(). AddField("Child", 0, `json:"int"`). Build() childInstance := childStruct.New() content, err := json.Marshal(childInstance) if err != nil { log.Fatal(err) } fmt.Println(string(content)) parentStruct := dynamicstruct.NewStruct(). AddField("Parent", &childInstance, "").Build() parentInstance := parentStruct.New() content, err = json.Marshal(parentInstance) if err != nil { log.Fatal(err) } fmt.Println(string(content)) }
Result will be:
{"int":0} {"Parent":null}
Seems like this is due to embedded dynamic struct instance is not initialized, so if explicitly init like below, it will marshal correctly
func main() { childStruct := dynamicstruct.NewStruct(). AddField("Child", 0, `json:"int"`). Build() childInstance := childStruct.New() content, err := json.Marshal(childInstance) if err != nil { log.Fatal(err) } fmt.Println(string(content)) parentStruct := dynamicstruct.NewStruct(). AddField("Parent", &childInstance, "").Build() parentInstance := parentStruct.New() data := []byte(` { "Parent": { "Child": 0 } } `) err = json.Unmarshal(data, &parentInstance) if err != nil { log.Fatal(err) } content, err = json.Marshal(parentInstance) if err != nil { log.Fatal(err) } fmt.Println(string(content)) }
Marshal result is
{"int":0} {"Parent":{"Child":0}}
See #21
This is still not working
Thanks for making this great tool. It is very useful for GraphQL query which requires dynamic struct sometimes.
One feature seems missing is not supported embedded dynamic struct. For example,
Result will be:
Seems like this is due to embedded dynamic struct instance is not initialized, so if explicitly init like below, it will marshal correctly
Marshal result is