dtqi87613 2016-10-12 06:54
浏览 657
已采纳

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?

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了