globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

allow ptr in inline structs #146

Closed larrycinnabar closed 6 years ago

larrycinnabar commented 6 years ago

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 commented 6 years ago

Hi @larrycinnabar

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

Dom

domodwyer commented 6 years ago

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 commented 6 years ago

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

domodwyer commented 6 years ago

Hi @larrycinnabar

Thanks for taking the time to contribute 👍

Dom

bojangavrovski commented 6 years ago

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 commented 6 years ago

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