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

#184: Allow custom unmarshal implementation #185

Merged
merged 2 commits into from
Feb 8, 2022

Conversation

errorhandler
Copy link
Contributor

Issue #, if available: #184

Description of changes:
Adds the unmarshalling equivalent of Marshaler. Allowing types to implement their own unmarshalling logic. It's basically the same implementation, but requires the UnmarshalIon method to be implemented with a pointer receiver as it needs to mutate the struct.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@popematt popematt self-requested a review February 7, 2022 21:36
@popematt
Copy link
Contributor

popematt commented Feb 7, 2022

Hi, thanks for your contribution!

It looks like the CI build failed because one of our Github Workflows was broken. I've fixed the workflow now. Do you mind rebasing your commit against the tip of master in amzn/ion-go so that we can re-run with the fixed workflow? Alternately, you could grant me permission to edit the PR, and I'll update it.

Copy link
Contributor

@popematt popematt left a comment

Choose a reason for hiding this comment

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

Aside from one very small typo, this looks good. Thank you!

@@ -38,6 +38,11 @@ var typesAcceptableKinds = map[Type][]reflect.Kind{
ListType: {reflect.Slice, reflect.Array},
}

// Unmarshaler is the interface implemented by types that can unmarshal themselves to Ion.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Unmarshaler is the interface implemented by types that can unmarshal themselves to Ion.
// Unmarshaler is the interface implemented by types that can unmarshal themselves from Ion.

@popematt popematt merged commit 0964a5e into amazon-ion:master Feb 8, 2022
@popematt popematt mentioned this pull request Feb 8, 2022
@Vingtoft
Copy link

Vingtoft commented Jan 4, 2023

HI, can you please provide an example of how to use UnmarshalIon?

Given the struct:

type Account struct {
	Name         string          `ion:"name"`
}

I write the function:

func (a *Account) UnmarshalIon(b []byte) error {
    fmt.Println("In Unmarshall")
    a.Name = "boo"
    return nil
}

The above implementation does not work.

Needless to say: Im learning Go.

Thanks!

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