-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add NullableRelationship support #23
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,35 @@ import ( | |
// Adapted from https://www.jvt.me/posts/2024/01/09/go-json-nullable/ | ||
type NullableAttr[T any] map[bool]T | ||
|
||
// NullableRelationship is a generic type, which implements a field that can be one of three states: | ||
// | ||
// - relationship is not set in the request | ||
// - relationship is explicitly set to `null` in the request | ||
// - relationship is explicitly set to a valid relationship value in the request | ||
// | ||
// NullableRelationship is intended to be used with JSON marshalling and unmarshalling. | ||
// This is generally useful for PATCH requests, where relationships with zero | ||
// values are intentionally not marshaled into the request payload so that | ||
// existing attribute values are not overwritten. | ||
// | ||
// Internal implementation details: | ||
// | ||
// - map[true]T means a value was provided | ||
// - map[false]T means an explicit null was provided | ||
// - nil or zero map means the field was not provided | ||
// | ||
// If the relationship is expected to be optional, add the `omitempty` JSON tags. Do NOT use `*NullableRelationship`! | ||
// | ||
// Slice types are not currently supported for NullableRelationships as the nullable nature can be expressed via empty array | ||
// `polyrelation` JSON tags are NOT currently supported. | ||
// | ||
// NullableRelationships must have an inner type of pointer: | ||
// | ||
// - NullableRelationship[*Comment] - valid | ||
// - NullableRelationship[[]*Comment] - invalid | ||
// - NullableRelationship[Comment] - invalid | ||
type NullableRelationship[T any] map[bool]T | ||
|
||
// NewNullableAttrWithValue is a convenience helper to allow constructing a | ||
// NullableAttr with a given value, for instance to construct a field inside a | ||
// struct without introducing an intermediate variable. | ||
|
@@ -87,3 +116,65 @@ func (t NullableAttr[T]) IsSpecified() bool { | |
func (t *NullableAttr[T]) SetUnspecified() { | ||
*t = map[bool]T{} | ||
} | ||
|
||
// NewNullableAttrWithValue is a convenience helper to allow constructing a | ||
// NullableAttr with a given value, for instance to construct a field inside a | ||
// struct without introducing an intermediate variable. | ||
func NewNullableRelationshipWithValue[T any](t T) NullableRelationship[T] { | ||
var n NullableRelationship[T] | ||
n.Set(t) | ||
return n | ||
} | ||
|
||
// NewNullNullableAttr is a convenience helper to allow constructing a NullableAttr with | ||
// an explicit `null`, for instance to construct a field inside a struct | ||
// without introducing an intermediate variable | ||
func NewNullNullableRelationship[T any]() NullableRelationship[T] { | ||
var n NullableRelationship[T] | ||
n.SetNull() | ||
return n | ||
} | ||
|
||
// Get retrieves the underlying value, if present, and returns an error if the value was not present | ||
func (t NullableRelationship[T]) Get() (T, error) { | ||
var empty T | ||
if t.IsNull() { | ||
return empty, errors.New("value is null") | ||
} | ||
if !t.IsSpecified() { | ||
return empty, errors.New("value is not specified") | ||
} | ||
return t[true], nil | ||
} | ||
|
||
// Set sets the underlying value to a given value | ||
func (t *NullableRelationship[T]) Set(value T) { | ||
*t = map[bool]T{true: value} | ||
} | ||
|
||
// Set sets the underlying value to a given value | ||
func (t *NullableRelationship[T]) SetInterface(value interface{}) { | ||
t.Set(value.(T)) | ||
} | ||
|
||
// IsNull indicate whether the field was sent, and had a value of `null` | ||
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. nit: s/indicate/indicates/ |
||
func (t NullableRelationship[T]) IsNull() bool { | ||
_, foundNull := t[false] | ||
return foundNull | ||
} | ||
|
||
// SetNull sets the value to an explicit `null` | ||
func (t *NullableRelationship[T]) SetNull() { | ||
var empty T | ||
*t = map[bool]T{false: empty} | ||
} | ||
|
||
// IsSpecified indicates whether the field was sent | ||
func (t NullableRelationship[T]) IsSpecified() bool { | ||
return len(t) != 0 | ||
} | ||
|
||
// SetUnspecified sets the value to be absent from the serialized payload | ||
func (t *NullableRelationship[T]) SetUnspecified() { | ||
*t = map[bool]T{} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -459,10 +459,30 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) | |
|
||
buf := bytes.NewBuffer(nil) | ||
|
||
json.NewEncoder(buf).Encode( | ||
data.Relationships[args[1]], | ||
) | ||
json.NewDecoder(buf).Decode(relationship) | ||
relDataStr := data.Relationships[args[1]] | ||
json.NewEncoder(buf).Encode(relDataStr) | ||
|
||
isExplicitNull := false | ||
relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship) | ||
if relationshipDecodeErr == nil && relationship.Data == nil { | ||
// If the relationship was a valid node and relationship data was null | ||
// 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to swallow this error? |
||
} | ||
|
||
// This will hold either the value of the choice type model or the actual | ||
// model, depending on annotation | ||
m := reflect.New(fieldValue.Type().Elem()) | ||
|
||
// Nullable relationships have an extra pointer indirection | ||
// 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the grossest part of the code. It feels wrong to "just unwrap one more pointer" but it does work as expected. |
||
} | ||
} | ||
|
||
/* | ||
http://jsonapi.org/format/#document-resource-object-relationships | ||
|
@@ -471,20 +491,29 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) | |
so unmarshal and set fieldValue only if data obj is not null | ||
*/ | ||
if relationship.Data == nil { | ||
|
||
// Explicit null supplied for the field value | ||
// If a nullable relationship we set the | ||
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. 👀 |
||
if isExplicitNull && strings.HasPrefix(fieldType.Type.Name(), "NullableRelationship[") { | ||
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. Would it be enough to simply check |
||
fieldValue.Set(reflect.MakeMapWithSize(fieldValue.Type(), 1)) | ||
fieldValue.SetMapIndex(reflect.ValueOf(false), m) | ||
} | ||
|
||
continue | ||
} | ||
|
||
// This will hold either the value of the choice type model or the actual | ||
// model, depending on annotation | ||
m := reflect.New(fieldValue.Type().Elem()) | ||
|
||
err = unmarshalNodeMaybeChoice(&m, relationship.Data, annotation, choiceMapping, included) | ||
if err != nil { | ||
er = err | ||
break | ||
} | ||
|
||
fieldValue.Set(m) | ||
if strings.HasPrefix(fieldType.Type.Name(), "NullableRelationship[") { | ||
fieldValue.Set(reflect.MakeMapWithSize(fieldValue.Type(), 1)) | ||
fieldValue.SetMapIndex(reflect.ValueOf(true), m) | ||
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. 👍 |
||
} else { | ||
fieldValue.Set(m) | ||
} | ||
} | ||
} else if annotation == annotationLinks { | ||
if data.Links == nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,7 +331,7 @@ func visitModelNode(model interface{}, included *map[string]*Node, | |
node.Attributes = make(map[string]interface{}) | ||
} | ||
|
||
// Handle Nullable[T] | ||
// Handle NullableAttr[T] | ||
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. 🙇 |
||
if strings.HasPrefix(fieldValue.Type().Name(), "NullableAttr[") { | ||
// handle unspecified | ||
if fieldValue.IsNil() { | ||
|
@@ -343,7 +343,6 @@ func visitModelNode(model interface{}, included *map[string]*Node, | |
node.Attributes[args[1]] = json.RawMessage("null") | ||
continue | ||
} else { | ||
|
||
// handle value | ||
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true)) | ||
} | ||
|
@@ -410,6 +409,28 @@ func visitModelNode(model interface{}, included *map[string]*Node, | |
omitEmpty = args[2] == annotationOmitEmpty | ||
} | ||
|
||
if node.Relationships == nil { | ||
node.Relationships = make(map[string]interface{}) | ||
} | ||
|
||
// Handle NullableRelationship[T] | ||
if strings.HasPrefix(fieldValue.Type().Name(), "NullableRelationship[") { | ||
|
||
if fieldValue.MapIndex(reflect.ValueOf(false)).IsValid() { | ||
innerTypeIsSlice := fieldValue.MapIndex(reflect.ValueOf(false)).Type().Kind() == reflect.Slice | ||
// handle explicit null | ||
if innerTypeIsSlice { | ||
node.Relationships[args[1]] = json.RawMessage("[]") | ||
} else { | ||
node.Relationships[args[1]] = json.RawMessage("{\"data\":null}") | ||
} | ||
continue | ||
} else if fieldValue.MapIndex(reflect.ValueOf(true)).IsValid() { | ||
// handle value | ||
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true)) | ||
} | ||
} | ||
|
||
isSlice := fieldValue.Type().Kind() == reflect.Slice | ||
if omitEmpty && | ||
(isSlice && fieldValue.Len() < 1 || | ||
|
@@ -481,10 +502,6 @@ func visitModelNode(model interface{}, included *map[string]*Node, | |
} | ||
} | ||
|
||
if node.Relationships == nil { | ||
node.Relationships = make(map[string]interface{}) | ||
} | ||
|
||
var relLinks *Links | ||
if linkableModel, ok := model.(RelationshipLinkable); ok { | ||
relLinks = linkableModel.JSONAPIRelationshipLinks(args[1]) | ||
|
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.
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.