Skip to content

Commit

Permalink
feat: struct reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Sep 6, 2023
1 parent 3f1f095 commit a0202b9
Show file tree
Hide file tree
Showing 22 changed files with 3,058 additions and 0 deletions.
77 changes: 77 additions & 0 deletions hz/struct_reuse/protobuf/api.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
syntax = "proto2";

package api;

import "google/protobuf/descriptor.proto";

option go_package = "a/b/c/kitex_gen/api";

extend google.protobuf.FieldOptions {
optional string raw_body = 50101;
optional string query = 50102;
optional string header = 50103;
optional string cookie = 50104;
optional string body = 50105;
optional string path = 50106;
optional string vd = 50107;
optional string form = 50108;
optional string js_conv = 50109;
optional string file_name = 50110;
optional string none = 50111;

// 50131~50160 used to extend field option by hz
optional string form_compatible = 50131;
optional string js_conv_compatible = 50132;
optional string file_name_compatible = 50133;
optional string none_compatible = 50134;
// 50135 is reserved to vt_compatible
// optional FieldRules vt_compatible = 50135;

optional string go_tag = 51001;
}

extend google.protobuf.MethodOptions {
optional string get = 50201;
optional string post = 50202;
optional string put = 50203;
optional string delete = 50204;
optional string patch = 50205;
optional string options = 50206;
optional string head = 50207;
optional string any = 50208;
optional string gen_path = 50301; // The path specified by the user when the client code is generated, with a higher priority than api_version
optional string api_version = 50302; // Specify the value of the :version variable in path when the client code is generated
optional string tag = 50303; // rpc tag, can be multiple, separated by commas
optional string name = 50304; // Name of rpc
optional string api_level = 50305; // Interface Level
optional string serializer = 50306; // Serialization method
optional string param = 50307; // Whether client requests take public parameters
optional string baseurl = 50308; // Baseurl used in ttnet routing
optional string handler_path = 50309; // handler_path specifies the path to generate the method

// 50331~50360 used to extend method option by hz
optional string handler_path_compatible = 50331; // handler_path specifies the path to generate the method
optional string custom = 55556; // deprecated
}

extend google.protobuf.EnumValueOptions {
optional int32 http_code = 50401;

// 50431~50460 used to extend enum option by hz
}

extend google.protobuf.ServiceOptions {
optional string base_domain = 50402;

// 50731~50760 used to extend service option by hz
optional string base_domain_compatible = 50731;
optional string service_path = 50732;
}

extend google.protobuf.MessageOptions {
// optional FieldRules msg_vt = 50111;

optional string reserve = 50830;
// 550831 is reserved to msg_vt_compatible
// optional FieldRules msg_vt_compatible = 50831;
}
22 changes: 22 additions & 0 deletions hz/struct_reuse/protobuf/hello/hello.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// idl/hello/hello.proto
syntax = "proto3"; // proto2/3 语法均可,推荐 proto3

package hello;

option go_package = "a/b/c/kitex_gen/hello";

import "api.proto";

message HelloReq {
string Name = 1;
}

message HelloResp {
string RespBody = 1;
}

service HelloService {
rpc Method1(HelloReq) returns(HelloResp) {
option (api.get) = "/api/v1";
}
}
20 changes: 20 additions & 0 deletions hz/struct_reuse/protobuf_reuse/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
hz_install:
go install github.com/cloudwego/hertz/cmd/hz@latest

kitex_install:
go install github.com/cloudwego/kitex/tool/cmd/kitex@latest

mod_init:
go mod init a/b/c

kitex_model:
kitex --module=a/b/c -I=../protobuf ../protobuf/hello/hello.proto

hz_model:
hz model --mod=a/b/c --model_dir=kitex_gen -I=../protobuf --idl=../protobuf/hello/hello.proto

# 使用 hz_model 去覆盖 kitex_model,
# 注意:
# 1.所有 proto 中 go_package 的定义必须是这种形式 "{GOMOD}/kitex_gen/***" 其中gomod为项目的 go module 名
# 2. hz 的命令必须加 --model_dir=kitex_gen,才能正确覆盖 kitex_modle
# 3. 目前 hz无法生成 kitex 需要的接口信息,需要进一步对齐
3 changes: 3 additions & 0 deletions hz/struct_reuse/protobuf_reuse/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module a/b/c

go 1.20
15 changes: 15 additions & 0 deletions hz/struct_reuse/protobuf_reuse/kitex_gen/api/api.pb.fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a0202b9

Please sign in to comment.