diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 1bd2c95..1ec1dfb 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -40,7 +40,7 @@ jobs: runs-on: ubuntu-latest steps: - - name: Steup Go + - name: Setup Go uses: actions/setup-go@v2 with: go-version: "1.24" diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..01db160 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,7 @@ +version: "2" +linters: + exclusions: + rules: + # Tests copied from the stdlib are not meant to be linted. + - path: 'golang_(.+_)?test\.go' + source: "^" # regex diff --git a/benchmarks/Makefile b/benchmarks/Makefile index e14cb3e..8b36507 100644 --- a/benchmarks/Makefile +++ b/benchmarks/Makefile @@ -74,7 +74,7 @@ benchstat := ${GOPATH}/bin/benchstat all: $(benchstat): - go install golang.org/x/perf/cmd/benchstat + go install golang.org/x/perf/cmd/benchstat@latest $(benchmark.cmd.dir)/message.pb.go: $(benchmark.cmd.dir)/message.proto @protoc -I. \ diff --git a/json/codec.go b/json/codec.go index 32c078f..77fe264 100644 --- a/json/codec.go +++ b/json/codec.go @@ -4,6 +4,7 @@ import ( "encoding" "encoding/json" "fmt" + "maps" "math/big" "reflect" "sort" @@ -73,12 +74,9 @@ func cacheLoad() map[unsafe.Pointer]codec { func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) { newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1) + maps.Copy(newCodecs, oldCodecs) newCodecs[typeid(typ)] = cod - for t, c := range oldCodecs { - newCodecs[t] = c - } - cache.Store(&newCodecs) } @@ -205,7 +203,7 @@ func constructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr b c = constructUnsupportedTypeCodec(t) } - p := reflect.PtrTo(t) + p := reflect.PointerTo(t) if canAddr { switch { @@ -291,7 +289,7 @@ func constructSliceCodec(t reflect.Type, seen map[reflect.Type]*structType) code // Go 1.7+ behavior: slices of byte types (and aliases) may override the // default encoding and decoding behaviors by implementing marshaler and // unmarshaler interfaces. - p := reflect.PtrTo(e) + p := reflect.PointerTo(e) c := codec{} switch { @@ -391,7 +389,7 @@ func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec kc := codec{} vc := constructCodec(v, seen, false) - if k.Implements(textMarshalerType) || reflect.PtrTo(k).Implements(textUnmarshalerType) { + if k.Implements(textMarshalerType) || reflect.PointerTo(k).Implements(textUnmarshalerType) { kc.encode = constructTextMarshalerEncodeFunc(k, false) kc.decode = constructTextUnmarshalerDecodeFunc(k, true) @@ -972,7 +970,6 @@ type structType struct { ficaseIndex map[string]*structField keyset []byte typ reflect.Type - inlined bool } type structField struct { @@ -1095,10 +1092,10 @@ var ( timeType = reflect.TypeOf(time.Time{}) rawMessageType = reflect.TypeOf(RawMessage(nil)) - numberPtrType = reflect.PtrTo(numberType) - durationPtrType = reflect.PtrTo(durationType) - timePtrType = reflect.PtrTo(timeType) - rawMessagePtrType = reflect.PtrTo(rawMessageType) + numberPtrType = reflect.PointerTo(numberType) + durationPtrType = reflect.PointerTo(durationType) + timePtrType = reflect.PointerTo(timeType) + rawMessagePtrType = reflect.PointerTo(rawMessageType) sliceInterfaceType = reflect.TypeOf(([]any)(nil)) sliceStringType = reflect.TypeOf(([]any)(nil)) diff --git a/json/decode.go b/json/decode.go index b44dde6..c87f01e 100644 --- a/json/decode.go +++ b/json/decode.go @@ -1410,7 +1410,7 @@ func (d decoder) decodeMaybeEmptyInterface(b []byte, p unsafe.Pointer, t reflect return d.decodeUnmarshalTypeError(b, p, t) } -func (d decoder) decodeUnmarshalTypeError(b []byte, p unsafe.Pointer, t reflect.Type) ([]byte, error) { +func (d decoder) decodeUnmarshalTypeError(b []byte, _ unsafe.Pointer, t reflect.Type) ([]byte, error) { v, b, _, err := d.parseValue(b) if err != nil { return b, err @@ -1500,7 +1500,7 @@ func (d decoder) decodeTextUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Typ value = "array" } - return b, &UnmarshalTypeError{Value: value, Type: reflect.PtrTo(t)} + return b, &UnmarshalTypeError{Value: value, Type: reflect.PointerTo(t)} } func (d decoder) prependField(key, field string) string { diff --git a/json/json.go b/json/json.go index 11ec69c..028fd1f 100644 --- a/json/json.go +++ b/json/json.go @@ -15,7 +15,7 @@ import ( type Delim = json.Delim // InvalidUTF8Error is documented at https://golang.org/pkg/encoding/json/#InvalidUTF8Error -type InvalidUTF8Error = json.InvalidUTF8Error +type InvalidUTF8Error = json.InvalidUTF8Error //nolint:staticcheck // compat. // InvalidUnmarshalError is documented at https://golang.org/pkg/encoding/json/#InvalidUnmarshalError type InvalidUnmarshalError = json.InvalidUnmarshalError @@ -39,7 +39,7 @@ type SyntaxError = json.SyntaxError type Token = json.Token // UnmarshalFieldError is documented at https://golang.org/pkg/encoding/json/#UnmarshalFieldError -type UnmarshalFieldError = json.UnmarshalFieldError +type UnmarshalFieldError = json.UnmarshalFieldError //nolint:staticcheck // compat. // UnmarshalTypeError is documented at https://golang.org/pkg/encoding/json/#UnmarshalTypeError type UnmarshalTypeError = json.UnmarshalTypeError diff --git a/json/json_test.go b/json/json_test.go index b40e000..8256be2 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -240,7 +240,7 @@ var testValues = [...]any{ A string `json:"name"` B string `json:"-"` C string `json:",omitempty"` - D map[string]any `json:",string"` + D map[string]any `json:",string"` //nolint:staticcheck // intentional e string }{A: "Luke", D: map[string]any{"answer": float64(42)}}, struct{ point }{point{1, 2}}, @@ -880,12 +880,11 @@ func TestDecodeLines(t *testing.T) { t.Run(test.desc, func(t *testing.T) { d := NewDecoder(test.reader) var count int - var err error for { var o obj - err = d.Decode(&o) + err := d.Decode(&o) if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } @@ -904,10 +903,6 @@ func TestDecodeLines(t *testing.T) { count++ } - if err != nil && err != io.EOF { - t.Error(err) - } - if count != test.expectCount { t.Errorf("expected %d objects, got %d", test.expectCount, count) } diff --git a/json/parse.go b/json/parse.go index 949e7f3..d0ee221 100644 --- a/json/parse.go +++ b/json/parse.go @@ -21,11 +21,6 @@ const ( cr = '\r' ) -const ( - escape = '\\' - quote = '"' -) - func internalParseFlags(b []byte) (flags ParseFlags) { // Don't consider surrounding whitespace b = skipSpaces(b)