Generate Structs Reference

Use Kitex tool to generate structs reference to solve public structs problem.

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.3.0

Create a file called idl-ref.yml to configure the remote references you want to use for a particular IDL:

ref:
  idl/base.thrift: "github.com/xxxx/public_repo/base"
  idl/public/item.thrift: "github.com/xxxx/public_repo/item"

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.

kitex -module example -service example-server -thrift code_ref idl/base.thrift

An example of the generated codes is as follows:

// Code generated by thriftgo (0.2.4). DO NOT EDIT.

package base

import (
    base "github.com/xxxx/public_repo/base"
)

type Base = base.Base

var NewBase = base.NewBase

type BaseResp = base.BaseResp

var NewBaseResp = base.NewBaseResp

const MY_CONST = base.MY_CONST

var MAP_CONST = base.MAP_CONST


Last modified January 18, 2024 : Upload volo blog (#936) (1fc8abb)