]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json/v2: cleanup error constructors
authorJoe Tsai <joetsai@digital-static.net>
Thu, 24 Jul 2025 22:07:39 +0000 (15:07 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Wed, 13 Aug 2025 22:47:52 +0000 (15:47 -0700)
There is no need to explicitly pass in the options
since this contained within the Encoder or Decoder struct
ever since https://github.com/go-json-experiment/json/pull/163.

Thus, remove it as an argument and fetch it from the coder.

This only modifies code that is compiled in under goexperiment.jsonv2.

Change-Id: I6c928b864bf7869889d7ee7d5c1d396fbe71296b
Reviewed-on: https://go-review.googlesource.com/c/go/+/695278
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/encoding/json/v2/arshal_default.go
src/encoding/json/v2/arshal_inlined.go
src/encoding/json/v2/arshal_time.go
src/encoding/json/v2/errors.go

index f3fc79beac0af4cc546223d92997904a1c2cd76b..c2307fa31d7fcc3d71f8fe9b09a4664fcebc75a6 100644 (file)
@@ -128,7 +128,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
 
                // Optimize for marshaling without preceding whitespace.
@@ -153,7 +153,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                tok, err := dec.ReadToken()
                if err != nil {
@@ -190,7 +190,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
                                return nil
                        }
                }
-               return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
+               return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
        }
        return &fncs
 }
@@ -200,7 +200,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
 
                // Optimize for marshaling without preceding whitespace.
@@ -237,7 +237,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                var flags jsonwire.ValueFlags
                val, err := xd.ReadValue(&flags)
@@ -327,7 +327,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
                                mo.Format = ""
                                return marshalArray(enc, va, mo)
                        default:
-                               return newInvalidFormatError(enc, t, mo)
+                               return newInvalidFormatError(enc, t)
                        }
                } else if mo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
                        return marshalArray(enc, va, mo)
@@ -365,7 +365,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
                                uo.Format = ""
                                return unmarshalArray(dec, va, uo)
                        default:
-                               return newInvalidFormatError(dec, t, uo)
+                               return newInvalidFormatError(dec, t)
                        }
                } else if uo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
                        return unmarshalArray(dec, va, uo)
@@ -433,7 +433,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
 
                // Optimize for marshaling without preceding whitespace or string escaping.
@@ -454,7 +454,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
                var flags jsonwire.ValueFlags
@@ -520,7 +520,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
 
                // Optimize for marshaling without preceding whitespace or string escaping.
@@ -541,7 +541,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
                var flags jsonwire.ValueFlags
@@ -602,7 +602,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
                        if mo.Format == "nonfinite" {
                                allowNonFinite = true
                        } else {
-                               return newInvalidFormatError(enc, t, mo)
+                               return newInvalidFormatError(enc, t)
                        }
                }
 
@@ -637,7 +637,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
                        if uo.Format == "nonfinite" {
                                allowNonFinite = true
                        } else {
-                               return newInvalidFormatError(dec, t, uo)
+                               return newInvalidFormatError(dec, t)
                        }
                }
                stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
@@ -737,7 +737,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
                                emitNull = false
                                mo.Format = ""
                        default:
-                               return newInvalidFormatError(enc, t, mo)
+                               return newInvalidFormatError(enc, t)
                        }
                }
 
@@ -882,7 +882,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
                        case "emitnull", "emitempty":
                                uo.Format = "" // only relevant for marshaling
                        default:
-                               return newInvalidFormatError(dec, t, uo)
+                               return newInvalidFormatError(dec, t)
                        }
                }
                tok, err := dec.ReadToken()
@@ -992,7 +992,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
                        }
                        return errUnmarshal
                }
-               return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
+               return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
        }
        return &fncs
 }
@@ -1037,7 +1037,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
                once.Do(init)
                if errInit != nil && !mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
@@ -1199,7 +1199,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                tok, err := dec.ReadToken()
                if err != nil {
@@ -1317,7 +1317,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
                        }
                        return errUnmarshal
                }
-               return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
+               return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
        }
        return &fncs
 }
@@ -1414,7 +1414,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
                                emitNull = false
                                mo.Format = ""
                        default:
-                               return newInvalidFormatError(enc, t, mo)
+                               return newInvalidFormatError(enc, t)
                        }
                }
 
@@ -1462,7 +1462,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
                        case "emitnull", "emitempty":
                                uo.Format = "" // only relevant for marshaling
                        default:
-                               return newInvalidFormatError(dec, t, uo)
+                               return newInvalidFormatError(dec, t)
                        }
                }
 
@@ -1518,7 +1518,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
                        }
                        return errUnmarshal
                }
-               return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
+               return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
        }
        return &fncs
 }
@@ -1539,7 +1539,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
                once.Do(init)
                if err := enc.WriteToken(jsontext.BeginArray); err != nil {
@@ -1563,7 +1563,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                tok, err := dec.ReadToken()
                if err != nil {
@@ -1616,7 +1616,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
                        }
                        return errUnmarshal
                }
-               return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
+               return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
        }
        return &fncs
 }
@@ -1706,7 +1706,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
        fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
                xe := export.Encoder(enc)
                if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
