]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: use slices to simplify the code
authorapocelipes <seve3r@outlook.com>
Tue, 2 Apr 2024 13:41:00 +0000 (13:41 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 11 Apr 2024 16:40:14 +0000 (16:40 +0000)
Use "slices.Equal" instead of "reflect.DeepEqual".

Replace unnecessary helper type "byIndex" with "slices.SortFunc".

No effect on benchmarks.

Change-Id: I1fb2768ea6d9db7f487408fa109343be3f1741d5
GitHub-Last-Rev: 8429bc145272ae620fcd001b1de393bf3c0b6108
GitHub-Pull-Request: golang/go#66646
Reviewed-on: https://go-review.googlesource.com/c/go/+/575715
Reviewed-by: qiu laidongfeng2 <2645477756@qq.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/encoding/json/decode_test.go
src/encoding/json/encode.go

index a10c1e1ebb93638e414ae6f7dab2a6d81cd29646..f5b44677b341626036d1f26540277ebee908d84e 100644 (file)
@@ -14,6 +14,7 @@ import (
        "math/big"
        "net"
        "reflect"
+       "slices"
        "strconv"
        "strings"
        "testing"
@@ -1998,7 +1999,7 @@ func TestByteKind(t *testing.T) {
        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)
        }
 }
@@ -2017,7 +2018,7 @@ func TestSliceOfCustomByte(t *testing.T) {
        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)
        }
 }
index d6f6900dc969ef04dda02a9fbef51a1066be3086..0035a65cfc1f7cc4a9e9ac9ee4a8f9ca8bb71a3a 100644 (file)
@@ -1042,25 +1042,6 @@ type field struct {
        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.
@@ -1195,7 +1176,7 @@ func typeFields(t reflect.Type) structFields {
                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,
@@ -1227,7 +1208,9 @@ func typeFields(t reflect.Type) structFields {
        }
 
        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]