Skip to content
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

Merged
merged 3 commits into from
Apr 11, 2022
Merged

Support for map of struct #29

merged 3 commits into from
Apr 11, 2022

Conversation

zenthangplus
Copy link
Contributor

@zenthangplus zenthangplus commented Oct 12, 2021

I've modified these codes to support for map

  • Map of struct
  • Map of ptr struct

Related #28

@creasty
Copy link
Owner

creasty commented Oct 13, 2021

Thank you for the PR.
I'm swamped this week, so it might take some time to review.

@creasty
Copy link
Owner

creasty commented Nov 1, 2021

Some test cases are failing.
(CI didn't run on the fork PRs due to insufficient configuration so I run locally.)

$ 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

@zenthangplus
Copy link
Contributor Author

@creasty fixed, i've mistake when write new value to map.

Copy link
Owner

@creasty creasty left a 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
}
Copy link
Owner

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()))
			}
		}

@creasty creasty merged commit f8acd9d into creasty:master Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants