Skip to content

Commit

Permalink
Remove merr package
Browse files Browse the repository at this point in the history
  • Loading branch information
JonCrowther committed Feb 2, 2024
1 parent 004384a commit 149fa6c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 93 deletions.
7 changes: 3 additions & 4 deletions pkg/apply/desiredset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/rancher/wrangler/v2/pkg/apply/injectors"
"github.com/rancher/wrangler/v2/pkg/kv"
"github.com/rancher/wrangler/v2/pkg/merr"
"github.com/rancher/wrangler/v2/pkg/objectset"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -50,19 +49,19 @@ type desiredSet struct {
injectors []injectors.ConfigInjector
ratelimitingQps float32
injectorNames []string
errs []error
errs error

createPlan bool
plan Plan
}

func (o *desiredSet) err(err error) error {
o.errs = append(o.errs, err)
o.errs = errors.Join(o.errs, err)
return o.Err()
}

func (o desiredSet) Err() error {
return merr.NewErrors(append(o.errs, o.objs.Err())...)
return errors.Join(o.errs, o.objs.Err())
}

func (o desiredSet) DryRun(objs ...runtime.Object) (Plan, error) {
Expand Down
26 changes: 13 additions & 13 deletions pkg/apply/desiredset_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package apply

import (
"context"
e "errors"
"fmt"
"sort"
"sync"

"github.com/pkg/errors"
gvk2 "github.com/rancher/wrangler/v2/pkg/gvk"
"github.com/rancher/wrangler/v2/pkg/merr"
"github.com/rancher/wrangler/v2/pkg/objectset"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -340,7 +340,7 @@ func (o *desiredSet) process(debugID string, set labels.Selector, gvk schema.Gro

func (o *desiredSet) list(namespaced bool, informer cache.SharedIndexInformer, client dynamic.NamespaceableResourceInterface, selector labels.Selector, desiredObjects objectset.ObjectByKey) (map[objectset.ObjectKey]runtime.Object, error) {
var (
errs []error
errs error
objs = objectset.ObjectByKey{}
)

Expand All @@ -361,25 +361,25 @@ func (o *desiredSet) list(namespaced bool, informer cache.SharedIndexInformer, c
// owner set and unspecified lister namespace, search all namespaces
err := allNamespaceList(o.ctx, client, selector, func(obj unstructured.Unstructured) {
if err := addObjectToMap(objs, &obj); err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
})
if err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
} else {
// no owner or lister namespace intentionally restricted; only search in specified namespaces
err := multiNamespaceList(o.ctx, namespaces, client, selector, func(obj unstructured.Unstructured) {
if err := addObjectToMap(objs, &obj); err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
})
if err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
}

return objs, merr.NewErrors(errs...)
return objs, errs
}

var namespace string
Expand All @@ -395,13 +395,13 @@ func (o *desiredSet) list(namespaced bool, informer cache.SharedIndexInformer, c

if err := cache.ListAllByNamespace(indexer, namespace, selector, func(obj interface{}) {
if err := addObjectToMap(objs, obj); err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
}); err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}

return objs, merr.NewErrors(errs...)
return objs, errs
}

func shouldPrune(obj runtime.Object) bool {
Expand Down Expand Up @@ -523,7 +523,7 @@ func inNamespace(namespace string, obj interface{}) bool {
// listByHash use a pre-configured indexer to list objects of a certain type by their hash label
func listByHash(indexer cache.Indexer, hash string, namespace string) (map[objectset.ObjectKey]runtime.Object, error) {
var (
errs []error
errs error
objs = objectset.ObjectByKey{}
)
res, err := indexer.ByIndex(byHash, hash)
Expand All @@ -535,8 +535,8 @@ func listByHash(indexer cache.Indexer, hash string, namespace string) (map[objec
continue
}
if err := addObjectToMap(objs, obj); err != nil {
errs = append(errs, err)
errs = e.Join(errs, err)
}
}
return objs, merr.NewErrors(errs...)
return objs, errs
}
37 changes: 0 additions & 37 deletions pkg/merr/error.go

This file was deleted.

17 changes: 6 additions & 11 deletions pkg/objectset/objectset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package objectset

import (
e "errors"
"fmt"
"reflect"
"sort"
Expand All @@ -9,7 +10,6 @@ import (
"github.com/rancher/wrangler/v2/pkg/gvk"
"github.com/rancher/wrangler/v2/pkg/stringset"

"github.com/rancher/wrangler/v2/pkg/merr"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -65,7 +65,7 @@ func (o ObjectByGVK) Add(obj runtime.Object) (schema.GroupVersionKind, error) {
}

type ObjectSet struct {
errs []error
errs error
objects ObjectByGVK
objectsByGK ObjectByGK
order []runtime.Object
Expand Down Expand Up @@ -113,13 +113,13 @@ func (o *ObjectSet) add(obj runtime.Object) {

gvk, err := o.objects.Add(obj)
if err != nil {
o.err(errors.Wrapf(err, "failed to add %T", obj))
o.AddErr(errors.Wrapf(err, "failed to add %T", obj))
return
}

_, err = o.objectsByGK.Add(obj)
if err != nil {
o.err(errors.Wrapf(err, "failed to add %T", obj))
o.AddErr(errors.Wrapf(err, "failed to add %T", obj))
return
}

Expand All @@ -130,17 +130,12 @@ func (o *ObjectSet) add(obj runtime.Object) {
}
}

func (o *ObjectSet) err(err error) error {
o.errs = append(o.errs, err)
return o.Err()
}

func (o *ObjectSet) AddErr(err error) {
o.errs = append(o.errs, err)
o.errs = e.Join(o.errs, err)
}

func (o *ObjectSet) Err() error {
return merr.NewErrors(o.errs...)
return o.errs
}

func (o *ObjectSet) Len() int {
Expand Down
4 changes: 2 additions & 2 deletions pkg/objectset/objectset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestObjectSet_Namespaces(t *testing.T) {
type fields struct {
errs []error
err error
objects ObjectByGVK
objectsByGK ObjectByGK
order []runtime.Object
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestObjectSet_Namespaces(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &ObjectSet{
errs: tt.fields.errs,
err: tt.fields.err,
objects: tt.fields.objects,
objectsByGK: tt.fields.objectsByGK,
order: tt.fields.order,
Expand Down
28 changes: 11 additions & 17 deletions pkg/schemas/mapper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package schemas

import (
"errors"

"github.com/rancher/wrangler/v2/pkg/data"
"github.com/rancher/wrangler/v2/pkg/data/convert"
"github.com/rancher/wrangler/v2/pkg/merr"
"github.com/rancher/wrangler/v2/pkg/schemas/definition"
)

Expand All @@ -22,11 +23,11 @@ func (m Mappers) FromInternal(data data.Object) {
}

func (m Mappers) ToInternal(data data.Object) error {
var errors []error
var returnErrors error
for i := len(m) - 1; i >= 0; i-- {
errors = append(errors, m[i].ToInternal(data))
returnErrors = errors.Join(returnErrors, m[i].ToInternal(data))
}
return merr.NewErrors(errors...)
return returnErrors
}

func (m Mappers) ModifySchema(schema *Schema, schemas *Schemas) error {
Expand Down Expand Up @@ -76,23 +77,16 @@ func (t *typeMapper) FromInternal(data data.Object) {
Mappers(t.Mappers).FromInternal(data)
}

func addError(errors []error, err error) []error {
if err == nil {
return errors
}
return append(errors, err)
}

func (t *typeMapper) ToInternal(data data.Object) error {
var errors []error
errors = addError(errors, Mappers(t.Mappers).ToInternal(data))
var returnErrors error
returnErrors = errors.Join(returnErrors, Mappers(t.Mappers).ToInternal(data))

for fieldName, schema := range t.subArraySchemas {
if schema.Mapper == nil {
continue
}
for _, fieldData := range data.Slice(fieldName) {
errors = addError(errors, schema.Mapper.ToInternal(fieldData))
returnErrors = errors.Join(returnErrors, schema.Mapper.ToInternal(fieldData))
}
}

Expand All @@ -101,18 +95,18 @@ func (t *typeMapper) ToInternal(data data.Object) error {
continue
}
for _, fieldData := range data.Map(fieldName) {
errors = addError(errors, schema.Mapper.ToInternal(convert.ToMapInterface(fieldData)))
returnErrors = errors.Join(returnErrors, schema.Mapper.ToInternal(convert.ToMapInterface(fieldData)))
}
}

for fieldName, schema := range t.subSchemas {
if schema.Mapper == nil {
continue
}
errors = addError(errors, schema.Mapper.ToInternal(data.Map(fieldName)))
returnErrors = errors.Join(returnErrors, schema.Mapper.ToInternal(data.Map(fieldName)))
}

return merr.NewErrors(errors...)
return returnErrors
}

func (t *typeMapper) ModifySchema(schema *Schema, schemas *Schemas) error {
Expand Down
16 changes: 7 additions & 9 deletions pkg/schemas/schemas.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package schemas

import (
"errors"
"fmt"
"reflect"
"strings"
"sync"

"github.com/rancher/wrangler/v2/pkg/data/convert"
"github.com/rancher/wrangler/v2/pkg/merr"
"github.com/rancher/wrangler/v2/pkg/name"
)

Expand Down Expand Up @@ -36,9 +36,7 @@ func EmptySchemas() *Schemas {
}

func NewSchemas(schemas ...*Schemas) (*Schemas, error) {
var (
errs []error
)
var errs error

s := &Schemas{
processingTypes: map[reflect.Type]*Schema{},
Expand All @@ -50,11 +48,11 @@ func NewSchemas(schemas ...*Schemas) (*Schemas, error) {

for _, schemas := range schemas {
if _, err := s.AddSchemas(schemas); err != nil {
errs = append(errs, err)
errs = errors.Join(errs, err)
}
}

return s, merr.NewErrors(errs...)
return s, errs
}

func (s *Schemas) Init(initFunc SchemasInitFunc) *Schemas {
Expand All @@ -70,13 +68,13 @@ func (s *Schemas) MustAddSchemas(schema *Schemas) *Schemas {
}

func (s *Schemas) AddSchemas(schema *Schemas) (*Schemas, error) {
var errs []error
var errs error
for _, schema := range schema.Schemas() {
if err := s.AddSchema(*schema); err != nil {
errs = append(errs, err)
errs = errors.Join(errs, err)
}
}
return s, merr.NewErrors(errs...)
return s, errs
}

func (s *Schemas) RemoveSchema(schema Schema) *Schemas {
Expand Down

0 comments on commit 149fa6c

Please sign in to comment.