-                       return newInvalidFormatError(enc, t, mo)
+                       return newInvalidFormatError(enc, t)
                }
                if va.IsNil() {
                        return enc.WriteToken(jsontext.Null)
@@ -1746,7 +1746,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
        fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
                xd := export.Decoder(dec)
                if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
-                       return newInvalidFormatError(dec, t, uo)
+                       return newInvalidFormatError(dec, t)
                }
                if uo.Flags.Get(jsonflags.MergeWithLegacySemantics) && !va.IsNil() {
                        // Legacy merge behavior is difficult to explain.
@@ -1795,7 +1795,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
 
                        k := dec.PeekKind()
                        if !isAnyType(t) {
-                               return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, internal.ErrNilInterface)
+                               return newUnmarshalErrorBeforeWithSkipping(dec, t, internal.ErrNilInterface)
                        }
                        switch k {
                        case 'f', 't':
index 6299cc4a428ae17a61bff45ca779cfbfc2f2f9d1..a25314450251f4b37085ff29b321c6f688886282 100644 (file)
@@ -188,7 +188,7 @@ func unmarshalInlinedFallbackNext(dec *jsontext.Decoder, va addressableValue, uo
                                        *b = append(*b, ',')
                                }
                        } else {
-                               return newUnmarshalErrorAfterWithSkipping(dec, uo, v.Type(), errRawInlinedNotObject)
+                               return newUnmarshalErrorAfterWithSkipping(dec, v.Type(), errRawInlinedNotObject)
                        }
                }
                *b = append(*b, quotedName...)
index 06fed03e05fbed1cbf5588351746b0d52e8df95a..ac29abe5021ca9352e32c972ff4da7579d64f9bc 100644 (file)
@@ -48,7 +48,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
                        var m durationArshaler
                        if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
                                if !m.initFormat(mo.Format) {
-                                       return newInvalidFormatError(enc, t, mo)
+                                       return newInvalidFormatError(enc, t)
                                }
                        } else if mo.Flags.Get(jsonflags.FormatDurationAsNano) {
                                return marshalNano(enc, va, mo)
@@ -74,13 +74,13 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
                        var u durationArshaler
                        if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
                                if !u.initFormat(uo.Format) {
-                                       return newInvalidFormatError(dec, t, uo)
+                                       return newInvalidFormatError(dec, t)
                                }
                        } else if uo.Flags.Get(jsonflags.FormatDurationAsNano) {
                                return unmarshalNano(dec, va, uo)
                        } else {
                                // TODO(https://go.dev/issue/71631): Decide on default duration representation.
-                               return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
+                               return newUnmarshalErrorBeforeWithSkipping(dec, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
                        }
 
                        stringify := !u.isNumeric() || xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
@@ -125,7 +125,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
                        var m timeArshaler
                        if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
                                if !m.initFormat(mo.Format) {
-                                       return newInvalidFormatError(enc, t, mo)
+                                       return newInvalidFormatError(enc, t)
                                }
                        }
 
@@ -148,7 +148,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
                        var u timeArshaler
                        if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
                                if !u.initFormat(uo.Format) {
-                                       return newInvalidFormatError(dec, t, uo)
+                                       return newInvalidFormatError(dec, t)
                                }
                        } else if uo.Flags.Get(jsonflags.ParseTimeWithLooseRFC3339) {
                                u.looseRFC3339 = true
index 1f31505869238159b9ae936df634a0806e242e71..940b720210eee6900cdbdefa2c9293851998a11f 100644 (file)
@@ -88,7 +88,10 @@ type SemanticError struct {
 }
 
 // coder is implemented by [jsontext.Encoder] or [jsontext.Decoder].
-type coder interface{ StackPointer() jsontext.Pointer }
+type coder interface {
+       StackPointer() jsontext.Pointer
+       Options() Options
+}
 
 // newInvalidFormatError wraps err in a SemanticError because
 // the current type t cannot handle the provided options format.
@@ -97,13 +100,13 @@ type coder interface{ StackPointer() jsontext.Pointer }
 // If [jsonflags.ReportErrorsWithLegacySemantics] is specified,
 // then this automatically skips the next value when unmarshaling
 // to ensure that the value is fully consumed.
-func newInvalidFormatError(c coder, t reflect.Type, o *jsonopts.Struct) error {
-       err := fmt.Errorf("invalid format flag %q", o.Format)
+func newInvalidFormatError(c coder, t reflect.Type) error {
+       err := fmt.Errorf("invalid format flag %q", c.Options().(*jsonopts.Struct).Format)
        switch c := c.(type) {
        case *jsontext.Encoder:
                err = newMarshalErrorBefore(c, t, err)
        case *jsontext.Decoder:
-               err = newUnmarshalErrorBeforeWithSkipping(c, o, t, err)
+               err = newUnmarshalErrorBeforeWithSkipping(c, t, err)
        }
        return err
 }
@@ -136,9 +139,9 @@ func newUnmarshalErrorBefore(d *jsontext.Decoder, t reflect.Type, err error) err
 // newUnmarshalErrorBeforeWithSkipping is like [newUnmarshalErrorBefore],
 // but automatically skips the next value if
 // [jsonflags.ReportErrorsWithLegacySemantics] is specified.
-func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
+func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
        err = newUnmarshalErrorBefore(d, t, err)
-       if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
+       if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
                if err2 := export.Decoder(d).SkipValue(); err2 != nil {
                        return err2
                }
@@ -170,9 +173,9 @@ func newUnmarshalErrorAfterWithValue(d *jsontext.Decoder, t reflect.Type, err er
 // newUnmarshalErrorAfterWithSkipping is like [newUnmarshalErrorAfter],
 // but automatically skips the remainder of the current value if
 // [jsonflags.ReportErrorsWithLegacySemantics] is specified.
-func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
+func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
        err = newUnmarshalErrorAfter(d, t, err)
-       if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
+       if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
                if err2 := export.Decoder(d).SkipValueRemainder(); err2 != nil {
                        return err2
                }