Skip to content

Pointer in inline structs returns error when reading from database #210

@bojangavrovski

Description

@bojangavrovski

Hi
I encountered an issue while populating a struct with db data. My structs are as follows:

type Product struct {
    ID bson.ObjectId `json:"id" bson:"_id"`
    Name string `json:"name" bson:"name"`
    Meta `json:"meta" bson:"meta"`
}

type Meta {
    *Printer `json:",inline" bson:",inline"`
    *Monitor `json:",inline" bson:",inline"`
}

type Monitor struct {
    ScreenSize float64 `json:"screen_size" bson:"screen_size"`
    ScreenType string `json:"screen_type" bson:"screen_type"`
}

...

p := Product{}
if err := db.C("products").Find(nil).One(&p); err != nil {
    return err
}

the error that I get from this action is

reflect: indirection through nil pointer to embedded struct

I should note that this only happens while reading from the db, and not when inserting into.

I am aware that this issue has been raised couple of times before: #146, go-mgo#346, and although a fix has been submitted, it still seems that it doesn't work properly (at least for reading data from the db).

The version of go that I'm using is

go version go1.10.3 darwin/amd64

and the globalsign/mgo version is the last one available

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions