为什么mgo无法正确解析我的结构体?

huangapple go评论88阅读模式
英文:

Why won't mgo unmarshall my struct properly?

问题

之前我发布了这个问题,询问如何在Go中使用mgo编写自定义的BSON编组/解组。现在我来测试它,我觉得我遇到了一个更大的问题。我的所有结构体都解组为nil值。

这是我的货币结构体,其中包含了bson.Getter和bson.Setter的实现:

type Currency struct {
    value        decimal.Decimal // 货币的实际值。
    currencyCode string          // ISO货币代码。
}

/*
GetBSON实现了bson.Getter接口。
*/
func (c Currency) GetBSON() (interface{}, error) {
    f, _ := c.Value().Float64()
    return bson.Marshal(struct {
        Value        float64 `json:"value" bson:"value"`
        CurrencyCode string  `json:"currencyCode" bson:"currencyCode"`
    }{
        Value:        f,
        CurrencyCode: c.currencyCode,
    })
}

/*
SetBSON实现了bson.Setter接口。
*/
func (c *Currency) SetBSON(raw bson.Raw) error {
    decoded := new(struct {
        Value        float64 `json:"value" bson:"value"`
        CurrencyCode string  `json:"currencyCode" bson:"currencyCode"`
    })

    fmt.Println(string(raw.Data))
    bsonErr := raw.Unmarshal(decoded)

    if bsonErr == nil {
        fmt.Println("Debug: no error returned.")
        fmt.Println(decoded)
        c.value = decimal.NewFromFloat(decoded.Value)
        c.currencyCode = decoded.CurrencyCode
        return nil
    } else {
        return bsonErr
    }
}

通过查看原始数据,它正确地进行了编组,但是在解组时,得到的结构体为空。你有什么想法,我在哪里出错了?我昨天刚刚使用go get gopkg.in/mgo.v2命令,所以我希望它是最新的,而且不会出现这样的错误,因为它是“最热门的MongoDB驱动程序”。

英文:

Earlier I posted this question asking about writing custom BSON marshalling/unmarshalling in Go using mgo. Now I've come to test it I think I've hit on a bigger problem. All my structs unmarshal to nil values.

This is my currency struct with the implementations of bson.Getter and bson.Setter:

type Currency struct {
	value        decimal.Decimal //The actual value of the currency.
	currencyCode string          //The ISO currency code.
}

/*
GetBSON implements bson.Getter.
*/
func (c Currency) GetBSON() (interface{}, error) {
	f, _ := c.Value().Float64()
	return bson.Marshal(struct {
		Value        float64 `json:"value" bson:"value"`
		CurrencyCode string  `json:"currencyCode" bson:"currencyCode"`
	}{
		Value:        f,
		CurrencyCode: c.currencyCode,
	})
}

/*
SetBSON implements bson.Setter.
*/
func (c *Currency) SetBSON(raw bson.Raw) error {
	decoded := new(struct {
		Value        float64 `json:"value" bson:"value"`
		CurrencyCode string  `json:"currencyCode" bson:"currencyCode"`
	})

	fmt.Println(string(raw.Data))
	bsonErr := raw.Unmarshal(decoded)

	if bsonErr == nil {
		fmt.Println("Debug: no error returned.")
		fmt.Println(decoded)
		c.value = decimal.NewFromFloat(decoded.Value)
		c.currencyCode = decoded.CurrencyCode
		return nil
	} else {
		return bsonErr
	}
}

By looking at the raw data, it marshals correctly, but when unmarshaling the resulting struct is just empty. Any ideas where I'm going wrong here? I used the go get gopkg.in/mgo.v2command literally yesterday so I would hope it was up to date and a bug like this wouldn't be present in "the hottest MongoDB driver around".

答案1

得分: 4

GetBSON 方法应该返回要进行编组的值,而不是编组后生成的二进制数据。这就是为什么它的第一个结果类型是 interface{} 而不是 []byte

英文:

The GetBSON method should return the value to be marshaled, not the binary data resulting from marshaling it. That's why its first result type is interface{} and not []byte.

huangapple
  • 本文由 发表于 2015年6月17日 23:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/30895854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定