Skip to content

Commit 4b1800e

Browse files
committed
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 go-json-experiment/json#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>
1 parent af88707 commit 4b1800e

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

src/encoding/json/v2/arshal_default.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
128128
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
129129
xe := export.Encoder(enc)
130130
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
131-
return newInvalidFormatError(enc, t, mo)
131+
return newInvalidFormatError(enc, t)
132132
}
133133

134134
// Optimize for marshaling without preceding whitespace.
@@ -153,7 +153,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
153153
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
154154
xd := export.Decoder(dec)
155155
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
156-
return newInvalidFormatError(dec, t, uo)
156+
return newInvalidFormatError(dec, t)
157157
}
158158
tok, err := dec.ReadToken()
159159
if err != nil {
@@ -190,7 +190,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
190190
return nil
191191
}
192192
}
193-
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
193+
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
194194
}
195195
return &fncs
196196
}
@@ -200,7 +200,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
200200
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
201201
xe := export.Encoder(enc)
202202
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
203-
return newInvalidFormatError(enc, t, mo)
203+
return newInvalidFormatError(enc, t)
204204
}
205205

206206
// Optimize for marshaling without preceding whitespace.
@@ -237,7 +237,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
237237
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
238238
xd := export.Decoder(dec)
239239
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
240-
return newInvalidFormatError(dec, t, uo)
240+
return newInvalidFormatError(dec, t)
241241
}
242242
var flags jsonwire.ValueFlags
243243
val, err := xd.ReadValue(&flags)
@@ -327,7 +327,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
327327
mo.Format = ""
328328
return marshalArray(enc, va, mo)
329329
default:
330-
return newInvalidFormatError(enc, t, mo)
330+
return newInvalidFormatError(enc, t)
331331
}
332332
} else if mo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
333333
return marshalArray(enc, va, mo)
@@ -365,7 +365,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
365365
uo.Format = ""
366366
return unmarshalArray(dec, va, uo)
367367
default:
368-
return newInvalidFormatError(dec, t, uo)
368+
return newInvalidFormatError(dec, t)
369369
}
370370
} else if uo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
371371
return unmarshalArray(dec, va, uo)
@@ -433,7 +433,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
433433
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
434434
xe := export.Encoder(enc)
435435
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
436-
return newInvalidFormatError(enc, t, mo)
436+
return newInvalidFormatError(enc, t)
437437
}
438438

439439
// Optimize for marshaling without preceding whitespace or string escaping.
@@ -454,7 +454,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
454454
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
455455
xd := export.Decoder(dec)
456456
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
457-
return newInvalidFormatError(dec, t, uo)
457+
return newInvalidFormatError(dec, t)
458458
}
459459
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
460460
var flags jsonwire.ValueFlags
@@ -520,7 +520,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
520520
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
521521
xe := export.Encoder(enc)
522522
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
523-
return newInvalidFormatError(enc, t, mo)
523+
return newInvalidFormatError(enc, t)
524524
}
525525

526526
// Optimize for marshaling without preceding whitespace or string escaping.
@@ -541,7 +541,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
541541
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
542542
xd := export.Decoder(dec)
543543
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
544-
return newInvalidFormatError(dec, t, uo)
544+
return newInvalidFormatError(dec, t)
545545
}
546546
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
547547
var flags jsonwire.ValueFlags
@@ -602,7 +602,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
602602
if mo.Format == "nonfinite" {
603603
allowNonFinite = true
604604
} else {
605-
return newInvalidFormatError(enc, t, mo)
605+
return newInvalidFormatError(enc, t)
606606
}
607607
}
608608

@@ -637,7 +637,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
637637
if uo.Format == "nonfinite" {
638638
allowNonFinite = true
639639
} else {
640-
return newInvalidFormatError(dec, t, uo)
640+
return newInvalidFormatError(dec, t)
641641
}
642642
}
643643
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
@@ -737,7 +737,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
737737
emitNull = false
738738
mo.Format = ""
739739
default:
740-
return newInvalidFormatError(enc, t, mo)
740+
return newInvalidFormatError(enc, t)
741741
}
742742
}
743743

