Skip to content

Commit

Permalink
Populate tag name customization
Browse files Browse the repository at this point in the history
  • Loading branch information
kamynina committed Feb 10, 2022
1 parent 4d2be03 commit 7c0ed88
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ func (b bind) bind(fs FlagSet, v interface{}) (err error) {
// short name.
if !tag.HasExplicitName ||
(usePFlag && tag.Name == tag.ShortName) {
tag.Name = FromCamelCase(structField.Name, Separator)
if b.NamePopulator == nil {
tag.Name = FromCamelCase(structField.Name, Separator)
} else {
tag.Name = b.NamePopulator(structField.Name)
}
}

fieldV := val.Field(i)
Expand Down
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func newBind(opts ...Option) bind {
type bind struct {
Prefix string
NoAutoFlatten bool
NamePopulator func(fieldName string) string
}

func (b bind) Option() Option {
Expand Down Expand Up @@ -41,3 +42,9 @@ func NoAutoFlatten() Option {
b.NoAutoFlatten = true
}
}

func NamePopulator(fn func(fieldName string) string) Option {
return func(b *bind) {
b.NamePopulator = fn
}
}

0 comments on commit 7c0ed88

Please sign in to comment.