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 23, 2023
1 parent 3032665 commit 2e5983c
Show file tree
Hide file tree
Showing 4 changed files with 1,250 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 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 Expand Up @@ -71,7 +72,6 @@ require (
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
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 2e5983c

Please sign in to comment.