Skip to content

Commit

Permalink
resource: fix vague error message
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Sep 29, 2016
1 parent 12baa7f commit 55bc298
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions resource/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,29 @@ func (p *Preparer) validateMutuallyExclusive(field reflect.StructField) error {
if mutuallyexclusives, ok := field.Tag.Lookup("mutually_exclusive"); ok {
name := p.getFieldName(field)

for _, mutuallyexclusive := range strings.Split(mutuallyexclusives, ",") {
exclusives := strings.Split(mutuallyexclusives, ",")
for _, mutuallyexclusive := range exclusives {
if mutuallyexclusive == name {
continue
}

if _, ok := p.Source[mutuallyexclusive]; ok {
return fmt.Errorf("%q and %q cannot both be set", name, mutuallyexclusive)
err := "only one of "
if len(exclusives) == 2 {
err += `"` + exclusives[0] + `" or "` + exclusives[1] + `"`
} else {
for i, exclusive := range exclusives {
err += `"` + exclusive + `"`
if i+1 != len(exclusives) {
err += ", "
}
if i+1 == len(exclusives)-1 {
err += "or "
}
}
}
err += " can be set"
return errors.New(err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion resource/preparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestPreparerPrepare(t *testing.T) {
}

_, err := prep.Prepare(fakerenderer.New())
assert.EqualError(t, err, `"a" and "b" cannot both be set`)
assert.EqualError(t, err, `only one of "a" or "b" can be set`)
})
})
}
Expand Down

0 comments on commit 55bc298

Please sign in to comment.