如何在Golang中响应所有字段,包括带有omitempty标签的字段?

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

How to response all field include field with tag omitempty in Golang?

问题

在我的Web服务中,我有一个模型:

// Comment结构体
type Comment struct {
Owner UserObject json:"owner"
ID int64 json:"id"
Message string json:"message"
Mentions []MentionObject json:"mentions,omitempty"
CreatedAt int64 json:"created_at,omitempty"
UpdatedAt int64 json:"updated_at,omitempty"
Status int json:"status,omitempty"
CanEdit bool json:"can_edit"
CanDelete bool json:"can_delete"
}

// UserObject结构体
type UserObject struct {
ID int64 json:"id"
Username string json:"username"
FullName string json:"full_name"
Avatar string json:"avatar"
}

// MentionObject结构体
type MentionObject struct {
ID int64 json:"id"
Length int64 json:"length"
Offset int64 json:"offset"
}

我使用gin gonic进行路由:

routes.GET("/user", func(c *gin.Context) {
c.JSON(200, Comment{})
})

我需要返回这个结构体的所有字段,我知道它将返回一个JSON:

{
"owner": {
"id": 0,
"username": "",
"full_name": "",
"avatar": ""
},
"id": 0,
"message": "",
"can_report": false,
"can_edit": false,
"can_delete": false
}

我知道这是正确的,但我仍然想要返回所有字段。怎么做呢?

英文:

In my Webservice, I have a model:

// Comment struct
type Comment struct {
	Owner            UserObject      `json:"owner"`
	ID               int64           `json:"id"`
	Message          string          `json:"message"`
	Mentions         []MentionObject `json:"mentions,omitempty"`
	CreatedAt        int64           `json:"created_at,omitempty"`
	UpdatedAt        int64           `json:"updated_at,omitempty"`
	Status           int             `json:"status,omitempty"`
	CanEdit          bool            `json:"can_edit"`
	CanDelete        bool            `json:"can_delete"`
}


// UserObject struct
type UserObject struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
	FullName string `json:"full_name"`
	Avatar   string `json:"avatar"`
}

// MentionObject struct
type MentionObject struct {
	ID     int64 `json:"id"`
	Length int64 `json:"length"`
	Offset int64 `json:"offset"`
}

I have use gin gonic to routing

routes.GET("/user", func(c *gin.Context) {
		c.JSON(200, Comment{})
	})

I need return all fields of this struct, I know that It going to response a json:

{
  "owner": {
    "id": 0,
    "username": "",
    "full_name": "",
    "avatar": ""
  },
  "id": 0,
  "message": "",
  "can_report": false,
  "can_edit": false,
  "can_delete": false
}

I know that This is right, but I still want response all of field.
How to do that?

答案1

得分: 3

如果您不想从标签中删除omitempty值,因为您需要它用于其他目的,您可以在Go 1.8+上定义一个与要序列化的类型相同但没有omitempty标签值的新type,然后简单地将旧类型的值转换为新类型。

这是一个1.8+“忽略标签”的类型转换示例

您还可以定义一个仅包含原始类型中省略字段的新类型,然后在新类型中嵌入原始类型如下所示

英文:

If you don't want to drop the omitempty value from the tag because you need it for some other purpose, you can either, if you're on Go 1.8+, define a new type identical to the one you want to serialize but without the omitempty tag values and then simply convert a value from the old type to the new type.

Here's an example of the 1.8+ "tag ignoring" type conversion

You can also define a new type with only those fields that are omitted in the original and then embed the original in the new type like so.

huangapple
  • 本文由 发表于 2017年5月25日 20:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/44180847.html
匿名

发表评论

匿名网友

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

确定