默认情况下,一个 IDL 生成的 golang 代码会有 thrift、frugal、json_tag
在 IDL 定义的结构体 field 后增加 go.tag= “xxx” 的 annotation 后,对应内容会作为 go 语言结构体中对应字段的 tags。
当 IDL 中定义如下结构体时(注意用 \ 转义):
struct foo {
1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
}
生成的go语言结构体如下
type Foo struct {
Bar string `thrift:"bar,1,required" frugal:"1,required,string" some_tag:"some_tag_value"`
}
可以看到,原本的 thrift tag 和 frugal tag 依然存在,但其他的 tag(主要是 json tag)就变成了 go.tag 指定的内容。
JSON Tag 属于默认 tag ,一旦通过 go.tag 自定义 tag 后,原来的 JSON Tag 便不会再生成。
解决方案:
升级 thriftgo >=0.2.10,kitex 执行时带上 -thrift always_gen_json_tag
参数
# upgrade thriftgo
go install github.com/cloudwego/thriftgo@latest
# regenerate with the argument
kitex -thrift always_gen_json_tag -module $mod -service $svc xxx.thrift
在 go.tag 里把你需要的 json tag 也加上,例如:
// 可以把之前生成的 json tag 拷贝过来(注意转义)
struct foo {
1: required string Bar (go.tag = "json=\"Bar,omitempty\" some_tag:\"xxx\"")
}