控制在生成的Thrift代码中的Golang注释。

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

Control golang annotations in thrift generated code

问题

我可以帮你翻译。以下是要翻译的内容:

我有一个来自Thrift的消息,我希望能够将其序列化为JSON,并能够从JSON中反序列化,但我不希望生成的JSON键与生成的Go代码中的键匹配。

有没有办法控制Thrift生成的Go代码中结构体附加的注释?

英文:

I have a message from thrift that I would like to be able to serialize into and out of json, but I don't want the generated json keys to match what is in the generated go code.

Is there a way to control what annotations are attached to the struct in the go code that thrift generates?

答案1

得分: 4

修改我的先前答案-这是没有记录的,但是确实是可能的,我通过阅读编译器代码找到了它。

但是无论如何,在thrift的主分支(1.0-dev)中,可以使用go.tag注释来实现。

以下是thrift代码的示例:

struct foo {
  1: string bar (go.tag = "json:\"baz\" yo:\"dawg\""),
  2: string bang
}

生成以下Go代码:

type Foo struct {
        Bar  string `thrift:"bar,1" json:"baz" yo:"dawg"`
        Bang string `thrift:"bang,2" json:"bang"`
}
英文:

Scratch my previous answer - it's undocumented, but it IS possible, I found it by reading the compiler code. Bah.

But anyway, in thrift's master (1.0-dev), here's how it's done - using a go.tag annotation.

This piece of thrift code:

struct foo {
  1: string bar (go.tag = "json:\"baz\" yo:\"dawg\""),
  2: string bang
}

Generates the following Go code:

type Foo struct {
        Bar  string `thrift:"bar,1" json:"baz" yo:"dawg"`
        Bang string `thrift:"bang,2" json:"bang"`
}

huangapple
  • 本文由 发表于 2015年7月9日 21:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/31319534.html
匿名

发表评论

匿名网友

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

确定