Navigation Menu

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 custom name matchers #250

Merged
merged 1 commit into from Sep 14, 2021
Merged

Support custom name matchers #250

merged 1 commit into from Sep 14, 2021

Conversation

abemedia
Copy link
Contributor

@abemedia abemedia commented Jun 13, 2021

Fixes #89 and #208 in a more dynamic way than #81, #176 or #207. Rather than just supporting a single use-case this also allows any other form of custom name matcher e.g. using https://github.com/iancoleman/strcase to automatically match to snake_case or kebab-case.

For example:

type Target struct {
  FooBar string
}

var out Target

config := &DecoderConfig{
  Result: &out,
  MatchName: func(mapKey, fieldName string) bool {
    return strcase.ToCamel(mapKey) == fieldName
  },
}

decoder, _ := NewDecoder(config)

decoder.Decode(map[string]interface{}{
  "foo_bar": "baz",
})

Would result in out being:

Target{
  FooBar: "baz",
}

@mitchellh
Copy link
Owner

I love this. Thank you.

@abemedia
Copy link
Contributor Author

You're very welcome. And thank you for creating such a great library!

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.

Add support for field name remapping hook
2 participants