Merge pull request #19517 from calavera/validate_config_keys

Verify that the configuration keys in the file are valid.
This commit is contained in:
Phil Estes 2016-01-22 15:01:29 -05:00
commit f87775b873
2 changed files with 30 additions and 0 deletions

View file

@ -1223,11 +1223,27 @@ func (v mergeVal) IsBoolFlag() bool {
return false return false
} }
// Name returns the name of a mergeVal.
// If the original value had a name, return the original name,
// otherwise, return the key asinged to this mergeVal.
func (v mergeVal) Name() string {
type namedValue interface {
Name() string
}
if nVal, ok := v.Value.(namedValue); ok {
return nVal.Name()
}
return v.key
}
// Merge is an helper function that merges n FlagSets into a single dest FlagSet // Merge is an helper function that merges n FlagSets into a single dest FlagSet
// In case of name collision between the flagsets it will apply // In case of name collision between the flagsets it will apply
// the destination FlagSet's errorHandling behavior. // the destination FlagSet's errorHandling behavior.
func Merge(dest *FlagSet, flagsets ...*FlagSet) error { func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
for _, fset := range flagsets { for _, fset := range flagsets {
if fset.formal == nil {
continue
}
for k, f := range fset.formal { for k, f := range fset.formal {
if _, ok := dest.formal[k]; ok { if _, ok := dest.formal[k]; ok {
var err error var err error
@ -1249,6 +1265,9 @@ func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
} }
newF := *f newF := *f
newF.Value = mergeVal{f.Value, k, fset} newF.Value = mergeVal{f.Value, k, fset}
if dest.formal == nil {
dest.formal = make(map[string]*Flag)
}
dest.formal[k] = &newF dest.formal[k] = &newF
} }
} }

View file

@ -514,3 +514,14 @@ func TestSortFlags(t *testing.T) {
t.Fatalf("NFlag (%d) != fs.NFlag() (%d) of elements visited", nflag, fs.NFlag()) t.Fatalf("NFlag (%d) != fs.NFlag() (%d) of elements visited", nflag, fs.NFlag())
} }
} }
func TestMergeFlags(t *testing.T) {
base := NewFlagSet("base", ContinueOnError)
base.String([]string{"f"}, "", "")
fs := NewFlagSet("test", ContinueOnError)
Merge(fs, base)
if len(fs.formal) != 1 {
t.Fatalf("FlagCount (%d) != number (1) of elements merged", len(fs.formal))
}
}