MongoDB在存储之前不会对值进行编组

时间:2016-10-19 18:09:44

标签: go bson mgo

我正在使用自定义JSON marshaller / unmarshaller来实现Go中整数和字符串之间的映射。问题是值作为整数而不是字符串存储在数据库中。在下面的示例中,我希望将其存储在MongoDB数据库中:

{ "_id" : "id123", "desc" : "Red Delicious", "value" : "apple" }

相反,我得到:

{ "_id" : "id123", "desc" : "Red Delicious", "value" : 1 }

如测试所示,编组和解组工作正常。发生了什么事?

以下是Go测试的示例(保存到unmarshal_test.go和“go test”)。

package testunmarshal

import (
    "fmt"
    "testing"
    "encoding/json"
    mgo "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Const int

const (
    Apple Const = 1
    Banana = 2
    Cherry = 4
)

type Record struct {
    Id string `bson:"_id" json:"id"`
    Desc string `bson:"desc" json:"desc"`
    Value Const `bson:"value" json:"value`
}

func (intValue Const) Code() string {
    switch intValue {
    case Apple: return "apple"
    case Banana: return "banana"
    case Cherry: return "cherry"
    }
    return "invalid"
}

func (intValue *Const) UnmarshalJSON(data []byte) (err error) {
    switch string(data) {
    case `"apple"`: 
        *intValue = Apple
    case `"banana"`:
        *intValue = Banana
    case `"cherry"`:
        *intValue = Cherry
    default:
        return fmt.Errorf("Invalid fruit %s", data)
    }
    return nil
}

func (intValue *Const) MarshalJSON() ([]byte, error) {
    return []byte(fmt.Sprintf(`"%s"`, intValue.Code())), nil
}

func TestMarshalJSON(t *testing.T) {
    var orig = Record {
        Id: "id456",
        Desc: "Cavendish",
        Value: Banana,
    }

    var copy Record

    bytes, err := json.Marshal(&orig)

    if err != nil {
        t.Errorf("Marshal failed: %s", err.Error())
        return
    }

    err = json.Unmarshal(bytes, &copy)

    if err != nil {
        t.Errorf("Unmarshal failed: %s", err.Error())
        return
    }

    if orig.Value != copy.Value {
        t.Errorf("Expected %d=%s, got %d=%s", orig.Value, orig.Value.Code(), copy.Value, copy.Value.Code())
    }
}

func TestMarshalBSON(t *testing.T) {
    var orig = Record {
        Id: "id456",
        Desc: "Cavendish",
        Value: Banana,
    }

    var copy Record

    bytes, err := bson.Marshal(&orig)

    if err != nil {
        t.Errorf("Marshal failed: %s", err.Error())
        return
    }

    err = bson.Unmarshal(bytes, &copy)

    if err != nil {
        t.Errorf("Unmarshal failed: %s", err.Error())
        return
    }

    if orig.Value != copy.Value {
        t.Errorf("Expected %d=%s, got %d=%s", orig.Value, orig.Value.Code(), copy.Value, copy.Value.Code())
    }
}

func TestMongo(t *testing.T) {
    var rec1 = Record {
        Id: "id123",
        Desc: "Red Delicious",
        Value: Apple,
    }
    var rec2 Record

    sess, err := mgo.Dial("localhost")

    if err != nil {
        t.Errorf(err.Error())
        return
    }

    db := sess.DB("test")

    if db == nil {
        t.Fatal("Failed to connect to database")
        return
    }

    col := db.C("fruit")

    if col == nil {
        t.Fatal("Failed to open collection")
        return
    }

    // defer col.DropCollection()

    err = col.Insert(&rec1)

    if err != nil {
        t.Fatal("Failed to insert: %s", err.Error())
        return
    }

    err = col.Find(bson.M{}).One(&rec2)

    if err != nil {
        t.Fatal("Failed to retrieve stored object: %s", err.Error())
        return
    }

    if rec1.Value != rec2.Value {
        t.Errorf("Expected %d=%s, got %d=%s", rec1.Value, rec1.Value.Code(), rec1.Value, rec2.Value.Code())
    }
}

修改:添加了更多测试,以证明编组和解组工作正常。

1 个答案:

答案 0 :(得分:1)

bson编码器不使用JSON编组接口。实现Getter接口:

func (intValue Const) GetBSON() (interface{}, error) {
    return intValue.Code(), nil
}

您还需要实现Setter界面。

func (intValue *Const) SetBSON(raw bson.Raw) error {
    var data int
    if err := raw.Unmarshal(&data); err != nil {
        return err
    }
    switch data {
    case `"apple"`: 
        *intValue = Apple
    case `"banana"`:
        *intValue = Banana
    case `"cherry"`:
        *intValue = Cherry
    default:
        return fmt.Errorf("Invalid fruit %s", data)
    }
    return nil
}