I am experiencing some odd behaviour with `inline`
When I return the output struct the thing works like a charm, but fails
when I pass a reference to the destination object.
Am i missing something here ?


package main

import (

type Base struct {
     Id string `bson:"_id" json:"_id"`
     ValueOn int `bson:"value_on" json:"value_on"`

type User struct {
     Base ",inline"
     Firstname string `bson:"first_name" json:"first_name"`

func BsonWithInterface(d interface{}, o interface{}) {
     b, _ := bson.Marshal(d)
     bson.Unmarshal(b, &o)

func BsonWithUser(d interface{}, o *User) {
     b, _ := bson.Marshal(d)
     bson.Unmarshal(b, &o)

func BsonWithReturn(d interface{}) User {
     var tmp User
     b, _ := bson.Marshal(d)
     bson.Unmarshal(b, &tmp)
     return tmp

func main() {
     var bson_tmp User
     var strict_tmp User

     x := User{Base{"1234", 5}, "Jaideep"}
     BsonWithInterface(x, &bson_tmp)
     log.Println("func (interface, interface):", bson_tmp.ValueOn,

     BsonWithUser(x, &strict_tmp)
     log.Println("func (interface, user):", bson_tmp.ValueOn, bson_tmp.Id)

     y := BsonWithReturn(x)
     log.Println("func (interface) User:", y.ValueOn, y.Id)

Output is
2013/05/13 19:44:17 func (interface, interface): 0
2013/05/13 19:44:17 func (interface, user): 0
2013/05/13 19:44:17 func (interface) User: 5 1234

Instead of
2013/05/13 19:44:17 func (interface, interface): 5 1234
2013/05/13 19:44:17 func (interface, user): 5 1234
2013/05/13 19:44:17 func (interface) User: 5 1234

On Thursday, April 11, 2013 11:58:06 AM UTC+5:30, Gustavo Niemeyer wrote:

Quickly following the earlier release of the mgo (http://labix.org/mgo)
MongoDB driver for Go, there's r2013.04.11 leaving the oven just now.

This release brings both a nice improvement for the marshaling of structs,
and a relevant bug fix. The changes are:

- The mgo/bson package (and consequently, mgo itself) now supports the
",inline" tag option on map fields as well. This feature enables the
use of type-rich structs even in situations where the document content
is only partially known. As a simplistic example, a struct such as

type Person struct {
Name string
Phone string
Extra map[string]interface{} `bson:",inline"`

means that the "name" and "phone" document attributes would be
unmarshalled into the Name and Phone fields as usual, but other
unknown attributes that the document presents, if any, would be
preserved in the inlined map.

Marshaling a value of such a struct has the same properties, with
the map contents being marshalled next to the other fields.

More details about supported options and their meaning at:


- A bug was fixed on the cluster synchronization mechanism in cases
where the primary server suffers a very short fallout and quickly
reestablishes its functionality as a secondary. In those situations,
mgo might still consider it as a primary and attempt to deliver
primary-specific commands. When the bug happened, no serious
consequences would arise other than "not master" error messages.
The bug was properly fixed and tested against to prevent regressions.

The discovery was made thanks to a new service being introduced by
MongoLabs, to be announced in the upcoming days.

- This release also brings down the replica set cluster synchronization
delay from 3 minutes to 30 seconds. This delay was and still is the
maximum delay, and there are events that may trigger such a
synchronization to happen sooner.

gustavo @ http://niemeyer.net
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
postedMay 13, '13 at 4:21p
activeMay 13, '13 at 4:21p

1 user in discussion

Piyush Verma: 1 post



site design / logo © 2023 Grokbase