@@ -882,7 +882,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
882882
case "emitnull", "emitempty":
883883
uo.Format = "" // only relevant for marshaling
884884
default:
885-
return newInvalidFormatError(dec, t, uo)
885+
return newInvalidFormatError(dec, t)
886886
}
887887
}
888888
tok, err := dec.ReadToken()
@@ -992,7 +992,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
992992
}
993993
return errUnmarshal
994994
}
995-
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
995+
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
996996
}
997997
return &fncs
998998
}
@@ -1037,7 +1037,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
10371037
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
10381038
xe := export.Encoder(enc)
10391039
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
1040-
return newInvalidFormatError(enc, t, mo)
1040+
return newInvalidFormatError(enc, t)
10411041
}
10421042
once.Do(init)
10431043
if errInit != nil && !mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
@@ -1199,7 +1199,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
11991199
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
12001200
xd := export.Decoder(dec)
12011201
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
1202-
return newInvalidFormatError(dec, t, uo)
1202+
return newInvalidFormatError(dec, t)
12031203
}
12041204
tok, err := dec.ReadToken()
12051205
if err != nil {
@@ -1317,7 +1317,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
13171317
}
13181318
return errUnmarshal
13191319
}
1320-
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
1320+
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
13211321
}
13221322
return &fncs
13231323
}
@@ -1414,7 +1414,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
14141414
emitNull = false
14151415
mo.Format = ""
14161416
default:
1417-
return newInvalidFormatError(enc, t, mo)
1417+
return newInvalidFormatError(enc, t)
14181418
}
14191419
}
14201420

@@ -1462,7 +1462,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
14621462
case "emitnull", "emitempty":
14631463
uo.Format = "" // only relevant for marshaling
14641464
default:
1465-
return newInvalidFormatError(dec, t, uo)
1465+
return newInvalidFormatError(dec, t)
14661466
}
14671467
}
14681468

@@ -1518,7 +1518,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
15181518
}
15191519
return errUnmarshal
15201520
}
1521-
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
1521+
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
15221522
}
15231523
return &fncs
15241524
}
@@ -1539,7 +1539,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
15391539
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
15401540
xe := export.Encoder(enc)
15411541
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
1542-
return newInvalidFormatError(enc, t, mo)
1542+
return newInvalidFormatError(enc, t)
15431543
}
15441544
once.Do(init)
15451545
if err := enc.WriteToken(jsontext.BeginArray); err != nil {
@@ -1563,7 +1563,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
15631563
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
15641564
xd := export.Decoder(dec)
15651565
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
1566-
return newInvalidFormatError(dec, t, uo)
1566+
return newInvalidFormatError(dec, t)
15671567
}
15681568
tok, err := dec.ReadToken()
15691569
if err != nil {
@@ -1616,7 +1616,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
16161616
}
16171617
return errUnmarshal
16181618
}
1619-
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
1619+
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
16201620
}
16211621
return &fncs
16221622
}
@@ -1706,7 +1706,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
17061706
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
17071707
xe := export.Encoder(enc)
17081708
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
1709-
return newInvalidFormatError(enc, t, mo)
1709+
return newInvalidFormatError(enc, t)
17101710
}
17111711
if va.IsNil() {
17121712
return enc.WriteToken(jsontext.Null)
@@ -1746,7 +1746,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
17461746
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
17471747
xd := export.Decoder(dec)
17481748
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
1749-
return newInvalidFormatError(dec, t, uo)
1749+
return newInvalidFormatError(dec, t)
17501750
}
17511751
if uo.Flags.Get(jsonflags.MergeWithLegacySemantics) && !va.IsNil() {
17521752
// Legacy merge behavior is difficult to explain.
@@ -1795,7 +1795,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
17951795

17961796
k := dec.PeekKind()
17971797
if !isAnyType(t) {
1798-
return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, internal.ErrNilInterface)
1798+
return newUnmarshalErrorBeforeWithSkipping(dec, t, internal.ErrNilInterface)
17991799
}
18001800
switch k {
18011801
case 'f', 't':

src/encoding/json/v2/arshal_inlined.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func unmarshalInlinedFallbackNext(dec *jsontext.Decoder, va addressableValue, uo
188188
*b = append(*b, ',')
189189
}
190190
} else {
191-
return newUnmarshalErrorAfterWithSkipping(dec, uo, v.Type(), errRawInlinedNotObject)
191+
return newUnmarshalErrorAfterWithSkipping(dec, v.Type(), errRawInlinedNotObject)
192192
}
193193
}
194194
*b = append(*b, quotedName...)

