-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This applies to: - ID - Attributes - Relationships Custom types need to be registered before usage, using the new `jsonapi.RegisterType()` function which takes 3 arguments: - the type to support (`reflect.Type`) - the function to use when marshalling a response - the function to use when unmarshalling a response Example: ```` RegisterType(uuidType, func(value interface{}) (string, error) { result := value.(*UUID).String() return result, nil }, func(value string) (interface{}, error) { return UUIDFromString(value) }) ```` The custom type will be represented as a `string` in the JSON document in the requests and responses. Fixes #114 Signed-off-by: Xavier Coulon <[email protected]>
- Loading branch information
Showing
7 changed files
with
424 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jsonapi | ||
|
||
import "reflect" | ||
|
||
type MarshallingFunc func(interface{}) (string, error) | ||
type UnmarshallingFunc func(string) (interface{}, error) | ||
|
||
// map of functions to use to convert the field value into a JSON string | ||
var customTypeMarshallingFuncs map[reflect.Type]MarshallingFunc | ||
|
||
// map of functions to use to convert the JSON string value into the target field | ||
var customTypeUnmarshallingFuncs map[reflect.Type]UnmarshallingFunc | ||
|
||
// init initializes the maps | ||
func init() { | ||
customTypeMarshallingFuncs = make(map[reflect.Type]MarshallingFunc, 0) | ||
customTypeUnmarshallingFuncs = make(map[reflect.Type]UnmarshallingFunc, 0) | ||
} | ||
|
||
// IsRegisteredType checks if the given type `t` is registered as a custom type | ||
func IsRegisteredType(t reflect.Type) bool { | ||
_, ok := customTypeMarshallingFuncs[t] | ||
return ok | ||
} | ||
|
||
// RegisterType registers the functions to convert the field from a custom type to a string and vice-versa | ||
// in the JSON requests/responses. | ||
// The `marshallingFunc` must be a function that returns a string (along with an error if something wrong happened) | ||
// and the `unmarshallingFunc` must be a function that takes | ||
// a string as its sole argument and return an instance of `typeName` (along with an error if something wrong happened). | ||
// Eg: `uuid.FromString(string) uuid.UUID {...} and `uuid.String() string {...} | ||
func RegisterType(customType reflect.Type, marshallingFunc MarshallingFunc, unmarshallingFunc UnmarshallingFunc) { | ||
// register the pointer to the type | ||
customTypeMarshallingFuncs[customType] = marshallingFunc | ||
customTypeUnmarshallingFuncs[customType] = unmarshallingFunc | ||
} | ||
|
||
// resetCustomTypeRegistrations resets the custom type registration, which is useful during testing | ||
func resetCustomTypeRegistrations() { | ||
customTypeMarshallingFuncs = make(map[reflect.Type]MarshallingFunc, 0) | ||
customTypeUnmarshallingFuncs = make(map[reflect.Type]UnmarshallingFunc, 0) | ||
} |
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,25 @@ | ||
package jsonapi | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRegisterCustomTypes(t *testing.T) { | ||
for _, uuidType := range []reflect.Type{reflect.TypeOf(UUID{}), reflect.TypeOf(&UUID{})} { | ||
// given | ||
resetCustomTypeRegistrations() // make sure no other registration interferes with this test | ||
// when | ||
RegisterType(uuidType, | ||
func(value interface{}) (string, error) { | ||
return "", nil | ||
}, | ||
func(value string) (interface{}, error) { | ||
return nil, nil | ||
}) | ||
// then | ||
if !IsRegisteredType(uuidType) { | ||
t.Fatalf("Expected `%v` to be registered but it was not", uuidType) | ||
} | ||
} | ||
} |
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.