GetBSON, SetBSON not called

531 views
Skip to first unread message

Darko Luketic

unread,
Mar 19, 2014, 7:52:05 PM3/19/14
to mgo-...@googlegroups.com
package main

import (
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
)

const (
    DBHOST = "localhost"
    DBNAME = "test5"
)

var (
    mgoSession  *mgo.Session
    userDB      *mgo.Database
    userR       *userRepository = new(userRepository)
    communityDB *mgo.Database
    communityR  *communityRepository = new(communityRepository)
)

type (
    User struct {
        Id       bson.ObjectId `bson:"_id,omitempty" json:"id"`
        Email    string        `json:"email"`
        Password string        `json:"password"`
    }

    Community struct {
        Id    bson.ObjectId `bson:"_id,omitempty" json:"id"`
        Name  string        `json:"name"`
        Owner *User         `json:"owner"`
    }

    communityRepository struct {
        Collection *mgo.Collection
    }

    userRepository struct {
        Collection *mgo.Collection
    }
)

func main() {
    var (
        err error
    )

    // Connect to Mongo
    mgoSession, err = mgo.Dial(DBHOST)
    if err != nil {
        panic(err)
    }
    defer mgoSession.Close()

    userDB = mgoSession.DB(DBNAME)
    userR.Collection = userDB.C("users")
    communityDB = mgoSession.DB(DBNAME)
    communityR.Collection = communityDB.C("communities")

    u := new(User)
    u.Id = bson.NewObjectIdWithTime(bson.Now())
    u.Email = "darko"
    u.Password = "pass"
    userR.Create(u)

    u1, err := userR.FindOneByEmail("darko")

    c := new(Community)
    c.Id = bson.NewObjectId()
    c.Name = "commy"
    c.Owner = u1
    communityR.Create(c)

}

func (r userRepository) Create(user *User) (err error) {
    if user.Id.Hex() == "" {
        user.Id = bson.NewObjectId()
    }
    _, err = r.Collection.UpsertId(user.Id, user)
    return
}

func (r userRepository) FindOneByEmail(email string) (result *User, err error) {
    result = new(User)
    err = r.Collection.Find(bson.M{"email": email}).One(result)
    return
}

func (r communityRepository) Create(community *Community) (err error) {
    if community.Id.Hex() == "" {
        community.Id = bson.NewObjectId()
    }
    _, err = r.Collection.UpsertId(community.Id, community)
    return
}

func (u *User) GetBSON(raw bson.Raw) error {
    fmt.Printf("raw:%s\n", raw)
    return raw.Unmarshal(u)
}

func (u *User) SetBSON() (interface{}, error) {
    fmt.Printf("set:%s\n", u)
    return u.Id.Hex(), nil
}

########

There is no output
It should output at least either raw: or set:

Gustavo Niemeyer

unread,
Mar 19, 2014, 8:06:04 PM3/19/14
to mgo-...@googlegroups.com
The methods must have the exact interface defined in the
documentation, otherwise they're not considered relevant:

http://labix.org/v2/mgo/bson#Getter
http://labix.org/v2/mgo/bson#Setter
> --
> You received this message because you are subscribed to the Google Groups
> "mgo-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mgo-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

gustavo @ http://niemeyer.net

Darko Luketic

unread,
Mar 19, 2014, 8:15:29 PM3/19/14
to mgo-...@googlegroups.com
Oh god I'm retarded
Thanks and sorry for bothering you

Matt Siegel

unread,
Mar 28, 2014, 9:32:27 PM3/28/14
to mgo-...@googlegroups.com
Another reason for SetBSON not being called: unmarshaling into the wrong type :)

My experience was:
I used GetBSON to marshal a [2]float64 array into a GeoJSON Point (which is a struct) for storage and queries... Conversion worked great in that direction.  But when I tried to load it back in, the data was silently sent into a black hole and I got zeroes instead.  No errors returned, just zeroes in the fields, simply due to array/struct being incompatible for unmarshaling.

The other symptom was SetBSON not being called.

So don't do what I did! :)  I'm switching the array to a struct.

Gustavo Niemeyer

unread,
Mar 28, 2014, 11:17:02 PM3/28/14
to mgo-...@googlegroups.com
You can use a slice or an array too, but you'll need to get a pointer to the
slice, in the same way you get a pointer to the struct, otherwise you'll
modify the copy that the function gets instead of the original value
pointed to by the value address.

Makes sense?

Matt Siegel

unread,
Mar 30, 2014, 4:23:41 PM3/30/14
to mgo-...@googlegroups.com
That makes sense, thanks Gustavo.  Using a struct worked out well in my case, but I'm glad to hear slices are also possible.

My compliments on your excellent library! :)
Reply all
Reply to author
Forward
0 new messages