diff --git a/content/en/docs/kitex/Tutorials/code-gen/struct_reference_generator.md b/content/en/docs/kitex/Tutorials/code-gen/struct_reference_generator.md index 716a9f5889..67514373cf 100644 --- a/content/en/docs/kitex/Tutorials/code-gen/struct_reference_generator.md +++ b/content/en/docs/kitex/Tutorials/code-gen/struct_reference_generator.md @@ -7,22 +7,35 @@ description: "Use Kitex tool to generate structs reference to solve public struc --- ## Usage Scenarios + For kitex_gen structs generated by the same IDL, if they are placed in different git repo, the type will be inconsistent. Some business teams will have an independent structs git repo for the public IDL to store these generated codes. The kitex tool can directly generate references to the remote repo for a specific IDL without actually generating a struct in the code generation stage, thereby solving the situation of type inconsistency. ## Practice + First make sure: + - The kitex tool version is not lower than v0.4.4 -- thriftgo version not lower than v0.2.4 +- thriftgo version not lower than v0.3.0 Create a file called idl-ref.yml to configure the remote references you want to use for a particular IDL: + ```yaml - ref: +ref: idl/base.thrift: "github.com/xxxx/public_repo/base" idl/public/item.thrift: "github.com/xxxx/public_repo/item" ``` -After the kitex command is normally executed in the directory with idl-ref.yml, the specified IDL will not generate a complete serialization and deserialization code after execution, but will generate a remote reference to the structs, thereby solving the common Library structure conflict problem. + +After executing the `kitex` command with the additional parameter `-thrift code_ref` in a directory with the idl-ref.yml file, the specified IDL will not generate a complete serialization and deserialization code after execution, but will generate a remote reference to the structs, thereby solving the common Library structure conflict problem. + +Complete example command. + +```bash +kitex -module example -service example-server -thrift code_ref idl/base.thrift +``` + An example of the generated codes is as follows: + ```go // Code generated by thriftgo (0.2.4). DO NOT EDIT. @@ -34,11 +47,11 @@ import ( type Base = base.Base -var NewBase = base. NewBase +var NewBase = base.NewBase -type BaseResp = base. BaseResp +type BaseResp = base.BaseResp -var NewBaseResp = base. NewBaseResp +var NewBaseResp = base.NewBaseResp const MY_CONST = base.MY_CONST diff --git a/content/zh/docs/kitex/Tutorials/code-gen/struct_reference_generator.md b/content/zh/docs/kitex/Tutorials/code-gen/struct_reference_generator.md index 2c7c3d5b70..80350a63cb 100644 --- a/content/zh/docs/kitex/Tutorials/code-gen/struct_reference_generator.md +++ b/content/zh/docs/kitex/Tutorials/code-gen/struct_reference_generator.md @@ -7,22 +7,35 @@ description: "使用 Kitex 命令行生成结构体引用,解决公共结构 --- ## 使用场景 + 对于同一份 IDL 生成的 kitex_gen 结构体,如果分别放到不同的仓库后,会出现类型不一致的情况。 有的业务团队会对公共的 IDL 有独立的结构体仓库来存放这些生成产物。kitex 命令行可以在生成代码阶段直接对特定的 IDL 不进行实际的结构体生成,而是向远端做引用,从而解决类型不一致的情况。 ## 使用方式 + 首先确保: + - kitex 命令行版本不低于 v0.4.4 -- thriftgo 版本不低于 v0.2.4 +- thriftgo 版本不低于 v0.3.0 创建一个名为 idl-ref.yml 的文件,对特定的 IDL 配置你想使用的远端引用: + ```yaml - ref: - idl/base.thrift: "github.com/xxxx/public_repo/base" - idl/public/item.thrift: "github.com/xxxx/public_repo/item" +ref: + idl/base.thrift: "github.com/xxxx/public_repo/base" + idl/public/item.thrift: "github.com/xxxx/public_repo/item" +``` + +在有 idl-ref.yml 的目录下正常执行 kitex 命令并传递额外参数 `-thrift code_ref` 后,这部分指定的 IDL 在执行后不会生成完整的序列化反序列化代码,而是对结构体生成一个远端引用,从而解决公共库结构体冲突问题。 + +完整的示例命令: + +```bash +kitex -module example -service example-server -thrift code_ref idl/base.thrift ``` -在有 idl-ref.yml 的目录下正常执行 kitex 命令后,这部分指定的 IDL 在执行后不会生成完整的序列化反序列化代码,而是对结构体生成一个远端引用,从而解决公共库结构体冲突问题。 + 生成产物的一个例子如下: + ```go // Code generated by thriftgo (0.2.4). DO NOT EDIT.