Rename api ErrorList for clarity

This commit is contained in:
Tim Hockin
2014-10-24 09:43:14 -07:00
parent 2bbd11eda6
commit d6effe3c6d
7 changed files with 74 additions and 70 deletions

View File

@@ -113,20 +113,22 @@ func NewFieldNotFound(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeNotFound, field, value}
}
// ErrorList is a collection of errors. This does not implement the error
// interface to avoid confusion where an empty ErrorList would still be an
// error (non-nil). To produce a single error instance from an ErrorList, use
// the ToError() method, which will return nil for an empty ErrorList.
type ErrorList util.ErrorList
// ValidationErrorList is a collection of ValidationErrors. This does not
// implement the error interface to avoid confusion where an empty
// ValidationErrorList would still be an error (non-nil). To produce a single
// error instance from a ValidationErrorList, use the ToError() method, which
// will return nil for an empty ValidationErrorList.
type ValidationErrorList util.ErrorList
// ToError converts an ErrorList into a "normal" error, or nil if the list is empty.
func (list ErrorList) ToError() error {
// ToError converts a ValidationErrorList into a "normal" error, or nil if the
// list is empty.
func (list ValidationErrorList) ToError() error {
return util.ErrorList(list).ToError()
}
// Prefix adds a prefix to the Field of every ValidationError in the list. Returns
// the list for convenience.
func (list ErrorList) Prefix(prefix string) ErrorList {
// Prefix adds a prefix to the Field of every ValidationError in the list.
// Returns the list for convenience.
func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
for i := range list {
if err, ok := list[i].(ValidationError); ok {
if strings.HasPrefix(err.Field, "[") {
@@ -137,13 +139,15 @@ func (list ErrorList) Prefix(prefix string) ErrorList {
err.Field = prefix
}
list[i] = err
} else {
glog.Warningf("ValidationErrorList holds non-ValidationError: %T", list)
}
}
return list
}
// PrefixIndex adds an index to the Field of every ValidationError in the list. Returns
// the list for convenience.
func (list ErrorList) PrefixIndex(index int) ErrorList {
// PrefixIndex adds an index to the Field of every ValidationError in the list.
// Returns the list for convenience.
func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
return list.Prefix(fmt.Sprintf("[%d]", index))
}