英文:
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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论