Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all fmt.Errorf occurrences with errors.New using either fmt.Sprintf or strings.Join #454

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions any.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Any interface {
type baseAny struct{}

func (any *baseAny) Get(path ...interface{}) Any {
return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("GetIndex %v from simple value", path))}
}

func (any *baseAny) Size() int {
Expand Down Expand Up @@ -142,7 +142,7 @@ func Wrap(val interface{}) Any {
}
return &falseAny{}
}
return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("unsupported type: %v", typ))}
}

// ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
Expand Down
11 changes: 7 additions & 4 deletions any_invalid.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package jsoniter

import "fmt"
import (
"fmt"
"errors"
)

type invalidAny struct {
baseAny
err error
}

func newInvalidAny(path []interface{}) *invalidAny {
return &invalidAny{baseAny{}, fmt.Errorf("%v not found", path)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("%v not found", path))}
}

func (any *invalidAny) LastError() error {
Expand Down Expand Up @@ -68,9 +71,9 @@ func (any *invalidAny) WriteTo(stream *Stream) {

func (any *invalidAny) Get(path ...interface{}) Any {
if any.err == nil {
return &invalidAny{baseAny{}, fmt.Errorf("get %v from invalid", path)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("get %v from invalid", path))}
}
return &invalidAny{baseAny{}, fmt.Errorf("%v, get %v from invalid", any.err, path)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("%v, get %v from invalid", any.err, path))}
}

func (any *invalidAny) Parse() *Iterator {
Expand Down
3 changes: 2 additions & 1 deletion any_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsoniter
import (
"fmt"
"strconv"
"errors"
)

type stringAny struct {
Expand All @@ -14,7 +15,7 @@ func (any *stringAny) Get(path ...interface{}) Any {
if len(path) == 0 {
return any
}
return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
return &invalidAny{baseAny{}, errors.New(fmt.Sprintf("GetIndex %v from simple value", path))}
}

func (any *stringAny) Parse() *Iterator {
Expand Down
5 changes: 3 additions & 2 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"errors"
)

// ValueType the type for JSON element
Expand Down Expand Up @@ -221,8 +222,8 @@ func (iter *Iterator) ReportError(operation string, msg string) {
contextEnd = iter.tail
}
context := string(iter.buf[contextStart:contextEnd])
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
operation, msg, iter.head-peekStart, parsing, context)
iter.Error = errors.New(fmt.Sprintf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
operation, msg, iter.head-peekStart, parsing, context))
}

// CurrentBuffer gets current buffer as string for debugging purpose
Expand Down
40 changes: 40 additions & 0 deletions misc_tests/jsoniter_error_log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package misc_tests

import (
"errors"
"fmt"
"strconv"
"strings"
"testing"
)

// 7793382 152 ns/op 40 B/op 3 allocs/op
// 14880938 78 ns/op 23 B/op 1 allocs/op
// 10051323 119 ns/op 24 B/op 2 allocs/op
func Benchmark_fmt_errorf(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
fmt.Errorf("Error #%d", n)
}
}

func Benchmark_errors_new_join(b *testing.B) {
for n := 0; n < b.N; n++ {
errors.New(strings.Join([]string{"Error #", strconv.Itoa(n)}, ""))
}
}

func Benchmark_errors_new_sprintf(b *testing.B) {
for n := 0; n < b.N; n++ {
errors.New(fmt.Sprintf("Error #%d", n))
}
}

func TestErrorsAreEquivalent(t *testing.T) {
errorf := fmt.Errorf("Error #%d", 1).Error()
join := errors.New(strings.Join([]string{"Error #", strconv.Itoa(1)}, "")).Error()
sprintf := errors.New(fmt.Sprintf("Error #%d", 1)).Error()
if errorf != join || errorf != sprintf {
t.Fatalf("Errors are not equal. [errorf: %s] [join: %s] [sprintf: %s]", errorf, join, sprintf)
}
}
11 changes: 8 additions & 3 deletions reflect.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package jsoniter

import (
"fmt"
"errors"
"reflect"
"strings"
"unsafe"

"github.com/modern-go/reflect2"
Expand Down Expand Up @@ -183,7 +184,9 @@ func _createDecoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
case reflect.Ptr:
return decoderOfOptional(ctx, typ)
default:
return &lazyErrorDecoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
return &lazyErrorDecoder{err: errors.New(strings.Join([]string{
ctx.prefix, typ.String(), " is unsupported type",
}, ""))}
}
}

Expand Down Expand Up @@ -282,7 +285,9 @@ func _createEncoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
case reflect.Ptr:
return encoderOfOptional(ctx, typ)
default:
return &lazyErrorEncoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
return &lazyErrorEncoder{err: errors.New(strings.Join([]string{
ctx.prefix, typ.String(), " is unsupported type",
}, ""))}
}
}

Expand Down
5 changes: 3 additions & 2 deletions reflect_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/modern-go/reflect2"
"io"
"unsafe"
"errors"
)

func decoderOfArray(ctx *ctx, typ reflect2.Type) ValDecoder {
Expand Down Expand Up @@ -48,7 +49,7 @@ func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
}
stream.WriteArrayEnd()
if stream.Error != nil && stream.Error != io.EOF {
stream.Error = fmt.Errorf("%v: %s", encoder.arrayType, stream.Error.Error())
stream.Error = errors.New(fmt.Sprintf("%v: %s", encoder.arrayType, stream.Error.Error()))
}
}

