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 custom unmarshaling support #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

merlinz01
Copy link

@merlinz01 merlinz01 commented Sep 14, 2024

This PR adds the capability to define an UnmarshalMsgpack() method on any type, which allows customizing how unmarshaling works.

The UnmarshalMsgpack() method takes a single parameter of type any which gets the value which would be unmarshaled into an any type. The method should check the type of the parameter, which can be any type that msgpack supports. The method returns the value to assign and any error encountered.

One use case for this is illustrated in the tests provided, where a string composed of concatenated IP networks is received and the user wants to parse them into a slice of net.IPNet's on the fly.

@merlinz01 merlinz01 marked this pull request as draft September 16, 2024 17:50
@merlinz01 merlinz01 marked this pull request as draft September 16, 2024 17:50
@merlinz01 merlinz01 marked this pull request as ready for review September 17, 2024 00:11
@shamaton
Copy link
Owner

Hi.

Why don't you use ext format?
Do you know how to use it?
It seems like that we can encode the same as time.Time.

@merlinz01
Copy link
Author

I suppose I could do it that way. The ext.Encoder and ext.Decoder interfaces are not documented; could you show me how to use them?

@shamaton
Copy link
Owner

shamaton commented Sep 23, 2024

Here is examples.
https://github.com/shamaton/msgpack/blob/master/time/decode.go
https://github.com/shamaton/msgpack/blob/master/time/encode.go

You implement your custom encoder and then call AddExtCoder when you start your service.

msgpack.AddExtCoder(encoder, decoder)

@merlinz01
Copy link
Author

Even the examples aren't documented. You need to add comments in the code to explain what's happening and what the function is for. I don't understand how I should make it decode my type.

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.

2 participants