"math/big"
"net"
"reflect"
+ "slices"
"strconv"
"strings"
"testing"
if err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Fatalf("Marshal/Unmarshal mismatch:\n\tgot: %v\n\twant: %v", got, want)
}
}
if err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Fatalf("Marshal/Unmarshal mismatch:\n\tgot: %v\n\twant: %v", got, want)
}
}
encoder encoderFunc
}
-// byIndex sorts field by index sequence.
-type byIndex []field
-
-func (x byIndex) Len() int { return len(x) }
-
-func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
-
-func (x byIndex) Less(i, j int) bool {
- for k, xik := range x[i].index {
- if k >= len(x[j].index) {
- return false
- }
- if xik != x[j].index[k] {
- return xik < x[j].index[k]
- }
- }
- return len(x[i].index) < len(x[j].index)
-}
-
// typeFields returns a list of fields that JSON should recognize for the given type.
// The algorithm is breadth-first search over the set of structs to include - the top struct
// and then any reachable anonymous structs.
if x[i].tag != x[j].tag {
return x[i].tag
}
- return byIndex(x).Less(i, j)
+ return slices.Compare(x[i].index, x[j].index) == -1
})
// Delete all fields that are hidden by the Go rules for embedded fields,
}
fields = out
- sort.Sort(byIndex(fields))
+ slices.SortFunc(fields, func(i, j field) int {
+ return slices.Compare(i.index, j.index)
+ })
for i := range fields {
f := &fields[i]