diff --git a/pkg/codec/wire.go b/pkg/codec/wire.go index c71f59b4c3c57eba0b5ce23b7cdad2a66c6c7d6d..194ebe556c493f3c0ed3a3c96a49a7eb3b854dd1 100644 --- a/pkg/codec/wire.go +++ b/pkg/codec/wire.go @@ -118,9 +118,6 @@ func (id ID) MarshalJSON() ([]byte, error) { // UnmarshalJSON implements json.Unmarshaler. func (id *ID) UnmarshalJSON(data []byte) error { - if len(data) == 0 { - return nil - } *id = data // now validate if id.IsNull() { diff --git a/pkg/codec/wire_test.go b/pkg/codec/wire_test.go index 791f11c3f4ac6b71602d0880392f35b52a6803f2..7a4ee3272c31d19635dc8f757f0bd98ab4ed57e9 100644 --- a/pkg/codec/wire_test.go +++ b/pkg/codec/wire_test.go @@ -24,7 +24,7 @@ func TestVersion(t *testing.T) { }) } -func TestID(t *testing.T) { +func TestIDMarshal(t *testing.T) { var v ID @@ -54,3 +54,37 @@ func TestID(t *testing.T) { assert.Equal(t, `null`, string(ans)) }) } + +func TestIDUnmarshal(t *testing.T) { + + var v ID + + t.Run("number", func(t *testing.T) { + err := json.Unmarshal([]byte("2"), &v) + assert.NoError(t, err) + assert.Equal(t, 2, v.Number()) + }) + + t.Run("numberstring", func(t *testing.T) { + err := json.Unmarshal([]byte(`"2"`), &v) + assert.NoError(t, err) + assert.Equal(t, `"2"`, string(v.RawMessage())) + }) + t.Run("string", func(t *testing.T) { + err := json.Unmarshal([]byte(`"doggo"`), &v) + assert.NoError(t, err) + assert.Equal(t, `"doggo"`, string(v.RawMessage())) + }) + t.Run("null", func(t *testing.T) { + err := json.Unmarshal([]byte(`null`), &v) + assert.NoError(t, err) + assert.True(t, v.IsNull()) + }) + t.Run("error", func(t *testing.T) { + err := json.Unmarshal([]byte(`%%%%`), &v) + assert.Error(t, err) + + err = json.Unmarshal([]byte(`1%%%%4`), &v) + assert.Error(t, err) + }) +}