Ompluscator / dynamic-struct

Golang package for editing struct's fields during runtime and mapping structs to other structs.
MIT License
683 stars 81 forks source link

Can we assign a structure variable to a structure field #26

Closed gaofeng-lin closed 1 year ago

gaofeng-lin commented 2 years ago

instance := dynamicstruct.NewStruct(). AddField("Integer", 0,json:"int"). AddField("Text", "",json:"someText"). AddField("Float", 0.0,json:"double"). AddField("Boolean", false, ""). AddField("Slice", []interface{}{}, ""). AddField("Anonymous", "",json:"-"`). Build(). New()

data := []byte(`

{ "int": 123, "someText": "example", "double": 123.45, "Boolean": true, "Slice": { "Test": 124, "Test2": 234 }, "Anonymous": "avoid to read" } ) The error message is as follows: json: cannot unmarshal object into Go struct field .Slice of type []interface {} exit status 1

Ompluscator commented 1 year ago

This issue is related to wrong type usage - Slice is defined as an array but JSON body provides a map instead of array.