Skip to content

Commit 7634fc1

Browse files
authored
Use errlist instead of copying code from Go. (#818)
1 parent 770dbdc commit 7634fc1

File tree

3 files changed

+17
-168
lines changed

3 files changed

+17
-168
lines changed

ygot/join.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

ygot/join_test.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

ygot/struct_validation_map.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"reflect"
3030
"strings"
3131

32+
"github.com/openconfig/gnmi/errlist"
3233
"github.com/openconfig/ygot/util"
3334
)
3435

@@ -672,21 +673,22 @@ func copyStruct(dstVal, srcVal reflect.Value, accessPath string, opts ...MergeOp
672673
return fmt.Errorf("cannot handle non-struct types, src: %v, dst: %v", srcVal.Type().Kind(), dstVal.Type().Kind())
673674
}
674675

675-
var errs []error
676+
var errs errlist.Error
677+
errs.Separator = "\n"
676678
for i := 0; i < srcVal.NumField(); i++ {
677679
srcField := srcVal.Field(i)
678680
dstField := dstVal.Field(i)
679681
accessPath := accessPath + "." + srcVal.Type().Field(i).Name
680682

681683
switch srcField.Kind() {
682684
case reflect.Ptr:
683-
errs = append(errs, copyPtrField(dstField, srcField, accessPath, opts...))
685+
errs.Add(copyPtrField(dstField, srcField, accessPath, opts...))
684686
case reflect.Interface:
685-
errs = append(errs, copyInterfaceField(dstField, srcField, accessPath, opts...))
687+
errs.Add(copyInterfaceField(dstField, srcField, accessPath, opts...))
686688
case reflect.Map:
687-
errs = append(errs, copyMapField(dstField, srcField, accessPath, opts...))
689+
errs.Add(copyMapField(dstField, srcField, accessPath, opts...))
688690
case reflect.Slice:
689-
errs = append(errs, copySliceField(dstField, srcField, accessPath, opts...))
691+
errs.Add(copySliceField(dstField, srcField, accessPath, opts...))
690692
case reflect.Int64:
691693
// In the case of an int64 field, which represents a YANG enumeration
692694
// we should only set the value in the destination if it is not set
@@ -695,7 +697,7 @@ func copyStruct(dstVal, srcVal reflect.Value, accessPath string, opts ...MergeOp
695697
switch {
696698
case vSrc != 0 && vDst != 0 && vSrc != vDst:
697699
if !fieldOverwriteEnabled(opts) {
698-
errs = append(errs, fmt.Errorf("%s: destination and source values were set when merging enum field, dst: %d, src: %d", accessPath, vSrc, vDst))
700+
errs.Add(fmt.Errorf("%s: destination and source values were set when merging enum field, dst: %d, src: %d", accessPath, vSrc, vDst))
699701
break
700702
}
701703
dstField.Set(srcField)
@@ -706,7 +708,7 @@ func copyStruct(dstVal, srcVal reflect.Value, accessPath string, opts ...MergeOp
706708
dstField.Set(srcField)
707709
}
708710
}
709-
return joinErrors(errs...)
711+
return errs.Err()
710712
}
711713

712714
// copyPtrField copies srcField to dstField. srcField and dstField must be
@@ -851,20 +853,21 @@ func copyMapField(dstField, srcField reflect.Value, accessPath string, opts ...M
851853
dstKeys[k.Interface()] = true
852854
}
853855

854-
var errs []error
856+
errs := &errlist.Error{}
857+
errs.Separator = "\n"
855858
for _, k := range srcField.MapKeys() {
856859
v := srcField.MapIndex(k)
857860
d := reflect.New(v.Elem().Type())
858861
if _, ok := dstKeys[k.Interface()]; ok {
859862
d = dstField.MapIndex(k)
860863
}
861864
if err := copyStruct(d.Elem(), v.Elem(), fmt.Sprintf("%s[%#v]", accessPath, k.Interface()), opts...); err != nil {
862-
errs = append(errs, err)
865+
errs.Add(err)
863866
continue
864867
}
865868
dstField.SetMapIndex(k, d)
866869
}
867-
return joinErrors(errs...)
870+
return errs.Err()
868871
}
869872

870873
// mapTypes provides a specification of a map.
@@ -935,17 +938,18 @@ func copySliceField(dstField, srcField reflect.Value, accessPath string, opts ..
935938
return nil
936939
}
937940

938-
var errs []error
941+
errs := &errlist.Error{}
942+
errs.Separator = "\n"
939943
for i := 0; i < srcField.Len(); i++ {
940944
v := srcField.Index(i)
941945
d := reflect.New(v.Type().Elem())
942946
if err := copyStruct(d.Elem(), v.Elem(), fmt.Sprintf("%s[%v]", accessPath, i), opts...); err != nil {
943-
errs = append(errs, err)
947+
errs.Add(err)
944948
continue
945949
}
946950
dstField.Set(reflect.Append(dstField, v))
947951
}
948-
return joinErrors(errs...)
952+
return errs.Err()
949953
}
950954

951955
// uniqueSlices takes two reflect.Values which must represent slices, and determines

0 commit comments

Comments
 (0)