Skip to content

Commit

Permalink
docs: update struct_reference_generator (#894)
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <rogers@rogerogers.com>
  • Loading branch information
rogerogers authored Dec 14, 2023
1 parent a7c8e25 commit eb7e239
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

1 comment on commit eb7e239

@vercel
Copy link

@vercel vercel bot commented on eb7e239 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.