Skip to content

Commit

Permalink
Adding field logic to data package
Browse files Browse the repository at this point in the history
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
MbolotSuse committed Oct 12, 2023
1 parent 0a0c449 commit aacc274
Show file tree
Hide file tree
Showing 5 changed files with 1,252 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/rancher/lasso v0.0.0-20230629200414-8a54b32e6792
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/sync v0.2.0
golang.org/x/text v0.11.0
golang.org/x/tools v0.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
50 changes: 50 additions & 0 deletions pkg/data/errors.go
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
}
Loading

0 comments on commit aacc274

Please sign in to comment.