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

fix MessageCodec Decode #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ramonberrutti
Copy link

Inside bson decode the code is doing something different to Encode:

	rval := reflect.ValueOf(val)
	switch rval.Kind() {
	case reflect.Ptr:
		if rval.IsNil() {
			return ErrDecodeToNil
		}
		rval = rval.Elem()
	case reflect.Map:
		if rval.IsNil() {
			return ErrDecodeToNil
		}
	default:
		return fmt.Errorf("argument to Decode must be a pointer or a map, but got %v", rval)
	}

@vallahaye
Copy link
Owner

Hello Ramón,

Apologies for the late response.

The last time I used protobson, the encoding/decoding of Protobuf messages worked correctly. I'm not aware of any change in the Mongo driver that has broken anything. The API used by protobson is public and the driver is still in v1.

That being said, I suspect you're passing a nil pointer to the struct you want to deserialize into, in which case you need to initialize it before decoding:

example := new(examplepb.Example)
collection.FindOne(...).Decode(example)

Or:

var example examplepb.Example
collection.FindOne(...).Decode(&example)

If it is not the case, could you please create an issue related to that PR and give me more context around your contribution, i.e. what's wrong with the current implementation.

Have a nice day

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