Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hz): struct reuse #112

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ You can enter the example for information about "How to run"
- [hz/hz_client:](hz/hz_client) Example of using hz to generate client code
- [hz/template:](hz/template) Example of using hz custom templates to generate server code
- [hz/plugin:](hz/plugin) Example of using hz to access third-party plugins
- [hz/struct_reuse:](hz/struct_reuse) Example of using hz to use kitex_gen as hertz model

## Note

Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- [hz/hz_client:](hz/hz_client) 使用 hz 生成客户端代码的示例
- [hz/template:](hz/template) 使用 hz 自定义模版生成服务端代码的示例
- [hz/plugin:](hz/plugin) 使用 hz 接入第三方插件的示例
- [hz/struct_reuse:](hz/struct_reuse) 使用 hz 与 kitex_gen 作为 hertz model 的代码示例

## Note

Expand Down
37 changes: 37 additions & 0 deletions hz/struct_reuse/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.o
*.a
*.so
_obj
_test
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.exe~
*.test
*.prof
*.rar
*.zip
*.gz
*.psd
*.bmd
*.cfg
*.pptx
*.log
*nohup.out
*settings.pyc
*.sublime-project
*.sublime-workspace
!.gitkeep
.DS_Store
/.idea
/.vscode
/output
*.local.yml
dumped_hertz_remote_config.json

6 changes: 6 additions & 0 deletions hz/struct_reuse/demo/.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Code generated by hz. DO NOT EDIT.

hz version: v0.7.1
handlerDir: ""
modelDir: ""
routerDir: ""
5 changes: 5 additions & 0 deletions hz/struct_reuse/demo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
new:
hz new --mod=github.com/cloudwego/hertz-examples/struct_recurse/demo --idl=../thrift/hello.thrift --use=a/b/c/kitex_gen

update:
hz update --idl=../thrift/hello.thrift
27 changes: 27 additions & 0 deletions hz/struct_reuse/demo/biz/handler/hello/example/hello_service.go

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

18 changes: 18 additions & 0 deletions hz/struct_reuse/demo/biz/handler/ping.go

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

21 changes: 21 additions & 0 deletions hz/struct_reuse/demo/biz/router/hello/example/hello.go

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

17 changes: 17 additions & 0 deletions hz/struct_reuse/demo/biz/router/hello/example/middleware.go

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

14 changes: 14 additions & 0 deletions hz/struct_reuse/demo/biz/router/register.go

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

6 changes: 6 additions & 0 deletions hz/struct_reuse/demo/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
RUN_NAME=hertz_service
mkdir -p output/bin
cp script/* output 2>/dev/null
chmod +x output/bootstrap.sh
go build -o output/bin/${RUN_NAME}
3 changes: 3 additions & 0 deletions hz/struct_reuse/demo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cloudwego/hertz-examples/struct_recurse/demo
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

14 changes: 14 additions & 0 deletions hz/struct_reuse/demo/main.go

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

15 changes: 15 additions & 0 deletions hz/struct_reuse/demo/router.go

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

16 changes: 16 additions & 0 deletions hz/struct_reuse/demo/router_gen.go

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

5 changes: 5 additions & 0 deletions hz/struct_reuse/demo/script/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
CURDIR=$(cd $(dirname $0); pwd)
BinaryName=hertz_service
echo "$CURDIR/bin/${BinaryName}"
exec $CURDIR/bin/${BinaryName}
14 changes: 14 additions & 0 deletions hz/struct_reuse/thrift/hello.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace go hello.example

struct HelloReq {
1: string Name (api.query="name");
}

struct HelloResp {
1: string RespBody;
}


service HelloService {
HelloResp HelloMethod(1: HelloReq request) (api.get="/hello");
}
15 changes: 15 additions & 0 deletions hz/struct_reuse/thrift_reuse/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 ../thrift/hello.thrift
hz_model:
hz model --mod=a/b/c --model_dir=kitex_gen -t=ignore_initialisms -t=gen_setter -t=gen_deep_equal -t=compatible_names -t=frugal_tag --idl=../thrift/hello.thrift

# 以上使用 hz_model 覆盖 kitex_model, 从而可以生成相同的结构体,也可以进而使用 hz 的 new/update 命令和 kitex 的 service 选项,创建服务代码。
5 changes: 5 additions & 0 deletions hz/struct_reuse/thrift_reuse/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module a/b/c

go 1.20

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
Loading