-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support custom types for IDs and attributes #114
Comments
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 google#114 Signed-off-by: Xavier Coulon <[email protected]>
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 google#114 Signed-off-by: Xavier Coulon <[email protected]>
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 google#114 Signed-off-by: Xavier Coulon <[email protected]>
@aren55555, may I ask if could you give a look at my PR (#115) and let me know if it's something you'd be interested in including in the library, please ? |
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 google#114 Signed-off-by: Xavier Coulon <[email protected]>
Why is it necessary to have developers register the type? Perhaps any types that implement |
I'll elaborate on what @tprost mentioned: an interface based approach (similar to what Go's standard Granted for the UUID type @xcoulon mentioned, you'd need to embed this in your own custom type and implement the interface, or ask the package maintainer to support implementing the interface. However I think it's a bit farfetched to expect every package's custom types to support the interfaces you desire. Based on the Godoc it appears the |
The library only supports
string
,int(8,16,32,64)
oruint(8,16,32,64)
forID
and other non-complex struct attributes, but it would be nice to have a mean to support other types, such as UUIDs, although they require the use of third-party libraries such as https://github.com/satori/go.uuid. This would save the developers to have to convert fromstring
toUUID
in the their code.Since it's not possible to support all third party libraries in https://github.com/google/jsonapi, a solution could be to provide a public function to register custom types that can be marshalled to/unmarshalled from a
string
.The text was updated successfully, but these errors were encountered: