In Go Lang Set the Tag for a Whole Struct

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

In Go Lang Set the Tag for a Whole Struct

问题

我正在使用Go和GoRestful来编写一个RESTful前端,用于访问存储在Google App Engine Datastore中的实体。

数据被转换为JSON/XML,并通过标签控制每种格式的样式来呈现给用户。我如何将标签应用于结构体名称本身,以便以正确的样式输出?

我的结构体示例如下:

type Shallow struct {
    Key          string    `datastore:"-" json:"key" xml:"key"`
    LastModified time.Time `json:"last_modified" xml:"last-modified"`
    Version      int       `json:"version" xml:"version"`
    Status       int       `json:"status" xml:"status"`
    Link         Link      `datastore:"-" json:"link" xml:"link"`
    Name         string    `json:"name" xml:"name"`
}

type ProbabilityEntry struct {
    ItemId      int64   `datastore:"ItemId" json:"item_id" xml:"item-id"`
    Probability float32 `datastore:"Probability" json:"probability" xml:"probability"`
    Quantity    int16   `datastore:"Quantity" json:"quantity" xml:"quantity"`
}

type LootTable struct {
    Shallow
    AllowPreload  bool               `json:"allow_preload" xml:"allow-preload"`
    Probabilities []ProbabilityEntry `json:"probabilities" xml:"probabilities"`
}

当LootTable结构体转换为JSON/XML时,它应该以'loot_table'或'loot-table'的形式表示自己,而不是'LootTable'。

英文:

I'm using Go and GoRestful to program a RESTFUL front end to some entities stored on Google App Engine Datastore.

The data is turned into JSON/XML and presented to the user with tags controlling the style for each format. How can I also apply tags to the name of the struct itself so it is output using the correct style?

An example of my structs would be:

type Shallow struct {
	Key          string    `datastore:"-" json:"key" xml:"key"`
	LastModified time.Time `json:"last_modified" xml:"last-modified"`
	Version      int       `json:"version" xml:"version"`
	Status       int       `json:"status" xml:"status"`
	Link         Link      `datastore:"-" json:"link" xml:"link"`
	Name         string    `json:"name" xml:"name"`
}

type ProbabilityEntry struct {
	ItemId      int64   `datastore:"ItemId" json:"item_id" xml:"item-id"`
	Probability float32 `datastore:"Probability" json:"probability" xml:"probability"`
	Quantity    int16   `datastore:"Quantity" json:"quantity" xml:"quantity"`
}

type LootTable struct {
	Shallow
	AllowPreload  bool               `json:"allow_preload" xml:"allow-preload"`
	Probabilities []ProbabilityEntry `json:"probabilities" xml:"probabilities"`
}

When the LootTable struct emits to JSON/XML it should represent itself as 'loot_table' or 'loot-table' rather than 'LootTable'.

答案1

得分: 4

简单回答:

将其包装在外部结构体中:

type Payload struct {
   Loot LootTable `json:"loot_table"`
}

更详细的回答:

如果JSON的接收方知道他们将要接收的内容,那么这并不是必需的。然而,在构建JSON API时,我经常创建一个Response结构体,其中包含有关请求的额外细节,其中可能包括响应的类型。以下是一个示例:

type JSONResponse struct {
   Obj    interface{} `json:"obj"`    // 未包装的JSON
   Type   string      `json:"type"`   // 例如:"loot_table"
   Ok     bool        `json:"ok"`     // 此响应是否需要错误处理?
   Errors []string    `json:"errors"` // 任何错误,您可以省略Ok字段,只需检查此字段即可
}

同样,对于API调用,通常您知道您期望的内容,但如果响应可能是多种类型之一,这种方法可以帮助。

英文:

Simple answer:

Wrap it in an outer struct:

type Payload struct {
   Loot LootTable `json:"loot_table"`
}

Longer answer:

If the receiver of the JSON knows what they're getting then this is not really necessary. However, when building a JSON API I often create a Response struct that contains extra details regarding the request, that could include the type of the response. Here is an example:

type JSONResponse struct {
   Obj    interface{} `json:"obj"`    // Marshall'ed JSON (not wrapped)
   Type   string      `json:"type"`   // "loot_table" for example
   Ok     bool        `json:"ok"`     // Does this response require error handling?
   Errors []string    `json:"errors"` // Any errors, you could leave out Ok and just check this
}

Again, with API calls you usually know what you're expecting, but if the response could be one of a number of types this approach can help.

huangapple
  • 本文由 发表于 2014年1月5日 17:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/20932231.html
匿名

发表评论

匿名网友

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

确定