Skip to content

Commit

Permalink
新加protobufmsg子命令
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Feb 25, 2023
1 parent e35ad81 commit 5e7e517
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# h2o
脚手架工具,统一的dsl,方便生成一些代码
脚手架工具,统一的dsl,方便生成一些代码, 静态MVC。持有model,其余的就是交给h2o生成代码。最多写写logic
目前可以直接生成的代码有:
1. http client:实现一个第三方sdk,so easy
2. http server:实现服务再不用纠结结构体的定义
3. curl 命令: 方便自测和api交流
4. protobuf定义: 从json或者yaml生成

## Install
```bash
Expand All @@ -8,6 +13,7 @@ go install github.com/antlabs/h2o/cmd/h2o@latest
## 命令行
* codemsg: 用于生成错误码相关代码
* http: 生成http client 和http server(代码)
* curl: 生成curl命令
* jsonstruct: 字符串转golang struct定义
* yamlstruct: 字符串转golang struct定义
* pb: dsl生成protobuf定义
Expand All @@ -20,9 +26,11 @@ Options:
Subcommand:
codemsg Generate code in codemsg format from constants
curl gen curl command
http gen http code
jsonstruct Generate structure from json
pb gen protobuf code
transport transport
yamlstruct Generate structure from yaml
```
## json 子命令
Expand Down
17 changes: 16 additions & 1 deletion cmd/h2o/h2o.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/antlabs/h2o/transport"
"github.com/antlabs/tostruct/json"
"github.com/antlabs/tostruct/option"
"github.com/antlabs/tostruct/protobuf"
"github.com/antlabs/tostruct/yaml"
"github.com/guonaihong/clop"
)
Expand All @@ -22,6 +23,7 @@ type JSON struct {
From string `clop:"short;long" usage:"which file to open from? If it is -, it means reading from stdin"`
StructName string `clop:"short;long" usage:"Control the name of the generated structure"`
yaml bool
protobuf bool
}

func (j *JSON) SubMain() {
Expand All @@ -44,6 +46,8 @@ func (j *JSON) SubMain() {

if j.yaml {
all, err = yaml.Marshal(all, opt...)
} else if j.protobuf {
all, err = protobuf.Marshal(all, opt...)
} else {
all, err = json.Marshal(all, opt...)
}
Expand All @@ -67,16 +71,27 @@ func (y *YAML) SubMain() {

}

type Protobuf struct {
JSON
}

func (p *Protobuf) SubMain() {
p.JSON.protobuf = true
p.JSON.SubMain()
}

// 主命令
type H2O struct {
// 子命令,入口函数是SubMain
JSONStruct JSON `clop:"subcommand" usage:"Generate structure from json"`
// 子命令,入口函数是SubMain
YAMLStruct YAML `clop:"subcommand" usage:"Generate structure from yaml"`
// 子命令,入口函数是SubMain
ProtobufMsg Protobuf `clop:"subcommand" usage:"Generate protobuf message from json or yaml"`
//子命令, 入口是SubMain
CodeMsg codemsg.CodeMsg `clop:"subcommand" usage:"Generate code in codemsg format from constants"`
// 子命令,生成http客户端代码和http服务端代码(TODO)
HTTP http.HTTP `clop:"subcommand" usage:"gen http code"`
HTTP http.HTTP `clop:"subcommand" usage:"gen http code(client and server)"`
//子命令,生成protobuf
PB pb.Pb `clop:"subcommand" usage:"gen protobuf code"`
//transport
Expand Down

0 comments on commit 5e7e517

Please sign in to comment.