Conversation
| RFC3339Time NullableAttr[time.Time] `jsonapi:"attr,rfc3339_time,rfc3339,omitempty"` | ||
| ISO8601Time NullableAttr[time.Time] `jsonapi:"attr,iso8601_time,iso8601,omitempty"` | ||
| Bool NullableAttr[bool] `jsonapi:"attr,bool,omitempty"` | ||
| NullableComment NullableRelationship[*Comment] `jsonapi:"relation,nullable_comment,omitempty"` |
There was a problem hiding this comment.
This is the intended usage.
By setting the NullableRelationship to an explicit value it will serialize that value, by setting the NullableRelationship to an explicit null will serialize a null value which is intended to clear the relationship.
| // unwind that here | ||
| if strings.HasPrefix(fieldType.Type.Name(), "NullableRelationship[") { | ||
| if m.Kind() == reflect.Ptr { | ||
| m = reflect.New(fieldValue.Type().Elem().Elem()) |
There was a problem hiding this comment.
This is the grossest part of the code. It feels wrong to "just unwrap one more pointer" but it does work as expected.
| }, | ||
| }, | ||
| { | ||
| desc: "nullable_comment_not_null", |
There was a problem hiding this comment.
If we were to want to support de-serializing slice types we would implement tests for that here.
As it's not included in the PR those sorts of tests are omitted here.
request.go
Outdated
| // this indicates disassociating the relationship | ||
| isExplicitNull = true | ||
| } else if relationshipDecodeErr != nil { | ||
| fmt.Printf("decode err %v\n", relationshipDecodeErr) |
There was a problem hiding this comment.
How about er = fmt.Errorf("decode err %v\n", relationshipDecodeErr)
request.go
Outdated
|
|
||
| // Explicit null supplied for the field value | ||
| // If a nullable relationship we set the | ||
| if isExplicitNull && strings.HasPrefix(fieldType.Type.Name(), "NullableRelationship[") { |
There was a problem hiding this comment.
Would it be enough to simply check isExplicitNull? It looks like the only way it can be true here is if we have a NullableRelationship.
There was a problem hiding this comment.
Yes I think something like this should be ok:
if isExplicitNull {
fieldValue.Set(reflect.MakeMapWithSize(fieldValue.Type(), 1))
fieldValue.SetMapIndex(reflect.ValueOf(false), m)
} I can run the tests to ensure they still pass
769caae to
f63a032
Compare
|
I have another PR that incorporates latest main branch's changes. |
http://jsonapi.org/format/#document-resource-object-relationships
http://jsonapi.org/format/#document-resource-object-linkage
Relationships can have a data node set to null (e.g. to disassociate the relationship)
The NullableRelationship type allows this data to be serialized/de-serialized. When serialized, a NullableRelationship with an explicit null will serialize to the appropriate "null" type. Either '"data": null' or '[]'.
Supports slice and regular reference types for serialization, and regular reference types for de-serialization.
The reason to not support slices for de-serialization is we need to decide how to handle the zero type for a NullableRelationship[[]*...] slice. Since we aren't using this and can emulate the nulling behavior by omitting omit-empty we decided to ignore this case for now.