Mayank Patel
Mayank Patel

Reputation: 8536

Parsing mongo entries into struct

I have mongo database with following schema

{ 
  "_id" : ObjectId("55c8526d8c16598efb5ee1e6"), 
  "guid" : "72811d52b48379e72c8fdd11aa09cb8b", 
  "blkid" : 1, 
  "vblkid" : 0, 
  "spltid" : 0, 
  "cmpr" : false, 
  "encr" : false,
  "chksum" : "",
  "dup" : false,
  "cid" : 1,
  "off" : 524508,
  "len" : 524408,
  "incr" : 0,
  "fBackupID" : 0,
  "vid" : 0,
  "plugInType" : 0, 
  "blkType" : 0, 
  "alen" : 0 
}

and I am trying to parse these into a struct with following structure:

type VhfsBlockMD struct {
    GUID       string `json:"guid"`
    BlkID      int    `bson:",minsize" json:"blkid"`
    VBlkID     int    `bson:",minsize" json:"vblkid"`
    SpltID     int    `bson:",minsize" json:"spltid"`
    Cmpr       bool   `json:"cmpr" `
    Encr       bool   `json:"encr"`
    Blksum     string `bson:"blksum,omitempty" json:"blksum,omitempty"`
    Chksum     string `json:"chksum"`
    Dup        bool   `json:"dup"`
    Cid        int    `bson:",minsize" json:"cid"`
    SplitLen   int    `bson:",minsize" json:"len"`
    Off        int64  `bson:",minsize" json:"off"`
    Incr       int    `bson:",minsize" json:"incr"`
    CDup       bool   `bson:"cdup,omitempty" json:"cdup,omitempty"`
    FBackupID  int    `bson:"fBackupID" json:"fBackupID"`
    Vid        int    `bson:"vid" json:"vid"`
    PlugInType int    `bson:"plugInType" json:"plugInType"`
    BlkType    int    `bson:"blkType" json:"blkType"`
    Alen       int    `bson:"alen" json:"alen"`
    IsValid    int    `bson:"-" json:"-"`
    Len        uint64 `bson:"-" json:"-"`
}

I am using mgo driver.

Now problem is that after parsing only attribute i am not able to parse correctly is "len" (SplitLen in go struct).

len is defined as

SplitLen int `bson:",minsize" json:"len"`

I believe it has something to do with tags. Also i would lke to mention that same struct was used to insert value into mongodb.

Any help would be appreciated.

Upvotes: 3

Views: 184

Answers (2)

icza
icza

Reputation: 417462

If a data element appears under a different name in the other representation (e.g. json text or database) than what the struct field name is, you have to tell what name to match to the struct field in the field tag.

You told the json package to get/set the json value "len" into the field SplitLen which is named differently by including this in its tag: json:"len".

But you haven't told the mongo driver to also use this field which is most likely named "Len" (or "len") in your mongodb. You explicitly excluded the field that could be "auto-matched" by name:

Len uint64 `bson:"-" json:"-"`

As Ainar-G suggested, you can designate the field by adding the "len" to the bson tag value which will force the mgo driver to also use the SplitLen field:

SplitLen int `bson:"len,minsize" json:"len"`

And now I don't see any purpose for the Len field, you should remove it to avoid confusion, or use the Len name instead of SplitLen:

Len int `bson:"len,minsize" json:"len"`

Upvotes: 2

Ainar-G
Ainar-G

Reputation: 36189

Add the field name to the BSON tag as well:

SplitLen int `bson:"len,minsize" json:"len"

Otherwise it seems that it will conflict with the Len field that is ignored.

Upvotes: 1

Related Questions