src/encoding/json/v2/arshal_time.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
4848
var m durationArshaler
4949
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
5050
if !m.initFormat(mo.Format) {
51-
return newInvalidFormatError(enc, t, mo)
51+
return newInvalidFormatError(enc, t)
5252
}
5353
} else if mo.Flags.Get(jsonflags.FormatDurationAsNano) {
5454
return marshalNano(enc, va, mo)
@@ -74,13 +74,13 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
7474
var u durationArshaler
7575
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
7676
if !u.initFormat(uo.Format) {
77-
return newInvalidFormatError(dec, t, uo)
77+
return newInvalidFormatError(dec, t)
7878
}
7979
} else if uo.Flags.Get(jsonflags.FormatDurationAsNano) {
8080
return unmarshalNano(dec, va, uo)
8181
} else {
8282
// TODO(https://go.dev/issue/71631): Decide on default duration representation.
83-
return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
83+
return newUnmarshalErrorBeforeWithSkipping(dec, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
8484
}
8585

8686
stringify := !u.isNumeric() || xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
@@ -125,7 +125,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
125125
var m timeArshaler
126126
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
127127
if !m.initFormat(mo.Format) {
128-
return newInvalidFormatError(enc, t, mo)
128+
return newInvalidFormatError(enc, t)
129129
}
130130
}
131131

@@ -148,7 +148,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
148148
var u timeArshaler
149149
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
150150
if !u.initFormat(uo.Format) {
151-
return newInvalidFormatError(dec, t, uo)
151+
return newInvalidFormatError(dec, t)
152152
}
153153
} else if uo.Flags.Get(jsonflags.ParseTimeWithLooseRFC3339) {
154154
u.looseRFC3339 = true

src/encoding/json/v2/errors.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ type SemanticError struct {
8888
}
8989

9090
// coder is implemented by [jsontext.Encoder] or [jsontext.Decoder].
91-
type coder interface{ StackPointer() jsontext.Pointer }
91+
type coder interface {
92+
StackPointer() jsontext.Pointer
93+
Options() Options
94+
}
9295

9396
// newInvalidFormatError wraps err in a SemanticError because
9497
// the current type t cannot handle the provided options format.
@@ -97,13 +100,13 @@ type coder interface{ StackPointer() jsontext.Pointer }
97100
// If [jsonflags.ReportErrorsWithLegacySemantics] is specified,
98101
// then this automatically skips the next value when unmarshaling
99102
// to ensure that the value is fully consumed.
100-
func newInvalidFormatError(c coder, t reflect.Type, o *jsonopts.Struct) error {
101-
err := fmt.Errorf("invalid format flag %q", o.Format)
103+
func newInvalidFormatError(c coder, t reflect.Type) error {
104+
err := fmt.Errorf("invalid format flag %q", c.Options().(*jsonopts.Struct).Format)
102105
switch c := c.(type) {
103106
case *jsontext.Encoder:
104107
err = newMarshalErrorBefore(c, t, err)
105108
case *jsontext.Decoder:
106-
err = newUnmarshalErrorBeforeWithSkipping(c, o, t, err)
109+
err = newUnmarshalErrorBeforeWithSkipping(c, t, err)
107110
}
108111
return err
109112
}
@@ -136,9 +139,9 @@ func newUnmarshalErrorBefore(d *jsontext.Decoder, t reflect.Type, err error) err
136139
// newUnmarshalErrorBeforeWithSkipping is like [newUnmarshalErrorBefore],
137140
// but automatically skips the next value if
138141
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
139-
func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
142+
func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
140143
err = newUnmarshalErrorBefore(d, t, err)
141-
if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
144+
if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
142145
if err2 := export.Decoder(d).SkipValue(); err2 != nil {
143146
return err2
144147
}
@@ -170,9 +173,9 @@ func newUnmarshalErrorAfterWithValue(d *jsontext.Decoder, t reflect.Type, err er
170173
// newUnmarshalErrorAfterWithSkipping is like [newUnmarshalErrorAfter],
171174
// but automatically skips the remainder of the current value if
172175
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
173-
func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
176+
func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
174177
err = newUnmarshalErrorAfter(d, t, err)
175-
if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
178+
if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
176179
if err2 := export.Decoder(d).SkipValueRemainder(); err2 != nil {
177180
return err2
178181
}

0 commit comments

Comments
 (0)