-
Notifications
You must be signed in to change notification settings - Fork 214
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 (#114) #115
Open
xcoulon
wants to merge
1
commit into
google:master
Choose a base branch
from
xcoulon:custom_types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use init(), don't use make, use var in block, e.g. var (
customTypeMarshallingFuncs = map[reflect.Type]MarshallingFunc{}
cutomTypeUnmarshallingFuncs = map[reflect.Type]UnmarshallingFunc{}
) |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow pattern of
net/http
'shttp.Handle
andhttp.HandleFunc
, and don't rely on input/output being string e.g.Then you can have
RegisterCustomType(in, out reflect.Type, marshaller TypeMarshaller, unmarshaller TypeUnmarshaller) and
RegisterCustomTypeFunc(in, out reflect.Type, marshallerFunc TypeMarshallerFunc, unmarshallerFunc TypeUnmarshallerFunc)`