Skip to content
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 #31

Merged
merged 6 commits into from
Feb 7, 2025
Merged

Conversation

netramali
Copy link
Collaborator

@netramali netramali commented Jan 31, 2025

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.

Related commits:
1st Commit
2nd Commit

Reference PR

@@ -42,6 +42,7 @@ type WithNullableAttrs struct {
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"`
Copy link
Collaborator Author

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.

} else {
node.Relationships[args[1]] = json.RawMessage("{\"data\":null}")
}
return nil
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Karl originally had continue here instead of return but it was giving me an error suggesting that we should use return instead.

@netramali netramali changed the title Add nullable relationship Add NullableRelationship support Feb 3, 2025
@netramali netramali marked this pull request as ready for review February 3, 2025 22:12
@netramali netramali requested a review from a team as a code owner February 3, 2025 22:12
Copy link

@ctrombley ctrombley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good! Just a few minor comments.

nullable.go Outdated Show resolved Hide resolved
nullable.go Outdated Show resolved Hide resolved
nullable.go Outdated Show resolved Hide resolved
request.go Outdated Show resolved Hide resolved
models_test.go Show resolved Hide resolved
Copy link

@mkam mkam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@netramali netramali force-pushed the netramali/nullable-relationship branch from 4316bbf to e123c06 Compare February 5, 2025 16:23
Copy link

@ctrombley ctrombley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice work!

@netramali netramali merged commit 876fb53 into main Feb 7, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants