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

Allow attributes to be marshalled for relations #12

Merged
merged 7 commits into from
Aug 26, 2021

Conversation

cam-stitt
Copy link
Member

@cam-stitt cam-stitt commented Aug 20, 2021

According to the spec, relationships must have both an id and type field be present. However, this limits the ability to perform certain actions such as creating records via the relationship at the same time as the root record.

This PR modifies toShallowNode to allow for attributes to be allocated if the ID has a zero value (""). It's worth noting that if the struct sent has a primary type that is not a string (eg. an int), the zero value check will fail as the ID will be "0". It was determined to be a reasonable issue to have as go-tfemakes no use of non-stringID` types.

Test Results
=== RUN   TestErrorObjectWritesExpectedErrorMessage
--- PASS: TestErrorObjectWritesExpectedErrorMessage (0.00s)
=== RUN   TestMarshalErrorsWritesTheExpectedPayload
=== RUN   TestMarshalErrorsWritesTheExpectedPayload/TestFieldsAreSerializedAsNeeded
=== RUN   TestMarshalErrorsWritesTheExpectedPayload/TestMetaFieldIsSerializedProperly
--- PASS: TestMarshalErrorsWritesTheExpectedPayload (0.00s)
    --- PASS: TestMarshalErrorsWritesTheExpectedPayload/TestFieldsAreSerializedAsNeeded (0.00s)
    --- PASS: TestMarshalErrorsWritesTheExpectedPayload/TestMetaFieldIsSerializedProperly (0.00s)
=== RUN   TestUnmarshall_attrStringSlice
--- PASS: TestUnmarshall_attrStringSlice (0.00s)
=== RUN   TestUnmarshall_attrInterface
--- PASS: TestUnmarshall_attrInterface (0.00s)
=== RUN   TestUnmarshalToStructWithPointerAttr
--- PASS: TestUnmarshalToStructWithPointerAttr (0.00s)
=== RUN   TestUnmarshalPayload_missingTypeFieldShouldError
--- PASS: TestUnmarshalPayload_missingTypeFieldShouldError (0.00s)
=== RUN   TestUnmarshalPayload_ptrsAllNil
--- PASS: TestUnmarshalPayload_ptrsAllNil (0.00s)
=== RUN   TestUnmarshalPayloadWithPointerID
--- PASS: TestUnmarshalPayloadWithPointerID (0.00s)
=== RUN   TestUnmarshalPayloadWithPointerAttr_AbsentVal
--- PASS: TestUnmarshalPayloadWithPointerAttr_AbsentVal (0.00s)
=== RUN   TestUnmarshalToStructWithPointerAttr_BadType_bool
--- PASS: TestUnmarshalToStructWithPointerAttr_BadType_bool (0.00s)
=== RUN   TestUnmarshalToStructWithPointerAttr_BadType_MapPtr
--- PASS: TestUnmarshalToStructWithPointerAttr_BadType_MapPtr (0.00s)
=== RUN   TestUnmarshalToStructWithPointerAttr_BadType_Struct
--- PASS: TestUnmarshalToStructWithPointerAttr_BadType_Struct (0.00s)
=== RUN   TestUnmarshalToStructWithPointerAttr_BadType_IntSlice
--- PASS: TestUnmarshalToStructWithPointerAttr_BadType_IntSlice (0.00s)
=== RUN   TestStringPointerField
--- PASS: TestStringPointerField (0.00s)
=== RUN   TestMalformedTag
--- PASS: TestMalformedTag (0.00s)
=== RUN   TestUnmarshalInvalidJSON
--- PASS: TestUnmarshalInvalidJSON (0.00s)
=== RUN   TestUnmarshalInvalidJSON_BadType
=== RUN   TestUnmarshalInvalidJSON_BadType/Test_string_field
=== RUN   TestUnmarshalInvalidJSON_BadType/Test_float_field
=== RUN   TestUnmarshalInvalidJSON_BadType/Test_time_field
=== RUN   TestUnmarshalInvalidJSON_BadType/Test_time_ptr_field
--- PASS: TestUnmarshalInvalidJSON_BadType (0.00s)
    --- PASS: TestUnmarshalInvalidJSON_BadType/Test_string_field (0.00s)
    --- PASS: TestUnmarshalInvalidJSON_BadType/Test_float_field (0.00s)
    --- PASS: TestUnmarshalInvalidJSON_BadType/Test_time_field (0.00s)
    --- PASS: TestUnmarshalInvalidJSON_BadType/Test_time_ptr_field (0.00s)
