Skip to content

Conversation

@Emptyless
Copy link
Contributor

Goal of this PR

Currently when implementing (not strictly nullable) unions, one has to fall back to either any or map[string]any while from a readability perspective it would be nice to see the actual possible Go types. This also makes the consumption of a received message easier as no casting to a type has to performed anymore and likewise creating objects to serialise is less error prone because no invalid type can be used (assuming that the struct is implemented correctly).

I've updated the README rather verbose, looking for feedback on the PR and the updated README

Fixes #
n/a

How did I test it?

  • [ x ] make test (and added tests for union cases, updated readme with real world example)
  • [ x ] make fmt
  • [ x ] make lint

The example in the README is tested from in-memory > marshal to avro bytes > unmarshal from avro bytes > same as before

@nrwiersma nrwiersma requested a review from Copilot April 16, 2025 05:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

codec_union.go:393

  • Ensure that the type conversion via UnionUnmarshaller is safe; consider adding extra validation or error handling to prevent potential runtime panics if the target type does not fully implement the interface.
isUnionUnmarshaller := d.typ.Implements(reflect2.Type2(reflect.TypeFor[UnionUnmarshaller]()))

codec.go:41

  • [nitpick] The use of an intermediate variable 'obj2' and its assignment to 'obj' for union unmarshalling could reduce code clarity; consider renaming 'obj2' or adding a clarifying comment to improve readability.
obj = &obj2

Copy link
Member

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

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

Interesting idea. I think you are handling this way to high up in the structure. Likely this needs to be a codec like textMarshalerCodec.

@Emptyless
Copy link
Contributor Author

Interesting idea. I think you are handling this way to high up in the structure. Likely this needs to be a codec like textMarshalerCodec.

I've tried to reimplement it in two codecs (one for Unmarshaller and one for Marshaller), but do note that this is the first time I've used the reflect2 library so extensively and might warrant an extensive review. I've added more tests as safeguards including an example e2e test TestEncoderDecoder_UnionMarshalUnmarshalInterface which also reflects on the comment:

Whatever type the Marshaller produces the first time will be cached and assumed for all runs, which is not true.

Given the TestEncoderDecoder_UnionMarshalUnmarshalInterface test works for two types, can you elaborate more if this is still an area for improvement in your opinion?

@Emptyless
Copy link
Contributor Author

@nrwiersma it looks like the github action needs to be updated but changing CI/CD of another project feels wrong. Based on an earlier discussion lemurheavy/coveralls-public#1716 , the inputs (https://github.com/coverallsapp/github-action?tab=readme-ov-file#inputs) need to be adjusted to reflect the parallel builds

      - name: Coveralls
        uses: coverallsapp/[email protected]
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel: true
          flag-name: go-${{ matrix.go-version }}
          path-to-lcov: coverage.lcov

I see that #524 has the same error

@Emptyless
Copy link
Contributor Author

thanks for the review @nrwiersma, I'll pick it up somewhere this week

@Emptyless
Copy link
Contributor Author

@nrwiersma have you had time to take a look at the changes?

Copy link
Member

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

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

I will look at the test coverage tonight, a couple nitpicks

@nrwiersma
Copy link
Member

Test coverage looks good to me. Once the 2 nitpicks are resolved I think we can merge.

@Emptyless
Copy link
Contributor Author

Updated branch and implemented comments

@nrwiersma nrwiersma merged commit 6c73860 into hamba:main Jun 10, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants