Skip to content

Commit aacc274

Browse files
committed
Adding field logic to data package
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.
1 parent 0a0c449 commit aacc274

File tree

5 files changed

+1252
-0
lines changed

5 files changed

+1252
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/rancher/lasso v0.0.0-20230629200414-8a54b32e6792
1212
github.com/sirupsen/logrus v1.9.0
1313
github.com/stretchr/testify v1.8.2
14+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
1415
golang.org/x/sync v0.2.0
1516
golang.org/x/text v0.11.0
1617
golang.org/x/tools v0.8.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
113113
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
114114
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
115115
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
116+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
117+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
116118
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
117119
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
118120
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=

pkg/data/errors.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package data
2+
3+
type errorCode int
4+
5+
const (
6+
errorInvalidData errorCode = iota + 1
7+
errorInvalidField
8+
errorFieldValueNotFound
9+
)
10+
11+
type DataError struct {
12+
message string
13+
code errorCode
14+
}
15+
16+
// Error returns the underlying error message, satisfying the error interface
17+
func (d *DataError) Error() string {
18+
return d.message
19+
}
20+
21+
func newDataError(message string, code errorCode) *DataError {
22+
return &DataError{
23+
message: message,
24+
code: code,
25+
}
26+
}
27+
28+
// IsInvalidDataError checks if a given error indicates that the provided data field was invalid.
29+
func IsInvalidDataError(err error) bool {
30+
return checkErrTypeAndCode(err, errorInvalidData)
31+
}
32+
33+
// IsInvalidFieldError checks if a given error indicates that the provided field was invalid.
34+
func IsInvalidFieldError(err error) bool {
35+
return checkErrTypeAndCode(err, errorInvalidField)
36+
}
37+
38+
// IsFieldValueNotFoundError checks if a given error indicates that the provided field was not found in the provided
39+
// data.
40+
func IsFieldValueNotFoundError(err error) bool {
41+
return checkErrTypeAndCode(err, errorFieldValueNotFound)
42+
}
43+
44+
func checkErrTypeAndCode(err error, code errorCode) bool {
45+
dataErr, ok := err.(*DataError)
46+
if !ok {
47+
return false
48+
}
49+
return dataErr.code == code
50+
}

0 commit comments

Comments
 (0)