=== RUN   TestUnmarshalSetsID
--- PASS: TestUnmarshalSetsID (0.00s)
=== RUN   TestUnmarshal_nonNumericID
--- PASS: TestUnmarshal_nonNumericID (0.00s)
=== RUN   TestUnmarshalSetsAttrs
--- PASS: TestUnmarshalSetsAttrs (0.00s)
=== RUN   TestUnmarshal_Times
=== RUN   TestUnmarshal_Times/default_byValue
=== RUN   TestUnmarshal_Times/default_byPointer
=== RUN   TestUnmarshal_Times/default_invalid
=== RUN   TestUnmarshal_Times/iso8601_byValue
=== RUN   TestUnmarshal_Times/iso8601_byPointer
=== RUN   TestUnmarshal_Times/iso8601_invalid
=== RUN   TestUnmarshal_Times/rfc3339_byValue
=== RUN   TestUnmarshal_Times/rfc3339_byPointer
=== RUN   TestUnmarshal_Times/rfc3339_invalid
--- PASS: TestUnmarshal_Times (0.00s)
    --- PASS: TestUnmarshal_Times/default_byValue (0.00s)
    --- PASS: TestUnmarshal_Times/default_byPointer (0.00s)
    --- PASS: TestUnmarshal_Times/default_invalid (0.00s)
    --- PASS: TestUnmarshal_Times/iso8601_byValue (0.00s)
    --- PASS: TestUnmarshal_Times/iso8601_byPointer (0.00s)
    --- PASS: TestUnmarshal_Times/iso8601_invalid (0.00s)
    --- PASS: TestUnmarshal_Times/rfc3339_byValue (0.00s)
    --- PASS: TestUnmarshal_Times/rfc3339_byPointer (0.00s)
    --- PASS: TestUnmarshal_Times/rfc3339_invalid (0.00s)
