diff --git a/api/client/v1alpha1/common_types.go b/api/client/v1alpha1/common_types.go index 0209d530..9cfdc5d3 100644 --- a/api/client/v1alpha1/common_types.go +++ b/api/client/v1alpha1/common_types.go @@ -1,6 +1,7 @@ package v1alpha1 // TypeMeta contains type metadata. +// This structure is equivalent to k8s.io/apimachinery/pkg/apis/meta/v1.TypeMeta type TypeMeta struct { Kind string `json:"kind,omitempty"` APIVersion string `json:"apiVersion,omitempty"` diff --git a/go.mod b/go.mod index a0a2eabb..77b56272 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/buger/jsonparser v1.1.1 github.com/emporous/collection-spec v0.0.0-20230112181029-9df787e68bce github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 + github.com/hashicorp/go-multierror v1.1.1 github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249 github.com/sigstore/cosign v1.13.1 ) @@ -176,7 +177,6 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-retryablehttp v0.7.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/huandu/xstrings v1.3.2 // indirect diff --git a/nodes/descriptor/properties.go b/nodes/descriptor/properties.go index 955cfb6b..9a752735 100644 --- a/nodes/descriptor/properties.go +++ b/nodes/descriptor/properties.go @@ -8,11 +8,11 @@ import ( "github.com/buger/jsonparser" empspec "github.com/emporous/collection-spec/specs-go/v1alpha1" + "github.com/hashicorp/go-multierror" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/emporous/emporous-go/attributes" "github.com/emporous/emporous-go/model" - "github.com/emporous/emporous-go/util/errlist" ) var _ model.AttributeSet = &Properties{} @@ -207,41 +207,43 @@ func Parse(in map[string]json.RawMessage) (*Properties, error) { var out Properties other := map[string]model.AttributeSet{} + var result *multierror.Error + var errs []error for key, prop := range in { switch key { case TypeLink: var l empspec.LinkAttributes if err := json.Unmarshal(prop, &l); err != nil { - errs = append(errs, ParseError{Key: key, Err: err}) + multierror.Append(result, ParseError{Key: key, Err: err}) continue } out.Link = &l case TypeDescriptor: var d empspec.DescriptorAttributes if err := json.Unmarshal(prop, &d); err != nil { - errs = append(errs, ParseError{Key: key, Err: err}) + multierror.Append(result, ParseError{Key: key, Err: err}) continue } out.Descriptor = &d case TypeSchema: var s empspec.SchemaAttributes if err := json.Unmarshal(prop, &s); err != nil { - errs = append(errs, ParseError{Key: key, Err: err}) + multierror.Append(result, ParseError{Key: key, Err: err}) continue } out.Schema = &s case TypeRuntime: var r ocispec.ImageConfig if err := json.Unmarshal(prop, &r); err != nil { - errs = append(errs, ParseError{Key: key, Err: err}) + multierror.Append(result, ParseError{Key: key, Err: err}) continue } out.Runtime = &r case TypeFile: var f empspec.File if err := json.Unmarshal(prop, &f); err != nil { - errs = append(errs, ParseError{Key: key, Err: err}) + multierror.Append(result, ParseError{Key: key, Err: err}) continue } out.File = &f @@ -285,5 +287,5 @@ func Parse(in map[string]json.RawMessage) (*Properties, error) { } } out.Others = other - return &out, errlist.NewErrList(errs) + return &out, result.ErrorOrNil() } diff --git a/util/errlist/errlist.go b/util/errlist/errlist.go deleted file mode 100644 index 6740f037..00000000 --- a/util/errlist/errlist.go +++ /dev/null @@ -1,101 +0,0 @@ -package errlist - -import ( - "errors" - "strings" -) - -// Separator is used to separate error messages when calling Error on a list. -const Separator = ", " - -// ErrList defines methods for lists of type error. -type ErrList interface { - error - Errors() []error - Is(error) bool -} - -// NewErrList returns a new ErrList based on a given list of -func NewErrList(list []error) ErrList { - if len(list) == 0 { - return nil - } - // In case of input error list contains nil - var errs []error - for _, e := range list { - if e != nil { - errs = append(errs, e) - } - } - if len(errs) == 0 { - return nil - } - return errList(errs) -} - -type errList []error - -// visitErrFunc is a function that defines input and -// outputs when an error is visited. -type visitErrFunc func(error) bool - -func (el errList) Error() string { - if len(el) == 0 { - return "" - } - if len(el) == 1 { - return el[0].Error() - } - seenErrs := map[string]struct{}{} - result := strings.Builder{} - el.visitErr(func(err error) bool { - msg := err.Error() - if _, has := seenErrs[msg]; has { - return false - } - seenErrs[msg] = struct{}{} - if len(seenErrs) > 1 { - result.WriteString(Separator) - } - result.WriteString(msg) - return false - }) - if len(seenErrs) == 1 { - return result.String() - } - return "[" + result.String() + "]" -} - -func (el errList) Is(target error) bool { - return el.visitErr(func(err error) bool { - return errors.Is(err, target) - }) -} - -// visitsErr visits each error and stops if a match is found. -func (el errList) visitErr(f visitErrFunc) bool { - for _, err := range el { - switch err := err.(type) { - case errList: - if match := err.visitErr(f); match { - return match - } - case ErrList: - for _, nestedErr := range err.Errors() { - if match := f(nestedErr); match { - return match - } - } - default: - if match := f(err); match { - return match - } - } - } - - return false -} - -func (el errList) Errors() []error { - return el -}