-
Notifications
You must be signed in to change notification settings - Fork 69
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
Support for map of struct #29
Conversation
Thank you for the PR. |
Some test cases are failing. $ go test ./...
--- FAIL: TestInit (0.00s)
--- FAIL: TestInit/map_of_ptr_struct (0.00s)
defaults_test.go:602: it should set default for Bar field in Struct1 item
defaults_test.go:605: it should set default for WithDefault field in Struct1 item
defaults_test.go:608: it should not override Foo field in Struct2 item
defaults_test.go:614: it should set default for WithDefault field in Struct2 item
FAIL
FAIL github.com/creasty/defaults 0.095s
? github.com/creasty/defaults/internal/fixture [no test files]
FAIL |
@creasty fixed, i've mistake when write new value to map. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delayed response.
field.SetMapIndex(e, newVal) | ||
} else { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delving in on my side, this feels more elegant and can support even more complex types like map[string]map[int]Struct
, map[string]*map[int]Struct
,map[string]AliasOfSliceOfStruct
, etc.
for _, e := range field.MapKeys() {
var v = field.MapIndex(e)
switch v.Kind() {
case reflect.Ptr:
switch v.Elem().Kind() {
case reflect.Struct, reflect.Slice, reflect.Map:
if err := setField(v.Elem(), ""); err != nil {
return err
}
}
case reflect.Struct, reflect.Slice, reflect.Map:
ref := reflect.New(v.Type())
ref.Elem().Set(v)
if err := setField(ref.Elem(), ""); err != nil {
return err
}
field.SetMapIndex(e, ref.Elem().Convert(v.Type()))
}
}
I've modified these codes to support for map
Related #28