=== RUN   TestUnmarshalRelationshipsWithoutIncluded
--- PASS: TestUnmarshalRelationshipsWithoutIncluded (0.00s)
=== RUN   TestUnmarshalRelationships
--- PASS: TestUnmarshalRelationships (0.00s)
=== RUN   TestUnmarshalNullRelationship
--- PASS: TestUnmarshalNullRelationship (0.00s)
=== RUN   TestUnmarshalNullRelationshipInSlice
--- PASS: TestUnmarshalNullRelationshipInSlice (0.00s)
=== RUN   TestUnmarshalNestedRelationships
--- PASS: TestUnmarshalNestedRelationships (0.00s)
=== RUN   TestUnmarshalRelationshipsSerializedEmbedded
--- PASS: TestUnmarshalRelationshipsSerializedEmbedded (0.00s)
=== RUN   TestUnmarshalNestedRelationshipsEmbedded
--- PASS: TestUnmarshalNestedRelationshipsEmbedded (0.00s)
=== RUN   TestUnmarshalRelationshipsSideloaded
--- PASS: TestUnmarshalRelationshipsSideloaded (0.00s)
=== RUN   TestUnmarshalNestedRelationshipsSideloaded
--- PASS: TestUnmarshalNestedRelationshipsSideloaded (0.00s)
=== RUN   TestUnmarshalNestedRelationshipsEmbedded_withClientIDs
--- PASS: TestUnmarshalNestedRelationshipsEmbedded_withClientIDs (0.00s)
=== RUN   TestUnmarshalLinks
--- PASS: TestUnmarshalLinks (0.00s)
=== RUN   TestUnmarshalManyPayload
--- PASS: TestUnmarshalManyPayload (0.00s)
=== RUN   TestOnePayload_withLinks
--- PASS: TestOnePayload_withLinks (0.00s)
=== RUN   TestManyPayload_withLinks
--- PASS: TestManyPayload_withLinks (0.00s)
=== RUN   TestUnmarshalCustomTypeAttributes
--- PASS: TestUnmarshalCustomTypeAttributes (0.00s)
=== RUN   TestUnmarshalCustomTypeAttributes_ErrInvalidType
--- PASS: TestUnmarshalCustomTypeAttributes_ErrInvalidType (0.00s)
=== RUN   TestUnmarshalNestedStructPtr
--- PASS: TestUnmarshalNestedStructPtr (0.00s)
=== RUN   TestUnmarshalNestedStruct
--- PASS: TestUnmarshalNestedStruct (0.00s)
=== RUN   TestUnmarshalNestedStructSlice
--- PASS: TestUnmarshalNestedStructSlice (0.00s)
=== RUN   TestUnmarshalNestedStructPointerSlice
--- PASS: TestUnmarshalNestedStructPointerSlice (0.00s)
=== RUN   TestMarshalPayload
--- PASS: TestMarshalPayload (0.00s)
=== RUN   TestMarshalPayloadWithNulls
--- PASS: TestMarshalPayloadWithNulls (0.00s)
=== RUN   TestMarshal_attrStringSlice
--- PASS: TestMarshal_attrStringSlice (0.00s)
=== RUN   TestWithoutOmitsEmptyAnnotationOnRelation
--- PASS: TestWithoutOmitsEmptyAnnotationOnRelation (0.00s)
=== RUN   TestWithOmitsEmptyAnnotationOnRelation
--- PASS: TestWithOmitsEmptyAnnotationOnRelation (0.00s)
=== RUN   TestWithExtraFieldOnRelation
=== RUN   TestWithExtraFieldOnRelation/to-one_success
=== RUN   TestWithExtraFieldOnRelation/to-many_success
--- PASS: TestWithExtraFieldOnRelation (0.00s)
    --- PASS: TestWithExtraFieldOnRelation/to-one_success (0.00s)
    --- PASS: TestWithExtraFieldOnRelation/to-many_success (0.00s)
=== RUN   TestWithOmitsEmptyAnnotationOnRelation_MixedData
--- PASS: TestWithOmitsEmptyAnnotationOnRelation_MixedData (0.00s)
=== RUN   TestWithOmitsEmptyAnnotationOnAttribute
--- PASS: TestWithOmitsEmptyAnnotationOnAttribute (0.00s)
=== RUN   TestMarshalIDPtr
--- PASS: TestMarshalIDPtr (0.00s)
=== RUN   TestMarshalOnePayload_omitIDString
--- PASS: TestMarshalOnePayload_omitIDString (0.00s)
=== RUN   TestMarshall_invalidIDType
--- PASS: TestMarshall_invalidIDType (0.00s)
=== RUN   TestOmitsEmptyAnnotation
--- PASS: TestOmitsEmptyAnnotation (0.00s)
=== RUN   TestHasPrimaryAnnotation
--- PASS: TestHasPrimaryAnnotation (0.00s)
=== RUN   TestSupportsAttributes
--- PASS: TestSupportsAttributes (0.00s)
=== RUN   TestOmitsZeroTimes
--- PASS: TestOmitsZeroTimes (0.00s)
=== RUN   TestMarshal_Times
=== RUN   TestMarshal_Times/default_byValue
=== RUN   TestMarshal_Times/default_byPointer
=== RUN   TestMarshal_Times/iso8601_byValue
=== RUN   TestMarshal_Times/iso8601_byPointer
=== RUN   TestMarshal_Times/rfc3339_byValue
=== RUN   TestMarshal_Times/rfc3339_byPointer
--- PASS: TestMarshal_Times (0.00s)
    --- PASS: TestMarshal_Times/default_byValue (0.00s)
    --- PASS: TestMarshal_Times/default_byPointer (0.00s)
    --- PASS: TestMarshal_Times/iso8601_byValue (0.00s)
    --- PASS: TestMarshal_Times/iso8601_byPointer (0.00s)
    --- PASS: TestMarshal_Times/rfc3339_byValue (0.00s)
    --- PASS: TestMarshal_Times/rfc3339_byPointer (0.00s)
