Skip to content

Commit ec4fed3

Browse files
authored
Rename NewMarshalers and NewUnmarshalers using Join prefix (#133)
WARNING: This commit contains breaking changes. Join better matches exactly what the function does and is consistent with the existing JoinOptions function.
1 parent c14f990 commit ec4fed3

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

arshal_funcs.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ var errNonSingularValue = errors.New("must read or write exactly one value")
3535
// There are no exported fields or methods on Marshalers.
3636
type Marshalers = typedMarshalers
3737

38-
// NewMarshalers constructs a flattened list of marshal functions.
38+
// JoinMarshalers constructs a flattened list of marshal functions.
3939
// If multiple functions in the list are applicable for a value of a given type,
4040
// then those earlier in the list take precedence over those that come later.
4141
// If a function returns [SkipFunc], then the next applicable function is called,
4242
// otherwise the default marshaling behavior is used.
4343
//
4444
// For example:
4545
//
46-
// m1 := NewMarshalers(f1, f2)
47-
// m2 := NewMarshalers(f0, m1, f3) // equivalent to m3
48-
// m3 := NewMarshalers(f0, f1, f2, f3) // equivalent to m2
49-
func NewMarshalers(ms ...*Marshalers) *Marshalers {
46+
// m1 := JoinMarshalers(f1, f2)
47+
// m2 := JoinMarshalers(f0, m1, f3) // equivalent to m3
48+
// m3 := JoinMarshalers(f0, f1, f2, f3) // equivalent to m2
49+
func JoinMarshalers(ms ...*Marshalers) *Marshalers {
5050
return newMarshalers(ms...)
5151
}
5252

@@ -57,18 +57,18 @@ func NewMarshalers(ms ...*Marshalers) *Marshalers {
5757
// There are no exported fields or methods on Unmarshalers.
5858
type Unmarshalers = typedUnmarshalers
5959

60-
// NewUnmarshalers constructs a flattened list of unmarshal functions.
60+
// JoinUnmarshalers constructs a flattened list of unmarshal functions.
6161
// If multiple functions in the list are applicable for a value of a given type,
6262
// then those earlier in the list take precedence over those that come later.
6363
// If a function returns [SkipFunc], then the next applicable function is called,
6464
// otherwise the default unmarshaling behavior is used.
6565
//
6666
// For example:
6767
//
68-
// u1 := NewUnmarshalers(f1, f2)
69-
// u2 := NewUnmarshalers(f0, u1, f3) // equivalent to u3
70-
// u3 := NewUnmarshalers(f0, f1, f2, f3) // equivalent to u2
71-
func NewUnmarshalers(us ...*Unmarshalers) *Unmarshalers {
68+
// u1 := JoinUnmarshalers(f1, f2)
69+
// u2 := JoinUnmarshalers(f0, u1, f3) // equivalent to u3
70+
// u3 := JoinUnmarshalers(f0, f1, f2, f3) // equivalent to u2
71+
func JoinUnmarshalers(us ...*Unmarshalers) *Unmarshalers {
7272
return newUnmarshalers(us...)
7373
}
7474

arshal_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,7 @@ func TestMarshal(t *testing.T) {
26612661
}, {
26622662
name: jsontest.Name("Structs/InlinedFallback/MapStringInt/MarshalFunc"),
26632663
opts: []Options{
2664-
WithMarshalers(NewMarshalers(
2664+
WithMarshalers(JoinMarshalers(
26652665
// Marshalers do not affect the string key of inlined maps.
26662666
MarshalFunc(func(v string) ([]byte, error) {
26672667
return []byte(fmt.Sprintf(`"%q"`, strings.ToUpper(v))), nil
@@ -3594,7 +3594,7 @@ func TestMarshal(t *testing.T) {
35943594
}, {
35953595
name: jsontest.Name("Functions/Bool/Empty2/NoMatch"),
35963596
opts: []Options{
3597-
WithMarshalers(NewMarshalers()),
3597+
WithMarshalers(JoinMarshalers()),
35983598
},
35993599
in: true,
36003600
want: `true`,
@@ -3854,7 +3854,7 @@ func TestMarshal(t *testing.T) {
38543854
}, {
38553855
name: jsontest.Name("Funtions/Struct/Fields"),
38563856
opts: []Options{
3857-
WithMarshalers(NewMarshalers(
3857+
WithMarshalers(JoinMarshalers(
38583858
MarshalFunc(func(v bool) ([]byte, error) {
38593859
return []byte(`"called1"`), nil
38603860
}),
@@ -3874,7 +3874,7 @@ func TestMarshal(t *testing.T) {
38743874
}, {
38753875
name: jsontest.Name("Functions/Struct/OmitEmpty"),
38763876
opts: []Options{
3877-
WithMarshalers(NewMarshalers(
3877+
WithMarshalers(JoinMarshalers(
38783878
MarshalFunc(func(v bool) ([]byte, error) {
38793879
return []byte(`null`), nil
38803880
}),
@@ -3906,7 +3906,7 @@ func TestMarshal(t *testing.T) {
39063906
}, {
39073907
name: jsontest.Name("Functions/Struct/OmitZero"),
39083908
opts: []Options{
3909-
WithMarshalers(NewMarshalers(
3909+
WithMarshalers(JoinMarshalers(
39103910
MarshalFunc(func(v bool) ([]byte, error) {
39113911
panic("should not be called")
39123912
}),
@@ -3926,7 +3926,7 @@ func TestMarshal(t *testing.T) {
39263926
}, {
39273927
name: jsontest.Name("Functions/Struct/Inlined"),
39283928
opts: []Options{
3929-
WithMarshalers(NewMarshalers(
3929+
WithMarshalers(JoinMarshalers(
39303930
MarshalFunc(func(v structInlinedL1) ([]byte, error) {
39313931
panic("should not be called")
39323932
}),
@@ -4219,7 +4219,7 @@ func TestMarshal(t *testing.T) {
42194219
return checkLast()
42204220
})
42214221

4222-
return NewMarshalers(
4222+
return JoinMarshalers(
42234223
anyMarshaler,
42244224
pointerAnyMarshaler,
42254225
namedAnyMarshaler,
@@ -4237,7 +4237,7 @@ func TestMarshal(t *testing.T) {
42374237
}, {
42384238
name: jsontest.Name("Functions/Precedence/V1First"),
42394239
opts: []Options{
4240-
WithMarshalers(NewMarshalers(
4240+
WithMarshalers(JoinMarshalers(
42414241
MarshalFunc(func(bool) ([]byte, error) {
42424242
return []byte(`"called"`), nil
42434243
}),
@@ -4251,7 +4251,7 @@ func TestMarshal(t *testing.T) {
42514251
}, {
42524252
name: jsontest.Name("Functions/Precedence/V2First"),
42534253
opts: []Options{
4254-
WithMarshalers(NewMarshalers(
4254+
WithMarshalers(JoinMarshalers(
42554255
MarshalToFunc(func(enc *jsontext.Encoder, v bool, opts Options) error {
42564256
return enc.WriteToken(jsontext.String("called"))
42574257
}),
@@ -4265,7 +4265,7 @@ func TestMarshal(t *testing.T) {
42654265
}, {
42664266
name: jsontest.Name("Functions/Precedence/V2Skipped"),
42674267
opts: []Options{
4268-
WithMarshalers(NewMarshalers(
4268+
WithMarshalers(JoinMarshalers(
42694269
MarshalToFunc(func(enc *jsontext.Encoder, v bool, opts Options) error {
42704270
return SkipFunc
42714271
}),
@@ -4279,8 +4279,8 @@ func TestMarshal(t *testing.T) {
42794279
}, {
42804280
name: jsontest.Name("Functions/Precedence/NestedFirst"),
42814281
opts: []Options{
4282-
WithMarshalers(NewMarshalers(
4283-
NewMarshalers(
4282+
WithMarshalers(JoinMarshalers(
4283+
JoinMarshalers(
42844284
MarshalFunc(func(bool) ([]byte, error) {
42854285
return []byte(`"called"`), nil
42864286
}),
@@ -4295,11 +4295,11 @@ func TestMarshal(t *testing.T) {
42954295
}, {
42964296
name: jsontest.Name("Functions/Precedence/NestedLast"),
42974297
opts: []Options{
4298-
WithMarshalers(NewMarshalers(
4298+
WithMarshalers(JoinMarshalers(
42994299
MarshalFunc(func(bool) ([]byte, error) {
43004300
return []byte(`"called"`), nil
43014301
}),
4302-
NewMarshalers(
4302+
JoinMarshalers(
43034303
MarshalFunc(func(bool) ([]byte, error) {
43044304
panic("should not be called")
43054305
}),
@@ -7972,7 +7972,7 @@ func TestUnmarshal(t *testing.T) {
79727972
}, {
79737973
name: jsontest.Name("Functions/String/Empty2/NoMatch"),
79747974
opts: []Options{
7975-
WithUnmarshalers(NewUnmarshalers()),
7975+
WithUnmarshalers(JoinUnmarshalers()),
79767976
},
79777977
inBuf: `""`,
79787978
inVal: addr(""),
@@ -8216,7 +8216,7 @@ func TestUnmarshal(t *testing.T) {
82168216
}, {
82178217
name: jsontest.Name("Funtions/Struct/Fields"),
82188218
opts: []Options{
8219-
WithUnmarshalers(NewUnmarshalers(
8219+
WithUnmarshalers(JoinUnmarshalers(
82208220
UnmarshalFunc(func(b []byte, v *bool) error {
82218221
if string(b) != `"called1"` {
82228222
return fmt.Errorf("got %s, want %s", b, `"called1"`)
@@ -8259,7 +8259,7 @@ func TestUnmarshal(t *testing.T) {
82598259
}, {
82608260
name: jsontest.Name("Functions/Struct/Inlined"),
82618261
opts: []Options{
8262-
WithUnmarshalers(NewUnmarshalers(
8262+
WithUnmarshalers(JoinUnmarshalers(
82638263
UnmarshalFunc(func([]byte, *structInlinedL1) error {
82648264
panic("should not be called")
82658265
}),
@@ -8370,7 +8370,7 @@ func TestUnmarshal(t *testing.T) {
83708370
}, {
83718371
name: jsontest.Name("Functions/Interface/NilPointerNetIP/Override"),
83728372
opts: []Options{
8373-
WithUnmarshalers(NewUnmarshalers(
8373+
WithUnmarshalers(JoinUnmarshalers(
83748374
UnmarshalFromFunc(func(dec *jsontext.Decoder, v *fmt.Stringer, opts Options) error {
83758375
*v = (*net.IP)(nil)
83768376
return SkipFunc
@@ -8578,7 +8578,7 @@ func TestUnmarshal(t *testing.T) {
85788578
return checkLast()
85798579
})
85808580

8581-
return NewUnmarshalers(
8581+
return JoinUnmarshalers(
85828582
// This is just like unmarshaling into a Go array,
85838583
// but avoids zeroing the element before calling unmarshal.
85848584
UnmarshalFromFunc(func(dec *jsontext.Decoder, v *[14]any, opts Options) error {
@@ -8611,7 +8611,7 @@ func TestUnmarshal(t *testing.T) {
86118611
}, {
86128612
name: jsontest.Name("Functions/Precedence/V1First"),
86138613
opts: []Options{
8614-
WithUnmarshalers(NewUnmarshalers(
8614+
WithUnmarshalers(JoinUnmarshalers(
86158615
UnmarshalFunc(func(b []byte, v *string) error {
86168616
if string(b) != `"called"` {
86178617
return fmt.Errorf("got %s, want %s", b, `"called"`)
@@ -8630,7 +8630,7 @@ func TestUnmarshal(t *testing.T) {
86308630
}, {
86318631
name: jsontest.Name("Functions/Precedence/V2First"),
86328632
opts: []Options{
8633-
WithUnmarshalers(NewUnmarshalers(
8633+
WithUnmarshalers(JoinUnmarshalers(
86348634
UnmarshalFromFunc(func(dec *jsontext.Decoder, v *string, opts Options) error {
86358635
switch t, err := dec.ReadToken(); {
86368636
case err != nil:
@@ -8652,7 +8652,7 @@ func TestUnmarshal(t *testing.T) {
86528652
}, {
86538653
name: jsontest.Name("Functions/Precedence/V2Skipped"),
86548654
opts: []Options{
8655-
WithUnmarshalers(NewUnmarshalers(
8655+
WithUnmarshalers(JoinUnmarshalers(
86568656
UnmarshalFromFunc(func(dec *jsontext.Decoder, v *string, opts Options) error {
86578657
return SkipFunc
86588658
}),
@@ -8671,8 +8671,8 @@ func TestUnmarshal(t *testing.T) {
86718671
}, {
86728672
name: jsontest.Name("Functions/Precedence/NestedFirst"),
86738673
opts: []Options{
8674-
WithUnmarshalers(NewUnmarshalers(
8675-
NewUnmarshalers(
8674+
WithUnmarshalers(JoinUnmarshalers(
8675+
JoinUnmarshalers(
86768676
UnmarshalFunc(func(b []byte, v *string) error {
86778677
if string(b) != `"called"` {
86788678
return fmt.Errorf("got %s, want %s", b, `"called"`)
@@ -8692,15 +8692,15 @@ func TestUnmarshal(t *testing.T) {
86928692
}, {
86938693
name: jsontest.Name("Functions/Precedence/NestedLast"),
86948694
opts: []Options{
8695-
WithUnmarshalers(NewUnmarshalers(
8695+
WithUnmarshalers(JoinUnmarshalers(
86968696
UnmarshalFunc(func(b []byte, v *string) error {
86978697
if string(b) != `"called"` {
86988698
return fmt.Errorf("got %s, want %s", b, `"called"`)
86998699
}
87008700
*v = "called"
87018701
return nil
87028702
}),
8703-
NewUnmarshalers(
8703+
JoinUnmarshalers(
87048704
UnmarshalFunc(func([]byte, *string) error {
87058705
panic("should not be called")
87068706
}),

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func ExampleWithMarshalers_errors() {
560560

561561
b, err := json.Marshal(&response,
562562
// Intercept every attempt to marshal an error type.
563-
json.WithMarshalers(json.NewMarshalers(
563+
json.WithMarshalers(json.JoinMarshalers(
564564
// Suppose we consider strconv.NumError to be a safe to serialize:
565565
// this type-specific marshal function intercepts this type
566566
// and encodes the error message as a JSON string.

migrate.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ echo "pkg encoding/json/v2, func MarshalFunc[\$0 interface{}](func(\$0) ([]uint8
178178
echo "pkg encoding/json/v2, func MarshalToFunc[\$0 interface{}](func(*jsontext.Encoder, \$0, jsonopts.Options) error) *typedArshalers[jsontext.Encoder] #$ISSUE" >> $GOROOT/api/$FILE
179179
echo "pkg encoding/json/v2, func MarshalWrite(io.Writer, interface{}, ...jsonopts.Options) error #$ISSUE" >> $GOROOT/api/$FILE
180180
echo "pkg encoding/json/v2, func MatchCaseInsensitiveNames(bool) jsonopts.Options #$ISSUE" >> $GOROOT/api/$FILE
181-
echo "pkg encoding/json/v2, func NewMarshalers(...*typedArshalers[jsontext.Encoder]) *typedArshalers[jsontext.Encoder] #$ISSUE" >> $GOROOT/api/$FILE
182-
echo "pkg encoding/json/v2, func NewUnmarshalers(...*typedArshalers[jsontext.Decoder]) *typedArshalers[jsontext.Decoder] #$ISSUE" >> $GOROOT/api/$FILE
181+
echo "pkg encoding/json/v2, func JoinMarshalers(...*typedArshalers[jsontext.Encoder]) *typedArshalers[jsontext.Encoder] #$ISSUE" >> $GOROOT/api/$FILE
182+
echo "pkg encoding/json/v2, func JoinUnmarshalers(...*typedArshalers[jsontext.Decoder]) *typedArshalers[jsontext.Decoder] #$ISSUE" >> $GOROOT/api/$FILE
183183
echo "pkg encoding/json/v2, func OmitZeroStructFields(bool) jsonopts.Options #$ISSUE" >> $GOROOT/api/$FILE
184184
echo "pkg encoding/json/v2, func RejectUnknownMembers(bool) jsonopts.Options #$ISSUE" >> $GOROOT/api/$FILE
185185
echo "pkg encoding/json/v2, func StringifyNumbers(bool) jsonopts.Options #$ISSUE" >> $GOROOT/api/$FILE

0 commit comments

Comments
 (0)