diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2025-07-24 15:07:39 -0700 |
|---|---|---|
| committer | Joseph Tsai <joetsai@digital-static.net> | 2025-08-13 15:47:52 -0700 |
| commit | 4b1800e47632d52006e3080580e4e60792389759 (patch) | |
| tree | f2b43968d3d1297c999966c5633cd5833a270504 /src/encoding/json/v2/errors.go | |
| parent | af8870708bbaf15956a27cbab15582b4c666855e (diff) | |
| download | go-4b1800e47632d52006e3080580e4e60792389759.tar.xz | |
encoding/json/v2: cleanup error constructors
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>
Diffstat (limited to 'src/encoding/json/v2/errors.go')
| -rw-r--r-- | src/encoding/json/v2/errors.go | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/encoding/json/v2/errors.go b/src/encoding/json/v2/errors.go index 1f31505869..940b720210 100644 --- a/src/encoding/json/v2/errors.go +++ b/src/encoding/json/v2/errors.go @@ -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 } |
