forked from rancher/wrangler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new Field struct which provides the ability to search through unstructed data (such as the data returned from a json call), and easily add or remove values to this data.
- Loading branch information
1 parent
0a0c449
commit aacc274
Showing
5 changed files
with
1,252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package data | ||
|
||
type errorCode int | ||
|
||
const ( | ||
errorInvalidData errorCode = iota + 1 | ||
errorInvalidField | ||
errorFieldValueNotFound | ||
) | ||
|
||
type DataError struct { | ||
message string | ||
code errorCode | ||
} | ||
|
||
// Error returns the underlying error message, satisfying the error interface | ||
func (d *DataError) Error() string { | ||
return d.message | ||
} | ||
|
||
func newDataError(message string, code errorCode) *DataError { | ||
return &DataError{ | ||
message: message, | ||
code: code, | ||
} | ||
} | ||
|
||
// IsInvalidDataError checks if a given error indicates that the provided data field was invalid. | ||
func IsInvalidDataError(err error) bool { | ||
return checkErrTypeAndCode(err, errorInvalidData) | ||
} | ||
|
||
// IsInvalidFieldError checks if a given error indicates that the provided field was invalid. | ||
func IsInvalidFieldError(err error) bool { | ||
return checkErrTypeAndCode(err, errorInvalidField) | ||
} | ||
|
||
// IsFieldValueNotFoundError checks if a given error indicates that the provided field was not found in the provided | ||
// data. | ||
func IsFieldValueNotFoundError(err error) bool { | ||
return checkErrTypeAndCode(err, errorFieldValueNotFound) | ||
} | ||
|
||
func checkErrTypeAndCode(err error, code errorCode) bool { | ||
dataErr, ok := err.(*DataError) | ||
if !ok { | ||
return false | ||
} | ||
return dataErr.code == code | ||
} |
Oops, something went wrong.