=== RUN   TestSupportsLinkable
--- PASS: TestSupportsLinkable (0.00s)
=== RUN   TestInvalidLinkable
--- PASS: TestInvalidLinkable (0.00s)
=== RUN   TestSupportsMetable
--- PASS: TestSupportsMetable (0.00s)
=== RUN   TestRelations
--- PASS: TestRelations (0.00s)
=== RUN   TestNoRelations
--- PASS: TestNoRelations (0.00s)
=== RUN   TestMarshalPayloadWithoutIncluded
--- PASS: TestMarshalPayloadWithoutIncluded (0.00s)
=== RUN   TestMarshalPayload_many
--- PASS: TestMarshalPayload_many (0.00s)
=== RUN   TestMarshalMany_WithSliceOfStructPointers
--- PASS: TestMarshalMany_WithSliceOfStructPointers (0.00s)
=== RUN   TestMarshalManyWithoutIncluded
--- PASS: TestMarshalManyWithoutIncluded (0.00s)
=== RUN   TestMarshalMany_SliceOfInterfaceAndSliceOfStructsSameJSON
--- PASS: TestMarshalMany_SliceOfInterfaceAndSliceOfStructsSameJSON (0.00s)
=== RUN   TestMarshal_InvalidIntefaceArgument
--- PASS: TestMarshal_InvalidIntefaceArgument (0.00s)
PASS
ok      github.com/hashicorp/jsonapi    0.131s
=== RUN   TestExampleHandler_post
--- PASS: TestExampleHandler_post (0.00s)
=== RUN   TestExampleHandler_put
--- PASS: TestExampleHandler_put (0.00s)
=== RUN   TestExampleHandler_get_show
--- PASS: TestExampleHandler_get_show (0.00s)
=== RUN   TestExampleHandler_get_list
--- PASS: TestExampleHandler_get_list (0.00s)
=== RUN   TestHttpErrorWhenHeaderDoesNotMatch
--- PASS: TestHttpErrorWhenHeaderDoesNotMatch (0.00s)
=== RUN   TestHttpErrorWhenMethodDoesNotMatch
--- PASS: TestHttpErrorWhenMethodDoesNotMatch (0.00s)
PASS
ok      github.com/hashicorp/jsonapi/examples   (cached)

@cam-stitt cam-stitt self-assigned this Aug 20, 2021
@cam-stitt cam-stitt requested a review from chrisarcand August 24, 2021 00:17
@cam-stitt cam-stitt marked this pull request as ready for review August 24, 2021 00:17
@cam-stitt cam-stitt requested a review from thrashr888 August 24, 2021 00:18
Copy link
Member

@thrashr888 thrashr888 left a comment

Choose a reason for hiding this comment

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

yes please!

@cam-stitt cam-stitt changed the title Allow an extra field to be marshalled for relations Allow attributes to be marshalled for relations Aug 24, 2021
Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

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

I'm wondering - is the special allowattrs field tag annotation necessary? It seems like we could do "If the primary ID field isn't set, any fields which are 1) not the primary ID or type attr and 2) Not zero value (omitempty) should then be included in the marshaling. "

Copy link
Member

@chrisarcand chrisarcand 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!

@cam-stitt cam-stitt merged commit ee7dae0 into master Aug 26, 2021
@cam-stitt cam-stitt deleted the cs/allow-extra-field-relation branch August 26, 2021 22:46
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.

3 participants