Golang + MongoDB嵌入类型(将结构嵌入另一个结构中) [英] Golang + MongoDB embedded type (embedding a struct in another struct)

查看:644
本文介绍了Golang + MongoDB嵌入类型(将结构嵌入另一个结构中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,我运行一个API,当用户在用户资源上发出GET请求时,我将以JSON形式返回相关字段。 c $ c $>类型User结构{
Id bson.ObjectId`json:id,omitemptybson:_ id,omitempty`
名称字符串`json:name,omitemptybson:name ,omitempty`
秘密字符串`json: - ,omitemptybson:secret,omitempty`
}

正如您所看到的,User中的Secret字段具有 json: - 。这意味着在大多数我不想返回的操作中。在这种情况下,回应将是

  {
id:1,
Name :John
}

字段密钥不会以<$ c $的形式返回



现在,我打开一个管理员唯一的路线,在那里我想返回秘密领域。但是,这意味着需要重复User结构。



我现在的解决方案如下所示:

  type adminUser struct {
Id bson.ObjectId`json:id,omitemptybson:_ id,omitempty`
名称字符串`json:name,omitemptybson: name,omitempty`
秘密字符串`json:secret,omitemptybson:secret,omitempty`
}

有没有办法将User嵌入到adminUser中?有点像继承:

 类型adminUser结构{
User
秘密字符串`json:secret, omitemptybson:secret,omitempty`
}

以上目前不起作用,因为在这种情况下只会返回字段秘密。



注意:在实际的代码库中,只有几十个字段。因此,复制代码的成本很高。



实际的mongo查询如下:

  func getUser(w http.ResponseWriter,r * http.Request){
....省略代码...

var user adminUser
err:= common.GetDB(r).C (users)。Find(
bson.M {_ id:userId},
).One(&user)
if err!= nil {
return
}
common.ServeJSON(w,& user)
}


解决方案

您应该查看bson包的内联标志
(这在 bson.Marshal )。
它应该允许你做这样的事情:

 类型adminUser结构{
User`bson: ,inline`
秘密字符串`json:secret,omitemptybson:secret,omitempty`
}

但是,现在您会注意到,当您尝试从数据库读取此结构时获得重复键重错误

,因为 adminUser 用户包含密钥秘密



在你的情况下,我会从 User
中删除 Secret 字段,只有 adminUser 中的一个。
然后,无论何时您需要写入秘密字段,
都要确保使用 adminUser


Hypothetical, I run an API and when a user makes a GET request on the user resource, I will return relevant fields as a JSON

type User struct {
  Id      bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
  Name    string        `json:"name,omitempty" bson:"name,omitempty"`
  Secret  string        `json:"-,omitempty" bson:"secret,omitempty"`
}

As you can see, the Secret field in User has json:"-". This implies that in most operation that I would not like to return. In this case, a response would be

{
  "id":1,
  "Name": "John"
}

The field secret will not be returned as json:"-" omits the field.

Now, I am openning an admin only route where I would like to return the secret field. However, that would mean duplicating the User struct.

My current solution looks like this:

type adminUser struct {      
  Id      bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
  Name    string        `json:"name,omitempty" bson:"name,omitempty"`
  Secret  string        `json:"secret,omitempty" bson:"secret,omitempty"`
}

Is there a way to embed User into adminUser? Kind of like inheritance:

type adminUser struct {      
  User
  Secret  string        `json:"secret,omitempty" bson:"secret,omitempty"`
}

The above currently does not work, as only the field secret will be returned in this case.

Note: In the actual code base, there are few dozens fields. As such, the cost of duplicating code is high.

The actual mongo query is below:

func getUser(w http.ResponseWriter, r *http.Request) {
  ....omitted code...

  var user adminUser
  err := common.GetDB(r).C("users").Find(
      bson.M{"_id": userId},
  ).One(&user)
  if err != nil {
      return
  }
  common.ServeJSON(w, &user)
}

解决方案

You should take a look at the bson package's inline flag (that is documented under bson.Marshal). It should allow you to do something like this:

type adminUser struct {
    User `bson:",inline"`
    Secret string `json:"secret,omitempty" bson:"secret,omitempty"`
}

However, now you'll notice that you get duplicate key errors when you try to read from the database with this structure, since both adminUser and User contain the key secret.

In your case I would remove the Secret field from User and only have the one in adminUser. Then whenever you need to write to the secret field, make sure you use an adminUser.

这篇关于Golang + MongoDB嵌入类型(将结构嵌入另一个结构中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