Rename IgnoreFieldsRegex -> IgnoreFields.

This commit is contained in:
Alec Thomas
2021-09-27 16:31:54 +10:00
parent 1d48b6f720
commit 5c7b038540
4 changed files with 19 additions and 19 deletions
+1 -1
View File
@@ -121,7 +121,7 @@ func buildNode(k *Kong, v reflect.Value, typ NodeType, seenFlags map[string]bool
MAIN: MAIN:
for _, field := range fields { for _, field := range fields {
for _, r := range k.ignoreFieldsRegex { for _, r := range k.ignoreFields {
if r.MatchString(v.Type().Name() + "." + field.field.Name) { if r.MatchString(v.Type().Name() + "." + field.field.Name) {
continue MAIN continue MAIN
} }
+2 -2
View File
@@ -53,7 +53,7 @@ type Kong struct {
loader ConfigurationLoader loader ConfigurationLoader
resolvers []Resolver resolvers []Resolver
registry *Registry registry *Registry
ignoreFieldsRegex []*regexp.Regexp ignoreFields []*regexp.Regexp
noDefaultHelp bool noDefaultHelp bool
usageOnError usageOnError usageOnError usageOnError
@@ -82,7 +82,7 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
vars: Vars{}, vars: Vars{},
bindings: bindings{}, bindings: bindings{},
helpFormatter: DefaultHelpValueFormatter, helpFormatter: DefaultHelpValueFormatter,
ignoreFieldsRegex: make([]*regexp.Regexp, 0), ignoreFields: make([]*regexp.Regexp, 0),
} }
options = append(options, Bind(k)) options = append(options, Bind(k))
+2 -2
View File
@@ -1321,7 +1321,7 @@ type testIgnoreFields struct {
func TestIgnoreRegex(t *testing.T) { func TestIgnoreRegex(t *testing.T) {
cli := testIgnoreFields{} cli := testIgnoreFields{}
k, err := kong.New(&cli, kong.IgnoreFieldsRegex(`.*\.XXX_.+`)) k, err := kong.New(&cli, kong.IgnoreFields(`.*\.XXX_.+`))
require.NoError(t, err) require.NoError(t, err)
_, err = k.Parse([]string{"foo", "sub"}) _, err = k.Parse([]string{"foo", "sub"})
@@ -1343,7 +1343,7 @@ func TestIgnoreRegex(t *testing.T) {
func TestIgnoreRegexEmpty(t *testing.T) { func TestIgnoreRegexEmpty(t *testing.T) {
cli := testIgnoreFields{} cli := testIgnoreFields{}
_, err := kong.New(&cli, kong.IgnoreFieldsRegex("")) _, err := kong.New(&cli, kong.IgnoreFields(""))
require.Error(t, err) require.Error(t, err)
require.Contains(t, "regex input cannot be empty", err.Error()) require.Contains(t, "regex input cannot be empty", err.Error())
} }
+3 -3
View File
@@ -304,13 +304,13 @@ func Resolvers(resolvers ...Resolver) Option {
}) })
} }
// IgnoreFieldsRegex will cause kong.New() to skip field names that match any // IgnoreFields will cause kong.New() to skip field names that match any
// of the provided regex patterns. This is useful if you are not able to add a // of the provided regex patterns. This is useful if you are not able to add a
// kong="-" struct tag to a struct/element before the call to New. // kong="-" struct tag to a struct/element before the call to New.
// //
// Example: When referencing protoc generated structs, you will likely want to // Example: When referencing protoc generated structs, you will likely want to
// ignore/skip XXX_* fields. // ignore/skip XXX_* fields.
func IgnoreFieldsRegex(regexes ...string) Option { func IgnoreFields(regexes ...string) Option {
return OptionFunc(func(k *Kong) error { return OptionFunc(func(k *Kong) error {
for _, r := range regexes { for _, r := range regexes {
if r == "" { if r == "" {
@@ -322,7 +322,7 @@ func IgnoreFieldsRegex(regexes ...string) Option {
return errors.Wrap(err, "unable to compile regex") return errors.Wrap(err, "unable to compile regex")
} }
k.ignoreFieldsRegex = append(k.ignoreFieldsRegex, re) k.ignoreFields = append(k.ignoreFields, re)
} }
return nil return nil