Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Support custom name matchers#250

Merged
mitchellh merged 1 commit into
mitchellh:masterfrom
abemedia:feat/custom-name-matchers
Sep 14, 2021
Merged

Support custom name matchers#250
mitchellh merged 1 commit into
mitchellh:masterfrom
abemedia:feat/custom-name-matchers

Conversation

@abemedia

@abemedia abemedia commented Jun 13, 2021

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Owner

I love this. Thank you.

@abemedia

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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