Skip to content

allow ptr in inline structs - #146

Merged
domodwyer merged 3 commits into
globalsign:developmentfrom
larrycinnabar:development
Apr 30, 2018
Merged

domodwyer merged 3 commits into
globalsign:developmentfrom
larrycinnabar:development

Conversation

@larrycinnabar

Copy link
Copy Markdown

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
Copy Markdown

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

@szank szank left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good!

Comment thread bson/encode.go
value = v.Field(info.Num)
} else {
value = v.FieldByIndex(info.Inline)
field, errField := safeFieldByIndex(v, info.Inline)

Copy link
Copy Markdown

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?

@larrycinnabar larrycinnabar Apr 19, 2018 •

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
Author

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

@domodwyer

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
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.

5 participants

⌂ 换网址