-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use custom IDOrName type for schemas (#545)
Add typing information to the fields that support passing either an ID as int, an ID as string or a Name as string. An empty value will be marshalled to `null` to reflect the behavior of an empty interface. After this change, we do not have to cast IDs to float64 or to string to be able to read the values. Since this is part of the schema package, this is not a breaking change for our customers, but might break our tests that rely on casting the interface to the desired type.
- Loading branch information
Showing
14 changed files
with
221 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package schema | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// IDOrName can be used in API requests where either a resource id or name can be | ||
// specified. | ||
type IDOrName struct { | ||
ID int64 | ||
Name string | ||
} | ||
|
||
var _ json.Unmarshaler = (*IDOrName)(nil) | ||
var _ json.Marshaler = (*IDOrName)(nil) | ||
|
||
func (o IDOrName) MarshalJSON() ([]byte, error) { | ||
if o.ID != 0 { | ||
return json.Marshal(o.ID) | ||
} | ||
if o.Name != "" { | ||
return json.Marshal(o.Name) | ||
} | ||
|
||
// We want to preserve the behavior of an empty interface{} to prevent breaking | ||
// changes (marshaled to null when empty). | ||
return json.Marshal(nil) | ||
} | ||
|
||
func (o *IDOrName) UnmarshalJSON(data []byte) error { | ||
d := json.NewDecoder(bytes.NewBuffer(data)) | ||
// This ensures we won't lose precision on large IDs, see json.Number below | ||
d.UseNumber() | ||
|
||
var v any | ||
if err := d.Decode(&v); err != nil { | ||
return err | ||
} | ||
|
||
switch typed := v.(type) { | ||
case string: | ||
id, err := strconv.ParseInt(typed, 10, 64) | ||
if err == nil { | ||
o.ID = id | ||
} else if typed != "" { | ||
o.Name = typed | ||
} | ||
case json.Number: | ||
id, err := typed.Int64() | ||
if err != nil { | ||
return &json.UnmarshalTypeError{ | ||
Value: string(data), | ||
Type: reflect.TypeOf(*o), | ||
} | ||
} | ||
o.ID = id | ||
default: | ||
return &json.UnmarshalTypeError{ | ||
Value: string(data), | ||
Type: reflect.TypeOf(*o), | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package schema | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIDOrNameMarshall(t *testing.T) { | ||
t.Run("id", func(t *testing.T) { | ||
i := IDOrName{ID: 1} | ||
|
||
got, err := i.MarshalJSON() | ||
require.NoError(t, err) | ||
require.Equal(t, `1`, string(got)) | ||
}) | ||
|
||
t.Run("name", func(t *testing.T) { | ||
i := IDOrName{Name: "name"} | ||
|
||
got, err := i.MarshalJSON() | ||
require.NoError(t, err) | ||
require.Equal(t, `"name"`, string(got)) | ||
}) | ||
|
||
t.Run("id and name", func(t *testing.T) { | ||
i := IDOrName{ID: 1, Name: "name"} | ||
|
||
got, err := i.MarshalJSON() | ||
require.NoError(t, err) | ||
require.Equal(t, `1`, string(got)) | ||
}) | ||
|
||
t.Run("null", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
got, err := i.MarshalJSON() | ||
require.NoError(t, err) | ||
require.Equal(t, `null`, string(got)) | ||
}) | ||
} | ||
|
||
func TestIDOrNameUnMarshall(t *testing.T) { | ||
t.Run("id", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
err := i.UnmarshalJSON([]byte(`1`)) | ||
require.NoError(t, err) | ||
require.Equal(t, IDOrName{ID: 1}, i) | ||
}) | ||
t.Run("name", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
err := i.UnmarshalJSON([]byte(`"name"`)) | ||
require.NoError(t, err) | ||
require.Equal(t, IDOrName{Name: "name"}, i) | ||
}) | ||
t.Run("id string", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
err := i.UnmarshalJSON([]byte(`"1"`)) | ||
require.NoError(t, err) | ||
require.Equal(t, IDOrName{ID: 1}, i) | ||
}) | ||
t.Run("id float", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
err := i.UnmarshalJSON([]byte(`1.0`)) | ||
require.EqualError(t, err, "json: cannot unmarshal 1.0 into Go value of type schema.IDOrName") | ||
}) | ||
t.Run("null", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
err := i.UnmarshalJSON([]byte(`null`)) | ||
require.EqualError(t, err, "json: cannot unmarshal null into Go value of type schema.IDOrName") | ||
}) | ||
} | ||
|
||
func TestIDOrName(t *testing.T) { | ||
// Make sure the behavior does not change from the use of an interface{}. | ||
type FakeRequest struct { | ||
Old interface{} `json:"old"` | ||
New IDOrName `json:"new"` | ||
} | ||
|
||
t.Run("null", func(t *testing.T) { | ||
o := FakeRequest{} | ||
body, err := json.Marshal(o) | ||
require.NoError(t, err) | ||
require.Equal(t, `{"old":null,"new":null}`, string(body)) | ||
}) | ||
t.Run("id", func(t *testing.T) { | ||
o := FakeRequest{Old: int64(1), New: IDOrName{ID: 1}} | ||
body, err := json.Marshal(o) | ||
require.NoError(t, err) | ||
require.Equal(t, `{"old":1,"new":1}`, string(body)) | ||
}) | ||
t.Run("name", func(t *testing.T) { | ||
o := FakeRequest{Old: "name", New: IDOrName{Name: "name"}} | ||
body, err := json.Marshal(o) | ||
require.NoError(t, err) | ||
require.Equal(t, `{"old":"name","new":"name"}`, string(body)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.