Skip to content

Commit 101af5c

Browse files
committed
应同事需求,更新下文档
1 parent 21ff733 commit 101af5c

File tree

4 files changed

+193
-89
lines changed

4 files changed

+193
-89
lines changed

README.md

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -49,89 +49,9 @@ h2o jsonstruct -f -
4949
h2o yamlstruct -f ./test.yaml -n
5050
h2o yamlstruct -f -
5151
```
52-
## codemsg 子命令
53-
只要实现code,自动生成String()类型和CodeMsg类型。可以在错误码代码节约时间
54-
1.给每个ErrNo类型生成String()方法, 内容就是注释里面的。
55-
2.给每个ErrNo类型生成CodeMsg{}结构
56-
```go
57-
// 这段代码是我们要写的。
58-
package demo
5952

60-
type ErrNo int32 // 这里的类型可以自定义,--type后面类型
53+
## codemsg 子命令文档
54+
[codemsg](./codemsg.md)
6155

62-
const (
63-
ENo ErrNo = 1003 // 号码出错
64-
65-
ENotFound ErrNo = 1004 // 找不到
66-
)
67-
68-
```
69-
70-
生成的CodeMsg代码如下
71-
```go
72-
// Code generated by "h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go"; DO NOT EDIT."
73-
74-
package demo
75-
76-
import (
77-
"encoding/json"
78-
"strings"
79-
)
80-
81-
type CodeMsger interface {
82-
SetCode(int)
83-
SetMsg(string)
84-
GetCode() int
85-
GetMsg() string
86-
}
87-
88-
var _ CodeMsger = (*CodeMsg)(nil)
89-
90-
type CodeMsg struct {
91-
Code ErrNo "json:\"code\""
92-
Message string "json:\"message\""
93-
}
94-
95-
func (x *CodeMsg) Error() string {
96-
all, _ := json.Marshal(x)
97-
var b strings.Builder
98-
b.Write(all)
99-
return b.String()
100-
}
101-
102-
func (x *CodeMsg) SetCode(code int) {
103-
x.Code = ErrNo(code)
104-
}
105-
106-
func (x *CodeMsg) SetMsg(msg string) {
107-
x.Message = msg
108-
}
109-
110-
func (x *CodeMsg) GetCode() int {
111-
return int(x.Code)
112-
}
113-
114-
func (x *CodeMsg) GetMsg() string {
115-
return x.Message
116-
}
117-
118-
func NewCodeMsg(code ErrNo) error {
119-
return &CodeMsg{
120-
Code: code,
121-
Message: code.String(),
122-
}
123-
}
124-
125-
var (
126-
ErrCodeMsgENo error = NewCodeMsg(ENo) //号码出错
127-
128-
ErrCodeMsgENotFound error = NewCodeMsg(ENotFound) //找不到
129-
130-
)
131-
```
132-
```bash
133-
./h2o codemsg --code-msg --linecomment --string --type ErrNo ./testdata/err.go
134-
// 如果要修改生成的String方法名, 可以通过--string-method String2 选项
135-
// 如果要生成grpc的错误就用下面的命令
136-
// h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go --grpc --string-method string2 --string
137-
```
56+
## http 子命令文档
57+
[http](./http.md)

codemsg.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
## codemsg 子命令
3+
只要实现code,自动生成String()类型和CodeMsg类型。可以在错误码代码节约时间
4+
1.给每个ErrNo类型生成String()方法, 内容就是注释里面的。
5+
2.给每个ErrNo类型生成CodeMsg{}结构
6+
```go
7+
// 这段代码是我们要写的。
8+
package demo
9+
10+
type ErrNo int32 // 这里的类型可以自定义,--type后面类型
11+
12+
const (
13+
ENo ErrNo = 1003 // 号码出错
14+
15+
ENotFound ErrNo = 1004 // 找不到
16+
)
17+
18+
```
19+
20+
生成的CodeMsg代码如下
21+
```go
22+
// Code generated by "h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go"; DO NOT EDIT."
23+
24+
package demo
25+
26+
import (
27+
"encoding/json"
28+
"strings"
29+
)
30+
31+
type CodeMsger interface {
32+
SetCode(int)
33+
SetMsg(string)
34+
GetCode() int
35+
GetMsg() string
36+
}
37+
38+
var _ CodeMsger = (*CodeMsg)(nil)
39+
40+
type CodeMsg struct {
41+
Code ErrNo "json:\"code\""
42+
Message string "json:\"message\""
43+
}
44+
45+
func (x *CodeMsg) Error() string {
46+
all, _ := json.Marshal(x)
47+
var b strings.Builder
48+
b.Write(all)
49+
return b.String()
50+
}
51+
52+
func (x *CodeMsg) SetCode(code int) {
53+
x.Code = ErrNo(code)
54+
}
55+
56+
func (x *CodeMsg) SetMsg(msg string) {
57+
x.Message = msg
58+
}
59+
60+
func (x *CodeMsg) GetCode() int {
61+
return int(x.Code)
62+
}
63+
64+
func (x *CodeMsg) GetMsg() string {
65+
return x.Message
66+
}
67+
68+
func NewCodeMsg(code ErrNo) error {
69+
return &CodeMsg{
70+
Code: code,
71+
Message: code.String(),
72+
}
73+
}
74+
75+
var (
76+
ErrCodeMsgENo error = NewCodeMsg(ENo) //号码出错
77+
78+
ErrCodeMsgENotFound error = NewCodeMsg(ENotFound) //找不到
79+
80+
)
81+
```
82+
```bash
83+
./h2o codemsg --code-msg --linecomment --string --type ErrNo ./testdata/err.go
84+
// 如果要修改生成的String方法名, 可以通过--string-method String2 选项
85+
// 如果要生成grpc的错误就用下面的命令
86+
// h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go --grpc --string-method string2 --string
87+
```

http.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## http
2+
本文档介绍http子命令的用法,主要涉及两个flag,client和server
3+
* client: 主要场景主要用于快速实现一些第三方sdk。
4+
* server: 主要场景主要快速实现一些callback服务
5+
6+
## 一个简单的配置文件
7+
该配置文件使用yaml描述http元数据。请求体里面支持curl命令(目前不支持form表单,有时间再支持一波)
8+
保存为apptoken.yaml 文件名
9+
```yaml
10+
---
11+
package: "message" #生成go的包名
12+
protobuf:
13+
package: "message.v1" #生成protobuf时使用,可以先忽略这个配置
14+
go_package: "./pb/v1/message" # 生成protobuf时,指定生成的go package的名字,可以先忽略这个配置
15+
init:
16+
handler: New #函数名, 名字是New认识是构造函数
17+
rvStruct:
18+
name: Message #返回的结构体名, 如果生成客户端代码,这个是必须的
19+
field:
20+
Host: '' #生成http 客户端结构体,自己配置的字段
21+
OrgName: '' #生成http 客户端结构体,自己配置的字段
22+
AppName: '' #生成http 客户端结构体,自己配置的字段
23+
Username: '' #生成http 客户端结构体,自己配置的字段
24+
25+
multi:
26+
- handler: Message.SendTxtMessageAsync #包名.方法名, 这样命名
27+
req:
28+
# 必填字段,例 method: POST
29+
method:
30+
curl: >-
31+
curl -X POST -i 'http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <YourAppToken>' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"}}'
32+
33+
# 必填字段
34+
# url: https://host/port
35+
url:
36+
template:
37+
url: true
38+
# 直接使用配置文件里面的值
39+
useDefault:
40+
header:
41+
#- Content-Type
42+
#- Accept
43+
body:
44+
#- .grant_type
45+
header: #可配置的http header,目前选配
46+
- 'Content-Type: application/json'
47+
- 'Accept: application/json'
48+
encode:
49+
body:
50+
body:
51+
52+
resp:
53+
newType:
54+
# 响应体,直接复制一个json就行
55+
body: >-
56+
{
57+
"path": "/messages/users",
58+
"uri": "https://XXXX/XXXX/XXXX/messages/users",
59+
"timestamp": 1657254052191,
60+
"organization": "XXXX",
61+
"application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
62+
"action": "post",
63+
"data": {
64+
"user2": "1029457500870543736"
65+
},
66+
"duration": 0,
67+
"applicationName": "XXXX"
68+
}
69+
70+
```
71+
72+
## 生成客户端代码
73+
h2o http -f ./apptoken.yaml --client
74+
75+
## 生成服务端代码
76+
h2o http -f ./apptoken.yaml --server

http/http.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ type HTTP struct {
4040
Debug bool `clop:"long" usage:"debug mode"`
4141
}
4242

43+
var exampleYaml = `
44+
package: "apptoken" #包名
45+
protobuf:
46+
package: "apptoken.v1"
47+
go_package: "./pb/v1/im/apptoken" #给protobuf使用
48+
init:
49+
handler: New #函数名, 名字是New默认是构造函数
50+
rvStruct:
51+
name: AppToken #返回的结构体名
52+
53+
`
54+
4355
// 入口函数
4456
func (h *HTTP) SubMain() {
4557
goModName := gomod.GetGoModuleName(h.Dir)
@@ -52,11 +64,20 @@ func (h *HTTP) SubMain() {
5264
return
5365
}
5466

55-
tmplClient := client.ClientTmpl{
56-
PackageName: c.Package,
57-
InitField: c.Init.RvStruct.Field,
58-
StructName: c.Init.RvStruct.Name,
59-
ReceiverName: strings.ToLower(string(c.Init.RvStruct.Name[0])),
67+
var tmplClient client.ClientTmpl
68+
if h.Client {
69+
if len(c.Init.RvStruct.Name) == 0 {
70+
// rvStruct.Name 不能为空
71+
fmt.Printf("rvStruct name is empty:\n%s\n", exampleYaml)
72+
continue
73+
}
74+
75+
tmplClient = client.ClientTmpl{
76+
PackageName: c.Package,
77+
InitField: c.Init.RvStruct.Field,
78+
StructName: c.Init.RvStruct.Name,
79+
ReceiverName: strings.ToLower(string(c.Init.RvStruct.Name[0])),
80+
}
6081
}
6182

6283
tmplClientType := pyaml.TypeTmpl{PackageName: c.Package}

0 commit comments

Comments
 (0)