Skip to content

Commit

Permalink
fix(tm2): rename methods to avoid conflicts with (un)marshaler interf…
Browse files Browse the repository at this point in the history
…aces

amino codec.MarshallJSON has the same method name than Go standard library
`json.Marshaler` interface but with a different signature. This is
rejected by `go vet`. The same applies for codec.UnmarshallJSON vs
`json.Unmarshaler`. To fix that, we rename codec.MarshalJSON to
codec.JSONMarshal and codec.UnmarshalJSON to codec.JSONUnmarshal.

Now `go vet ./...' pass on the full mono-repo.

Fixes gnolang#2954.
  • Loading branch information
mvertes committed Oct 22, 2024
1 parent 5c876f3 commit abcec80
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
14 changes: 7 additions & 7 deletions tm2/pkg/amino/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func UnmarshalAnySized(bz []byte, ptr interface{}) error {
}

func MarshalJSON(o interface{}) ([]byte, error) {
return gcdc.MarshalJSON(o)
return gcdc.JSONMarshal(o)
}

func MarshalJSONAny(o interface{}) ([]byte, error) {
Expand All @@ -146,7 +146,7 @@ func MustMarshalJSONAny(o interface{}) []byte {
}

func UnmarshalJSON(bz []byte, ptr interface{}) error {
return gcdc.UnmarshalJSON(bz, ptr)
return gcdc.JSONUnmarshal(bz, ptr)
}

func MustUnmarshalJSON(bz []byte, ptr interface{}) {
Expand Down Expand Up @@ -756,7 +756,7 @@ func (cdc *Codec) MustUnmarshalAny(bz []byte, ptr interface{}) {
return
}

func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error) {
func (cdc *Codec) JSONMarshal(o interface{}) ([]byte, error) {
cdc.doAutoseal()

rv := reflect.ValueOf(o)
Expand Down Expand Up @@ -814,7 +814,7 @@ func (cdc *Codec) MarshalJSONAny(o interface{}) ([]byte, error) {

// MustMarshalJSON panics if an error occurs. Besides tha behaves exactly like MarshalJSON.
func (cdc *Codec) MustMarshalJSON(o interface{}) []byte {
bz, err := cdc.MarshalJSON(o)
bz, err := cdc.JSONMarshal(o)
if err != nil {
panic(err)
}
Expand All @@ -830,7 +830,7 @@ func (cdc *Codec) MustMarshalJSONAny(o interface{}) []byte {
return bz
}

func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {
func (cdc *Codec) JSONUnmarshal(bz []byte, ptr interface{}) error {
cdc.doAutoseal()
if len(bz) == 0 {
return errors.New("cannot decode empty bytes")
Expand All @@ -851,15 +851,15 @@ func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {

// MustUnmarshalJSON panics if an error occurs. Besides tha behaves exactly like UnmarshalJSON.
func (cdc *Codec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
if err := cdc.UnmarshalJSON(bz, ptr); err != nil {
if err := cdc.JSONUnmarshal(bz, ptr); err != nil {
panic(err)
}
}

// MarshalJSONIndent calls json.Indent on the output of cdc.MarshalJSON
// using the given prefix and indent string.
func (cdc *Codec) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
bz, err := cdc.MarshalJSON(o)
bz, err := cdc.JSONMarshal(o)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/amino/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func _benchmarkBinary(b *testing.B, cdc *amino.Codec, rt reflect.Type, codecType
case "binary":
bz, err = cdc.Marshal(ptr)
case "json":
bz, err = cdc.MarshalJSON(ptr)
bz, err = cdc.JSONMarshal(ptr)
case "binary_pb":
bz, err = pbcdc.Marshal(ptr)
case "binary_pb_translate_only":
Expand All @@ -129,7 +129,7 @@ func _benchmarkBinary(b *testing.B, cdc *amino.Codec, rt reflect.Type, codecType
case "binary":
err = cdc.Unmarshal(bz, ptr2)
case "json":
err = cdc.UnmarshalJSON(bz, ptr2)
err = cdc.JSONUnmarshal(bz, ptr2)
case "binary_pb":
err = pbcdc.Unmarshal(bz, ptr2)
case "binary_pb_translate_only":
Expand Down
52 changes: 26 additions & 26 deletions tm2/pkg/amino/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestMarshalJSON(t *testing.T) {

for i, tt := range cases {
t.Logf("Trying case #%v", i)
blob, err := cdc.MarshalJSON(tt.in)
blob, err := cdc.JSONMarshal(tt.in)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("#%d:\ngot:\n\t%v\nwant non-nil error containing\n\t%q", i,
Expand Down Expand Up @@ -151,11 +151,11 @@ func TestMarshalJSONTime(t *testing.T) {
Time: time.Now().Round(0).UTC(), // strip monotonic.
}

b, err := cdc.MarshalJSON(s)
b, err := cdc.JSONMarshal(s)
assert.Nil(t, err)

var s2 SimpleStruct
err = cdc.UnmarshalJSON(b, &s2)
err = cdc.JSONUnmarshal(b, &s2)
assert.Nil(t, err)
assert.Equal(t, s, s2)
}
Expand All @@ -176,11 +176,11 @@ func TestMarshalJSONPBTime(t *testing.T) {
Duration: &durationpb.Duration{Seconds: 100},
}

b, err := cdc.MarshalJSON(s)
b, err := cdc.JSONMarshal(s)
assert.Nil(t, err)

var s2 SimpleStruct
err = cdc.UnmarshalJSON(b, &s2)
err = cdc.JSONUnmarshal(b, &s2)
assert.Nil(t, err)
assert.Equal(t, s, s2)
}
Expand Down Expand Up @@ -217,15 +217,15 @@ func TestUnmarshalMap(t *testing.T) {
obj := new(map[string]int)
cdc := amino.NewCodec()
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, &obj)
err := cdc.JSONUnmarshal(jsonBytes, &obj)
assert.Fail(t, "should have panicked but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, obj)
err := cdc.JSONUnmarshal(jsonBytes, obj)
assert.Fail(t, "should have panicked but got err: %v", err)
})
assert.Panics(t, func() {
bz, err := cdc.MarshalJSON(obj)
bz, err := cdc.JSONMarshal(obj)
assert.Fail(t, "should have panicked but got bz: %X err: %v", bz, err)
})
}
Expand All @@ -237,22 +237,22 @@ func TestUnmarshalFunc(t *testing.T) {
obj := func() {}
cdc := amino.NewCodec()
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, &obj)
err := cdc.JSONUnmarshal(jsonBytes, &obj)
assert.Fail(t, "should have panicked but got err: %v", err)
})

err := cdc.UnmarshalJSON(jsonBytes, obj)
// UnmarshalJSON expects a pointer
err := cdc.JSONUnmarshal(jsonBytes, obj)
// JSONUnmarshal expects a pointer
assert.Error(t, err)

// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalJSON(obj)
bz, err := cdc.JSONMarshal(obj)
assert.Fail(t, "should have panicked but got bz: %X err: %v", bz, err)
})
}

func TestUnmarshalJSON(t *testing.T) {
func TestJSONUnmarshal(t *testing.T) {
t.Parallel()

cdc := amino.NewCodec()
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestUnmarshalJSON(t *testing.T) {
}

for i, tt := range cases {
err := cdc.UnmarshalJSON([]byte(tt.blob), tt.in)
err := cdc.JSONUnmarshal([]byte(tt.blob), tt.in)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("#%d:\ngot:\n\t%q\nwant non-nil error containing\n\t%q", i,
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestJSONCodecRoundTrip(t *testing.T) {
}

for i, tt := range cases {
mBlob, err := cdc.MarshalJSON(tt.in)
mBlob, err := cdc.JSONMarshal(tt.in)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("#%d:\ngot:\n\t%q\nwant non-nil error containing\n\t%q", i,
Expand All @@ -400,27 +400,27 @@ func TestJSONCodecRoundTrip(t *testing.T) {
}

if err != nil {
t.Errorf("#%d: unexpected error after MarshalJSON: %v", i, err)
t.Errorf("#%d: unexpected error after JSONMarshal: %v", i, err)
continue
}

if err = cdc.UnmarshalJSON(mBlob, tt.out); err != nil {
t.Errorf("#%d: unexpected error after UnmarshalJSON: %v\nmBlob: %s", i, err, mBlob)
if err = cdc.JSONUnmarshal(mBlob, tt.out); err != nil {
t.Errorf("#%d: unexpected error after JSONUnmarshal: %v\nmBlob: %s", i, err, mBlob)
continue
}

// Now check that the input is exactly equal to the output
uBlob, err := cdc.MarshalJSON(tt.out)
uBlob, err := cdc.JSONMarshal(tt.out)
assert.NoError(t, err)
if err := cdc.UnmarshalJSON(mBlob, tt.out); err != nil {
t.Errorf("#%d: unexpected error after second MarshalJSON: %v", i, err)
if err := cdc.JSONUnmarshal(mBlob, tt.out); err != nil {
t.Errorf("#%d: unexpected error after second JSONMarshal: %v", i, err)
continue
}
if !reflect.DeepEqual(tt.want, tt.out) {
t.Errorf("#%d: After roundtrip UnmarshalJSON\ngot: \t%v\nwant:\t%v", i, tt.out, tt.want)
t.Errorf("#%d: After roundtrip JSONUnmarshal\ngot: \t%v\nwant:\t%v", i, tt.out, tt.want)
}
if !bytes.Equal(mBlob, uBlob) {
t.Errorf("#%d: After roundtrip MarshalJSON\ngot: \t%s\nwant:\t%s", i, uBlob, mBlob)
t.Errorf("#%d: After roundtrip JSONMarshal\ngot: \t%s\nwant:\t%s", i, uBlob, mBlob)
}
}
}
Expand Down Expand Up @@ -526,10 +526,10 @@ func TestAminoJSONTimeEncodeDecodeRoundTrip(t *testing.T) {
din := time.Date(2008, 9, 15, 14, 13, 12, 11109876, loc).Round(time.Millisecond).UTC()

cdc := amino.NewCodec()
blobAmino, err := cdc.MarshalJSON(din)
require.Nil(t, err, "amino.Codec.MarshalJSON should succeed")
blobAmino, err := cdc.JSONMarshal(din)
require.Nil(t, err, "amino.Codec.JSONMarshal should succeed")
var tAminoOut time.Time
require.Nil(t, cdc.UnmarshalJSON(blobAmino, &tAminoOut), "amino.Codec.UnmarshalJSON should succeed")
require.Nil(t, cdc.JSONUnmarshal(blobAmino, &tAminoOut), "amino.Codec.JSONUnmarshal should succeed")
require.NotEqual(t, tAminoOut, time.Time{}, "amino.marshaled definitely isn't equal to zero time")
require.Equal(t, tAminoOut, din, "expecting marshaled in to be equal to marshaled out")

Expand Down
6 changes: 3 additions & 3 deletions tm2/pkg/amino/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func _testCodec(t *testing.T, rt reflect.Type, codecType string) {
case "binary":
bz, err = cdc.Marshal(ptr)
case "json":
bz, err = cdc.MarshalJSON(ptr)
bz, err = cdc.JSONMarshal(ptr)
default:
panic("should not happen")
}
Expand All @@ -133,7 +133,7 @@ func _testCodec(t *testing.T, rt reflect.Type, codecType string) {
case "binary":
err = cdc.Unmarshal(bz, ptr2)
case "json":
err = cdc.UnmarshalJSON(bz, ptr2)
err = cdc.JSONUnmarshal(bz, ptr2)
default:
panic("should not happen")
}
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestCodecJSONRoundtripNonNilRegisteredTypeDef(t *testing.T) {
"ConcreteTypeDef incorrectly serialized")

var i1 tests.Interface1
err = cdc.UnmarshalJSON(bz, &i1)
err = cdc.JSONUnmarshal(bz, &i1)
assert.Nil(t, err)
assert.Equal(t, c3, i1)
}
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/amino/repr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func TestMarshalAminoJSON(t *testing.T) {
c: []*Foo{nil, nil, nil},
D: "J",
}
bz, err := cdc.MarshalJSON(f)
bz, err := cdc.JSONMarshal(f)
assert.Nil(t, err)

t.Logf("bz %X", bz)

var f2 Foo
err = cdc.UnmarshalJSON(bz, &f2)
err = cdc.JSONUnmarshal(bz, &f2)
assert.Nil(t, err)

assert.Equal(t, f, f2)
Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/amino/tests/fuzz/json/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func main() {
bz := []byte("TODO")
cdc := amino.NewCodec()
cst := tests.ComplexSt{}
err := cdc.UnmarshalJSON(bz, &cst)
err := cdc.JSONUnmarshal(bz, &cst)
fmt.Printf("Expected a panic but did not. (err: %v)", err)
}
2 changes: 1 addition & 1 deletion tm2/pkg/amino/tests/fuzz/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func Fuzz(data []byte) int {
cdc := amino.NewCodec()
cst := tests.ComplexSt{}
err := cdc.UnmarshalJSON(data, &cst)
err := cdc.JSONUnmarshal(data, &cst)
if err != nil {
return 0
}
Expand Down

0 comments on commit abcec80

Please sign in to comment.