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

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

Why won't mgo unmarshall my struct properly?

问题

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

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

  1. type Currency struct {
  2. value decimal.Decimal // 货币的实际值。
  3. currencyCode string // ISO货币代码。
  4. }
  5. /*
  6. GetBSON实现了bson.Getter接口。
  7. */
  8. func (c Currency) GetBSON() (interface{}, error) {
  9. f, _ := c.Value().Float64()
  10. return bson.Marshal(struct {
  11. Value float64 `json:"value" bson:"value"`
  12. CurrencyCode string `json:"currencyCode" bson:"currencyCode"`
  13. }{
  14. Value: f,
  15. CurrencyCode: c.currencyCode,
  16. })
  17. }
  18. /*
  19. SetBSON实现了bson.Setter接口。
  20. */
  21. func (c *Currency) SetBSON(raw bson.Raw) error {
  22. decoded := new(struct {
  23. Value float64 `json:"value" bson:"value"`
  24. CurrencyCode string `json:"currencyCode" bson:"currencyCode"`
  25. })
  26. fmt.Println(string(raw.Data))
  27. bsonErr := raw.Unmarshal(decoded)
  28. if bsonErr == nil {
  29. fmt.Println("Debug: no error returned.")
  30. fmt.Println(decoded)
  31. c.value = decimal.NewFromFloat(decoded.Value)
  32. c.currencyCode = decoded.CurrencyCode
  33. return nil
  34. } else {
  35. return bsonErr
  36. }
  37. }

通过查看原始数据,它正确地进行了编组,但是在解组时,得到的结构体为空。你有什么想法,我在哪里出错了?我昨天刚刚使用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:

  1. type Currency struct {
  2. value decimal.Decimal //The actual value of the currency.
  3. currencyCode string //The ISO currency code.
  4. }
  5. /*
  6. GetBSON implements bson.Getter.
  7. */
  8. func (c Currency) GetBSON() (interface{}, error) {
  9. f, _ := c.Value().Float64()
  10. return bson.Marshal(struct {
  11. Value float64 `json:"value" bson:"value"`
  12. CurrencyCode string `json:"currencyCode" bson:"currencyCode"`
  13. }{
  14. Value: f,
  15. CurrencyCode: c.currencyCode,
  16. })
  17. }
  18. /*
  19. SetBSON implements bson.Setter.
  20. */
  21. func (c *Currency) SetBSON(raw bson.Raw) error {
  22. decoded := new(struct {
  23. Value float64 `json:"value" bson:"value"`
  24. CurrencyCode string `json:"currencyCode" bson:"currencyCode"`
  25. })
  26. fmt.Println(string(raw.Data))
  27. bsonErr := raw.Unmarshal(decoded)
  28. if bsonErr == nil {
  29. fmt.Println("Debug: no error returned.")
  30. fmt.Println(decoded)
  31. c.value = decimal.NewFromFloat(decoded.Value)
  32. c.currencyCode = decoded.CurrencyCode
  33. return nil
  34. } else {
  35. return bsonErr
  36. }
  37. }

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:

确定