dtqi87613
2016-10-12 06:54
浏览 432

Golang Bson结构-对JSON中的单个字段使用多个字段名称,但仅将一个字段用于写入数据库

I have a struct like this -

type Address struct {
    AddressLine1 string        `json:"addressLine1" bson:"addressLine1"`
    AddressLine2 string        `json:"addressLine2" bson:"addressLine2"`
    Landmark     string        `json:"landmark" bson:"landmark"`
    Zipcode      string        `json:"zipcode" bson:"zipcode"`
    City         string        `json:"city" bson:"city"`
}

Due to some compatibility issues between the previous build and the latest yet-to-be-released build, I want to make sure that if someone posts json data that decodes using this struct they should be able to use either 'zipcode' or 'pincode' as the field name in their json. But when this value is written to my database, the field name should only be 'zipcode'.

In short,

{
"city": "Mumbai",
"zipcode": "400001"
}

or

{
"city": "Mumbai",
"pincode": "400001"
}

should both appear inside the database as -

{
"city": "Mumbai",
"zipcode": "400001"
}

How do I allow this?

图片转代码服务由CSDN问答提供 功能建议

我有一个像这样的结构-

  type地址结构 {
 AddressLine1字符串`json:“ addressLine1” bson:“ addressLine1”`
 AddressLine2字符串`json:“ addressLine2” bson:“ addressLine2”`
地标字符串`json:“ landmark” bson:“ landmark”`\  n邮政编码字符串`json:“ zipcode” bson:“邮政编码” 
城市字符串`json:“ city” bson:“ city”`
} 
   
 
 

由于以前的版本与最新的尚未发布版本之间存在兼容性问题,因此我想确保,如果有人发布使用此结构解码的json数据,则他们应该能够使用“邮政编码”或“ 密码”作为其json中的字段名称。 但是,当将此值写入我的数据库时,字段名称只能是“邮政编码”。

简而言之,

  {\  n“ city”:“孟买”,
“邮政编码”:“ 400001” 
} 
   
 
 

   {
“ city”:“孟买”,
“ pincode”:“ 400001” 
} 
   
 
 

都应以如下形式出现在数据库中 -

  {
“ city”:“孟买”,
“邮政编码”:“ 400001” 
} 
   
  
 

如何允许这个?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbin3021 2016-10-12 07:17
    已采纳

    You can have both fields as pointer to string:

    type Address struct {
        AddressLine1 string        `json:"addressLine1" bson:"addressLine1"`
        AddressLine2 string        `json:"addressLine2" bson:"addressLine2"`
        Landmark     string        `json:"landmark" bson:"landmark"`
        Zipcode      *string       `json:"zipcode,omitempty" bson:"zipcode"`
        Pincode      *string       `json:"pincode,omitempty" bson:"zipcode"`
        City         string        `json:"city" bson:"city"`
    }
    

    As you may note we're using omitempty in the json tag, so if one of the fields is not present in the json it will be ignored as a nil pointer and it will not be present after Marshal() or Unmarshal()

    Edit:

    In this case all we have to do is implement the method UnmarshalJSON([]byte) error to satisfy the interface Unmarshaler, the method json.Unmarshal() will always try to call that method and we can add our own logic after Unmarshal the struct, in this case we want to know if pincode is settled if it's we assigned to zipcode: full example here: https://play.golang.org/p/zAOPMtCwBs

    type Address struct {
        AddressLine1 string  `json:"addressLine1" bson:"addressLine1"`
        AddressLine2 string  `json:"addressLine2" bson:"addressLine2"`
        Landmark     string  `json:"landmark" bson:"landmark"`
        Zipcode      *string `json:"zipcode,omitempty" bson:"zipcode"`
        Pincode      *string `json:"pincode,omitempty"`
        City         string  `json:"city" bson:"city"`
    }
    
    // private alias of Address to avoid recursion in UnmarshalJSON()
    type address Address
    
    func (a *Address) UnmarshalJSON(data []byte) error {
        b := address{}
        if err := json.Unmarshal(data, &b); err != nil {
            return nil
        }
        *a = Address(b) // convert the alias to address
    
        if a.Pincode != nil && a.Zipcode == nil {
            a.Zipcode = a.Pincode
            a.Pincode = nil // unset pincode
        }
    
        return nil
    }
    

    Note that the field Zipcode has a bson tag and Pincode not, also we have to create an alias of type address to avoid calling UnmarshalJSON recursively

    打赏 评论

相关推荐 更多相似问题