Expand All @@ -64,7 +65,7 @@ type arrayDecoder struct {
func (decoder *arrayDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
decoder.doDecode(ptr, iter)
if iter.Error != nil && iter.Error != io.EOF {
iter.Error = fmt.Errorf("%v: %s", decoder.arrayType, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v: %s", decoder.arrayType, iter.Error.Error()))
}
}

Expand Down
5 changes: 3 additions & 2 deletions reflect_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"sort"
"unsafe"
"errors"
)

func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder {
Expand Down Expand Up @@ -88,7 +89,7 @@ func decoderOfMapKey(ctx *ctx, typ reflect2.Type) ValDecoder {
valType: typ,
}
}
return &lazyErrorDecoder{err: fmt.Errorf("unsupported map key type: %v", typ)}
return &lazyErrorDecoder{err: errors.New(fmt.Sprintf("unsupported map key type: %v", typ))}
}
}

Expand Down Expand Up @@ -131,7 +132,7 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
if typ.Kind() == reflect.Interface {
return &dynamicMapKeyEncoder{ctx, typ}
}
return &lazyErrorEncoder{err: fmt.Errorf("unsupported map key type: %v", typ)}
return &lazyErrorEncoder{err: errors.New(fmt.Sprintf("unsupported map key type: %v", typ))}
}
}

Expand Down
5 changes: 3 additions & 2 deletions reflect_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/modern-go/reflect2"
"io"
"unsafe"
"errors"
)

func decoderOfSlice(ctx *ctx, typ reflect2.Type) ValDecoder {
Expand Down Expand Up @@ -43,7 +44,7 @@ func (encoder *sliceEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
}
stream.WriteArrayEnd()
if stream.Error != nil && stream.Error != io.EOF {
stream.Error = fmt.Errorf("%v: %s", encoder.sliceType, stream.Error.Error())
stream.Error = errors.New(fmt.Sprintf("%v: %s", encoder.sliceType, stream.Error.Error()))
}
}

Expand All @@ -59,7 +60,7 @@ type sliceDecoder struct {
func (decoder *sliceDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
decoder.doDecode(ptr, iter)
if iter.Error != nil && iter.Error != io.EOF {
iter.Error = fmt.Errorf("%v: %s", decoder.sliceType, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v: %s", decoder.sliceType, iter.Error.Error()))
}
}

Expand Down
25 changes: 13 additions & 12 deletions reflect_struct_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"strings"
"unsafe"
"errors"

"github.com/modern-go/reflect2"
)
Expand Down Expand Up @@ -508,7 +509,7 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
decoder.decodeOneField(ptr, iter)
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
if c != '}' {
iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c}))
Expand Down Expand Up @@ -589,7 +590,7 @@ func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -623,7 +624,7 @@ func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -661,7 +662,7 @@ func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -703,7 +704,7 @@ func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -749,7 +750,7 @@ func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -799,7 +800,7 @@ func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -853,7 +854,7 @@ func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -911,7 +912,7 @@ func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -973,7 +974,7 @@ func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand Down Expand Up @@ -1039,7 +1040,7 @@ func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
}
}
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%v.%s", decoder.typ, iter.Error.Error()))
}
iter.decrementDepth()
}
Expand All @@ -1053,7 +1054,7 @@ func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
fieldPtr := decoder.field.UnsafeGet(ptr)
decoder.fieldDecoder.Decode(fieldPtr, iter)
if iter.Error != nil && iter.Error != io.EOF {
iter.Error = fmt.Errorf("%s: %s", decoder.field.Name(), iter.Error.Error())
iter.Error = errors.New(fmt.Sprintf("%s: %s", decoder.field.Name(), iter.Error.Error()))
}
}

Expand Down
8 changes: 5 additions & 3 deletions reflect_struct_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"reflect"
"unsafe"
"errors"
"strings"
)

func encoderOfStruct(ctx *ctx, typ reflect2.Type) ValEncoder {
Expand Down Expand Up @@ -66,7 +68,7 @@ func createCheckIsEmpty(ctx *ctx, typ reflect2.Type) checkIsEmpty {
case reflect.Ptr:
return &OptionalEncoder{}
default:
return &lazyErrorEncoder{err: fmt.Errorf("unsupported type: %v", typ)}
return &lazyErrorEncoder{err: errors.New(fmt.Sprintf("unsupported type: %v", typ))}
}
}

Expand Down Expand Up @@ -109,7 +111,7 @@ func (encoder *structFieldEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
fieldPtr := encoder.field.UnsafeGet(ptr)
encoder.fieldEncoder.Encode(fieldPtr, stream)
if stream.Error != nil && stream.Error != io.EOF {
stream.Error = fmt.Errorf("%s: %s", encoder.field.Name(), stream.Error.Error())
stream.Error = errors.New(strings.Join([]string{encoder.field.Name(), stream.Error.Error()}, ": "))
}
}

Expand Down Expand Up @@ -160,7 +162,7 @@ func (encoder *structEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
}
stream.WriteObjectEnd()
if stream.Error != nil && stream.Error != io.EOF {
stream.Error = fmt.Errorf("%v.%s", encoder.typ, stream.Error.Error())
stream.Error = errors.New(fmt.Sprintf("%v.%s", encoder.typ, stream.Error.Error()))
}
}

Expand Down
Loading