-
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.
refactor: use custom IDOrName type for schemas
- Loading branch information
Showing
14 changed files
with
181 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,67 @@ | ||
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) | ||
} | ||
return nil, &json.UnsupportedValueError{ | ||
Value: reflect.ValueOf(o), | ||
Str: "id or name must not be zero values", | ||
} | ||
} | ||
|
||
func (o *IDOrName) UnmarshalJSON(data []byte) error { | ||
d := json.NewDecoder(bytes.NewBuffer(data)) | ||
d.UseNumber() // This ensures we won't lose precision on large IDs | ||
|
||
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: "number", | ||
Type: reflect.TypeOf(*o), | ||
} | ||
} | ||
o.ID = id | ||
default: | ||
return &json.UnmarshalTypeError{ | ||
Value: reflect.TypeOf(typed).Name(), | ||
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,66 @@ | ||
package schema | ||
|
||
import ( | ||
"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("none", func(t *testing.T) { | ||
i := IDOrName{} | ||
|
||
_, err := i.MarshalJSON() | ||
require.EqualError(t, err, "json: unsupported value: id or name must not be zero values") | ||
}) | ||
} | ||
|
||
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) | ||
}) | ||
} |
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.