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 ptr in inline structs #146

Merged
merged 3 commits into from
Apr 30, 2018
Merged

allow ptr in inline structs #146

merged 3 commits into from
Apr 30, 2018

Conversation

larrycinnabar
Copy link

BSON encoder doesn't allow to use pointers to struct in inline mode.

There was an issue on the same topic on old repo:
https://github.com/go-mgo/mgo/issues/346

Such structure:

type MStruct struct {
	M int `bson:"m,omitempty"`
}
type inlinePtrStruct struct {
	A int
	*MStruct `bson:",inline"`
}

is assumed to become {"a":0, "m":0} (without mstruct structure) - the same way as json encoder does it

without fix we get: "reflect: indirection through nil pointer to embedded struct"

@domodwyer
Copy link

Hi @larrycinnabar

Thanks for the PR - we'll take a proper look but seems like a great change!

Dom

szank
szank previously approved these changes Apr 19, 2018
Copy link

@szank szank left a comment

Choose a reason for hiding this comment

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

Looks good!

bson/encode.go Outdated
@@ -229,7 +229,12 @@ func (e *encoder) addStruct(v reflect.Value) {
if info.Inline == nil {
value = v.Field(info.Num)
} else {
value = v.FieldByIndex(info.Inline)
field, errField := safeFieldByIndex(v, info.Inline)

Choose a reason for hiding this comment

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

this change will silently skip encoding errors, where they used to panic before.
Not very familiar with the bson encoder anymore, @domodwyer thoughts?

Copy link
Author

@larrycinnabar larrycinnabar Apr 19, 2018

Choose a reason for hiding this comment

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

Encoder has a structInfo object, based only on type, not on value.
As we allow pointers to structs here, we can have chain of inline indices (info.Inline) and e.g. element behind a n-1 index might be nil, so trying to get element by index n will panic because of reflect: indirection through nil pointer to embedded struct

Choose a reason for hiding this comment

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

I think this is probably fine either way - relying on the encoder to panic because the input was invalid is a bit of a stretch in terms of backwards-compatibility.

@domodwyer
Copy link

Hi @larrycinnabar

Would you pull the latest development into this branch and we'll get it merged!

Thanks for taking the time to open a PR - it's really appreciated.

Dom

@larrycinnabar
Copy link
Author

Branch is updated. Also a commit with some extra comments is added

@domodwyer
Copy link

Hi @larrycinnabar

Thanks for taking the time to contribute 👍

Dom

@domodwyer domodwyer merged commit 9a3d363 into globalsign:development Apr 30, 2018
@larrycinnabar larrycinnabar deleted the development branch May 1, 2018 08:40
@domodwyer domodwyer mentioned this pull request Jun 15, 2018
@bojangavrovski
Copy link

This only works when transforming data to bson to be written into mongo. When reading (populating the struct with) a record I still get the "reflect: indirection through nil pointer to embedded struct" error.

@larrycinnabar
Copy link
Author

@bojangavrovski hm. Ok, I'm looking into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants