-
Notifications
You must be signed in to change notification settings - Fork 99
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
feat: plain map and slice #464
base: main
Are you sure you want to change the base?
Changes from all commits
28df012
72cd758
df794b9
67bf318
8d09c83
a733118
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,18 @@ import ( | |
func createDecoderOfUnion(d *decoderContext, schema *UnionSchema, typ reflect2.Type) ValDecoder { | ||
switch typ.Kind() { | ||
case reflect.Map: | ||
if typ.(reflect2.MapType).Key().Kind() != reflect.String || | ||
typ.(reflect2.MapType).Elem().Kind() != reflect.Interface { | ||
if typ.(reflect2.MapType).Key().Kind() != reflect.String { | ||
break | ||
} | ||
|
||
if typ.(reflect2.MapType).Elem().Kind() != reflect.Interface { | ||
if !schema.Nullable() { | ||
break | ||
} | ||
Comment on lines
+20
to
+23
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. There is an edge case here being: To deal with this case, IMO the en/decoder should always error towards the map union (existing functionality) and failing that, check the new non-pointer version. This would also need to be documented. 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. @nrwiersma sorry my use case has changed and I no longer need this feature. Sadly I don't have time to fix it at the moment. Feel free to fix it, hand it over to another volunteer or close it. |
||
|
||
return decoderOfNullableUnion(d, schema, typ) | ||
} | ||
|
||
return decoderOfMapUnion(d, schema, typ) | ||
case reflect.Slice: | ||
if !schema.Nullable() { | ||
|
@@ -44,10 +52,18 @@ func createDecoderOfUnion(d *decoderContext, schema *UnionSchema, typ reflect2.T | |
func createEncoderOfUnion(e *encoderContext, schema *UnionSchema, typ reflect2.Type) ValEncoder { | ||
switch typ.Kind() { | ||
case reflect.Map: | ||
if typ.(reflect2.MapType).Key().Kind() != reflect.String || | ||
typ.(reflect2.MapType).Elem().Kind() != reflect.Interface { | ||
if typ.(reflect2.MapType).Key().Kind() != reflect.String { | ||
break | ||
} | ||
|
||
if typ.(reflect2.MapType).Elem().Kind() != reflect.Interface { | ||
if !schema.Nullable() { | ||
break | ||
} | ||
|
||
return encoderOfNullableUnion(e, schema, typ) | ||
} | ||
|
||
return encoderOfMapUnion(e, schema, typ) | ||
case reflect.Slice: | ||
if !schema.Nullable() { | ||
|
@@ -179,6 +195,8 @@ func decoderOfNullableUnion(d *decoderContext, schema Schema, typ reflect2.Type) | |
isPtr = true | ||
case *reflect2.UnsafeSliceType: | ||
baseTyp = v | ||
case *reflect2.UnsafeMapType: | ||
baseTyp = v | ||
} | ||
decoder := decoderOfType(d, union.Types()[typeIdx], baseTyp) | ||
|
||
|
@@ -249,6 +267,8 @@ func encoderOfNullableUnion(e *encoderContext, schema Schema, typ reflect2.Type) | |
isPtr = true | ||
case *reflect2.UnsafeSliceType: | ||
baseTyp = v | ||
case *reflect2.UnsafeMapType: | ||
baseTyp = v | ||
} | ||
encoder := encoderOfType(e, union.Types()[typeIdx], baseTyp) | ||
|
||
|
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.
The decoder will not work for map like it does for slice currently.
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.
Thanks for your feedback, however I'm not sure what you are referring to here. What would need to change in the decoder for this to work?
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.
@nrwiersma I'm not familiar with the whole codec logic yet but I had a look and tried to add support for nullable maps for encoding and decoding. Have a look at my latest commits and let me know what you think.