Skip to content

Commit 4487c05

Browse files
author
YangSen-qn
committed
code-gen
1 parent 32e91c1 commit 4487c05

32 files changed

+2214
-0
lines changed

code-gen/go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/YangSen-qn/code-gen
2+
3+
require (
4+
github.com/getkin/kin-openapi v0.124.0
5+
github.com/iancoleman/strcase v0.3.0
6+
gopkg.in/yaml.v3 v3.0.1
7+
)
8+
9+
require (
10+
github.com/go-openapi/jsonpointer v0.20.2 // indirect
11+
github.com/go-openapi/swag v0.22.8 // indirect
12+
github.com/invopop/yaml v0.2.0 // indirect
13+
github.com/josharian/intern v1.0.0 // indirect
14+
github.com/mailru/easyjson v0.7.7 // indirect
15+
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
16+
github.com/perimeterx/marshmallow v1.1.5 // indirect
17+
)
18+
19+
go 1.21.8

code-gen/src/api_desc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

code-gen/src/apidesc/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package apidesc
2+
3+
type ApiDetailedDescription struct {
4+
Documentation string `yaml:"documentation,omitempty"` // API 文档
5+
PackageName string `yaml:"package_name,omitempty"` // 包名,文件路径中提取
6+
Name string `yaml:"name,omitempty"` // API 名称,文件名
7+
CamelCaseName string `yaml:"camel_case_name,omitempty"` // 驼峰命名
8+
SnakeCaseName string `yaml:"snake_case_name,omitempty"` // 蛇形命名:特指下划线命名
9+
Method MethodName `yaml:"method,omitempty"` // HTTP 方法
10+
ServiceNames []ServiceName `yaml:"service_names,omitempty"` // 七牛服务名称
11+
Command string `yaml:"command,omitempty"` // URL 查询命令
12+
BasePath string `yaml:"base_path,omitempty"` // URL 基础路径
13+
PathSuffix string `yaml:"path_suffix,omitempty"` // URL 路径后缀
14+
Request ApiRequestDescription `yaml:"request,omitempty"` // 请求参数
15+
Response ApiResponseDescription `yaml:"response,omitempty"` // 响应参数
16+
}

code-gen/src/apidesc/body_json.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package apidesc
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/yaml.v3"
7+
)
8+
9+
type (
10+
JsonType struct {
11+
String bool `yaml:"string,omitempty"` // 字符串
12+
Integer bool `yaml:"integer,omitempty"` // 整型数字
13+
Float bool `yaml:"float,omitempty"` // 浮点型数字
14+
Boolean bool `yaml:"boolean,omitempty"` // 布尔值
15+
Array *JsonArray `yaml:"array,omitempty"` // 数组
16+
Struct *JsonStruct `yaml:"struct,omitempty"` // 结构体
17+
Any bool `yaml:"any,omitempty"` // 任意数据结构
18+
StringMap bool `yaml:"string_map,omitempty"` // 任意字符串映射结构
19+
}
20+
21+
JsonArray struct {
22+
Documentation string `yaml:"documentation,omitempty"` // JSON 数组参数文档
23+
Name string `yaml:"name,omitempty"` // JSON 数组名称
24+
CamelCaseName string `yaml:"camel_case_name,omitempty"` // JSON 数组驼峰命名
25+
SnakeCaseName string `yaml:"snake_case_name,omitempty"` // JSON 数组下划线命名
26+
Type *JsonType `yaml:"type,omitempty"` // JSON 数组类型
27+
}
28+
29+
JsonStruct struct {
30+
Documentation string `yaml:"documentation,omitempty"` // JSON 结构体参数文档
31+
Name string `yaml:"name,omitempty"` // JSON 结构体名称
32+
CamelCaseName string `yaml:"camel_case_name,omitempty"` // JSON 结构体驼峰命名
33+
SnakeCaseName string `yaml:"snake_case_name,omitempty"` // JSON 结构体下划线命名
34+
Fields []JsonField `yaml:"fields,omitempty"` // JSON 字段列表
35+
}
36+
37+
JsonField struct {
38+
Documentation string `yaml:"documentation,omitempty"` // JSON 字段参数文档
39+
Key string `yaml:"key,omitempty"` // JSON 字段参数名称
40+
FieldName string `yaml:"field_name,omitempty"` // JSON 字段名称
41+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // JSON 字段驼峰命名
42+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // JSON 字段下划线命名
43+
Type JsonType `yaml:"type,omitempty"` // JSON 字段类型
44+
Optional *OptionalType `yaml:"optional,omitempty"` // JSON 字段是否可选,如果为空,则表示必填
45+
ServiceBucket *ServiceBucketType `yaml:"service_bucket,omitempty"` // JSON 字段是否是空间名称,如果为空,则表示不是,如果不为空,则填写格式
46+
ServiceObject *ServiceObjectType `yaml:"service_object,omitempty"` // JSON 字段是否是对象名称,如果为空,则表示不是,如果不为空,则填写格式
47+
}
48+
)
49+
50+
func (jsonType *JsonType) UnmarshalYAML(value *yaml.Node) error {
51+
switch value.ShortTag() {
52+
case "!!str":
53+
switch value.Value {
54+
case "string":
55+
jsonType.String = true
56+
case "integer":
57+
jsonType.Integer = true
58+
case "float":
59+
jsonType.Float = true
60+
case "boolean":
61+
jsonType.Boolean = true
62+
case "any":
63+
jsonType.Any = true
64+
case "string_map":
65+
jsonType.StringMap = true
66+
default:
67+
return fmt.Errorf("unknown json type: %s", value.Value)
68+
}
69+
return nil
70+
case "!!map":
71+
switch value.Content[0].Value {
72+
case "array":
73+
return value.Content[1].Decode(&jsonType.Array)
74+
case "struct":
75+
return value.Content[1].Decode(&jsonType.Struct)
76+
default:
77+
return fmt.Errorf("unknown json type: %s", value.Content[0].Value)
78+
}
79+
default:
80+
return fmt.Errorf("unknown json type: %s", value.ShortTag())
81+
}
82+
}

code-gen/src/apidesc/consts.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package apidesc
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type MethodName string
8+
9+
const (
10+
MethodNameGET MethodName = http.MethodGet
11+
MethodNamePOST MethodName = http.MethodPost
12+
MethodNamePUT MethodName = http.MethodPut
13+
MethodNameDELETE MethodName = http.MethodDelete
14+
)
15+
16+
type ServiceName string
17+
18+
const (
19+
ServiceNameUp ServiceName = "up"
20+
ServiceNameIo ServiceName = "io"
21+
ServiceNameRs ServiceName = "rs"
22+
ServiceNameRsf ServiceName = "rsf"
23+
ServiceNameApi ServiceName = "api"
24+
ServiceNameBucket ServiceName = "uc"
25+
)
26+
27+
// StringLikeType 类字符串参数类型
28+
type StringLikeType = string
29+
30+
const (
31+
StringLikeTypeString StringLikeType = "string" // 字符串
32+
StringLikeTypeInteger StringLikeType = "integer" // 整型数字
33+
StringLikeTypeFloat StringLikeType = "float" // 浮点型数字
34+
StringLikeTypeBoolean StringLikeType = "boolean" // 布尔值
35+
)
36+
37+
type MultipartFormDataType = string
38+
39+
const (
40+
MultipartFormDataTypeString MultipartFormDataType = "string" // 字符串
41+
MultipartFormDataTypeInteger MultipartFormDataType = "integer"
42+
MultipartFormDataTypeUploadToken MultipartFormDataType = "upload_token"
43+
MultipartFormDataTypeBinaryData MultipartFormDataType = "binary_data"
44+
)
45+
46+
type OptionalType string
47+
48+
const (
49+
OptionalTypeRequired OptionalType = "" // 用户必须传值
50+
OptionalTypeOmitEmpty OptionalType = "omitempty" // 如果用户不传值,则该字段省略
51+
OptionalTypeKeepEmpty OptionalType = "keepempty" // 即使用户不传值,也会发送空值
52+
OptionalTypeNullable OptionalType = "nullable" // 如果用户不传值,则该字段省略,但如果用户传值,即使是空值也会发送
53+
)
54+
55+
type Authorization string
56+
57+
const (
58+
AuthorizationNone Authorization = ""
59+
AuthorizationQbox Authorization = "Qbox"
60+
AuthorizationQiniu Authorization = "qiniu"
61+
AuthorizationUpToken Authorization = "UploadToken"
62+
)
63+
64+
type Idempotent string
65+
66+
const (
67+
IdempotentDefault Idempotent = "default" // 默认幂等性(根据 HTTP 方法判定)
68+
IdempotentAlways Idempotent = "always" // 总是幂等
69+
IdempotentNever Idempotent = "never" // 总是不幂等
70+
)
71+
72+
type EncodeType string
73+
74+
const (
75+
EncodeTypeNone EncodeType = "none"
76+
EncodeTypeUrlSafeBase64 EncodeType = "url_safe_base64" // 需要进行编码
77+
EncodeTypeUrlSafeBase64OrNone EncodeType = "url_safe_base64_or_none" // 不仅需要编码,即使路径参数的值是 None 也要编码。该选项暗示了 nullable
78+
)
79+
80+
type ServiceBucketType string
81+
82+
const (
83+
ServiceBucketTypeNone ServiceBucketType = "" //
84+
ServiceBucketTypePlainText ServiceBucketType = "plain_text" // 该值为存储空间名称
85+
ServiceBucketTypeEntry ServiceBucketType = "entry" // 该值格式为 UrlSafeBase64("$bucket:$key")
86+
ServiceBucketTypeUploadToken ServiceBucketType = "upload_token" // 该值为上传凭证,内部包含存储空间信息
87+
)
88+
89+
type ServiceObjectType string
90+
91+
const (
92+
ServiceObjectTypeNone ServiceObjectType = "" //
93+
ServiceObjectTypePlainText ServiceObjectType = "plain_text" // 该值为对象名称
94+
ServiceObjectTypeEntry ServiceObjectType = "entry" // 该值格式为 UrlSafeBase64("$bucket:$key")
95+
)

code-gen/src/apidesc/header.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package apidesc
2+
3+
type HeaderName struct {
4+
HeaderName string `yaml:"header_name,omitempty"` // HTTP 头名称
5+
Documentation string `yaml:"documentation,omitempty"` // HTTP 头参数文档
6+
FieldName string `yaml:"field_name,omitempty"` // HTTP 头参数名称
7+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // HTTP 头参数驼峰命名
8+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // HTTP 头参数下划线命名
9+
Optional *OptionalType `yaml:"optional,omitempty"` // HTTP 头参数是否可选,如果为空,则表示必填
10+
}
11+
12+
type HeaderNames []HeaderName

code-gen/src/apidesc/request.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package apidesc
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/yaml.v3"
7+
)
8+
9+
type NamedPathParam struct {
10+
Documentation string `yaml:"documentation,omitempty"` // URL 路径参数文档
11+
PathSegment string `yaml:"path_segment,omitempty"` // URL 路径段落,如果为空,则表示参数直接追加在 URL 路径末尾
12+
FieldName string `yaml:"field_name,omitempty"` // URL 路径参数名称
13+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // URL 路径参数驼峰命名
14+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // URL 路径参数下划线命名
15+
Type *StringLikeType `yaml:"type,omitempty"` // URL 路径参数类型
16+
Encode *EncodeType `yaml:"encode,omitempty"` // URL 路径参数编码方式,如果为空,表示直接转码成字符串
17+
ServiceBucket *ServiceBucketType `yaml:"service_bucket,omitempty"` // URL 路径参数是否是空间名称,如果为空,则表示不是,如果不为空,则填写格式
18+
ServiceObject *ServiceObjectType `yaml:"service_object,omitempty"` // URL 路径参数是否是对象名称,如果为空,则表示不是,如果不为空,则填写格式
19+
Optional *OptionalType `yaml:"optional,omitempty"` // URL 路径参数是否可选,如果为空,则表示必填
20+
}
21+
22+
type FreePathParams struct {
23+
FieldName string `yaml:"field_name,omitempty"` // URL 路径参数名称
24+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // URL 路径参数驼峰命名
25+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // URL 路径参数下划线命名
26+
Documentation string `yaml:"documentation,omitempty"` // URL 路径参数文档
27+
EncodeParamKey *EncodeType `yaml:"encode_param_key"` // URL 路径参数键编码方式,如果为空,表示直接转码成字符串
28+
EncodeParamValue *EncodeType `yaml:"encode_param_value"` // URL 路径参数值编码方式,如果为空,表示直接转码成字符串
29+
}
30+
31+
type PathParams struct {
32+
Named []NamedPathParam `yaml:"named,omitempty"` // URL 路径有名参数列表
33+
Free *FreePathParams `yaml:"free,omitempty"` // URL 路径自由参数列表
34+
}
35+
36+
type RequestBody struct {
37+
Json *JsonType `yaml:"json,omitempty"` // JSON 类型
38+
FormUrlencoded *FormUrlencodedRequestStruct `yaml:"form_urlencoded,omitempty"` // URL 编码表单调用(无法上传二进制数据)
39+
MultipartFormData *MultipartFormFields `yaml:"multipart_form_data,omitempty"` // 复合表单调用(可以上传二进制数据)
40+
BinaryData bool `yaml:"binary_data,omitempty"` // 二进制数据
41+
}
42+
43+
func (body *RequestBody) UnmarshalYAML(value *yaml.Node) error {
44+
switch value.ShortTag() {
45+
case "!!str":
46+
switch value.Value {
47+
case "binary_data":
48+
body.BinaryData = true
49+
default:
50+
return fmt.Errorf("unknown request body type: %s", value.Value)
51+
}
52+
return nil
53+
case "!!map":
54+
switch value.Content[0].Value {
55+
case "json":
56+
return value.Content[1].Decode(&body.Json)
57+
case "form_urlencoded":
58+
return value.Content[1].Decode(&body.FormUrlencoded)
59+
case "multipart_form_data":
60+
return value.Content[1].Decode(&body.MultipartFormData)
61+
default:
62+
return fmt.Errorf("unknown request body type: %s", value.Content[0].Value)
63+
}
64+
default:
65+
return fmt.Errorf("unknown request body type: %s", value.ShortTag())
66+
}
67+
}
68+
69+
type ApiRequestDescription struct {
70+
PathParams *PathParams `yaml:"path_params,omitempty"` // URL 路径参数列表
71+
HeaderNames HeaderNames `yaml:"header_names,omitempty"` // HTTP 头参数列表
72+
QueryNames QueryNames `yaml:"query_names,omitempty"` // URL 查询参数列表
73+
Body *RequestBody `yaml:"body,omitempty"` // 请求体
74+
Authorization *Authorization `yaml:"authorization,omitempty"` // 鉴权参数
75+
Idempotent *Idempotent `yaml:"idempotent,omitempty"` // 幂等性
76+
responseTypeRequired bool `yaml:"response_type_required"` //
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package apidesc
2+
3+
type FormUrlencodedRequestStruct struct {
4+
Fields []FormUrlencodedRequestField `yaml:"fields,omitempty"` // URL 编码表单字段列表
5+
}
6+
7+
type FormUrlencodedRequestField struct {
8+
FieldName string `yaml:"field_name,omitempty"` // URL 编码表单字段名称
9+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // URL 编码表单字段驼峰命名
10+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // URL 编码表单字段下划线命名
11+
Key string `yaml:"key,omitempty"` // URL 编码表单参数名称
12+
Documentation string `yaml:"documentation,omitempty"` // URL 编码表单参数文档
13+
Type *StringLikeType `yaml:"type,omitempty"` // URL 编码表单参数类型
14+
Multiple bool `yaml:"multiple,omitempty"` // URL 编码表单参数是否可以有多个值
15+
Optional *OptionalType `yaml:"optional,omitempty"` // URL 编码表单参数是否可选,如果为空,则表示必填
16+
ServiceBucket *ServiceBucketType `yaml:"service_bucket,omitempty"` // URL 编码表单参数是否是空间名称,如果为空,则表示不是,如果不为空,则填写格式
17+
ServiceObject *ServiceObjectType `yaml:"service_object,omitempty"` // URL 编码表单参数是否是对象名称,如果为空,则表示不是,如果不为空,则填写格式
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package apidesc
2+
3+
type MultipartFormFields struct {
4+
Named []NamedMultipartFormField `yaml:"named_fields,omitempty"`
5+
Free *FreeMultipartFormFields `yaml:"free_fields,omitempty"`
6+
}
7+
8+
type NamedMultipartFormField struct {
9+
FieldName string `yaml:"field_name,omitempty"`
10+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"`
11+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"`
12+
Key string `yaml:"key,omitempty"`
13+
Type *MultipartFormDataType `yaml:"type,omitempty"`
14+
Documentation string `yaml:"documentation,omitempty"`
15+
ServiceBucket *ServiceBucketType `yaml:"service_bucket,omitempty"`
16+
ServiceObject *ServiceObjectType `yaml:"service_object,omitempty"`
17+
Optional *OptionalType `yaml:"optional,omitempty"`
18+
}
19+
20+
type FreeMultipartFormFields struct {
21+
FieldName string `yaml:"field_name,omitempty"`
22+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"`
23+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"`
24+
Documentation string `yaml:"documentation,omitempty"`
25+
}

code-gen/src/apidesc/request_query.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package apidesc
2+
3+
type QueryName struct {
4+
FieldName string `yaml:"field_name,omitempty"` // 参数名称
5+
FieldCamelCaseName string `yaml:"field_camel_case_name,omitempty"` // URL 路径参数驼峰命名
6+
FieldSnakeCaseName string `yaml:"field_snake_case_name,omitempty"` // URL 路径参数下划线命名
7+
QueryName string `yaml:"query_name,omitempty"` // URL 查询参数名称
8+
Documentation string `yaml:"documentation,omitempty"` // URL 查询参数文档
9+
Multiple bool `yaml:"multiple,omitempty"` // URL 查询参数是否可以有多个值
10+
QueryType *StringLikeType `yaml:"query_type,omitempty"` // URL 查询参数类型
11+
ServiceBucket *ServiceBucketType `yaml:"service_bucket,omitempty"` // URL 查询参数是否是空间名称,如果为空,则表示不是,如果不为空,则填写格式
12+
ServiceObject *ServiceObjectType `yaml:"service_object,omitempty"` // URL 查询参数是否是对象名称,如果为空,则表示不是,如果不为空,则填写格式
13+
Optional *OptionalType `yaml:"optional,omitempty"` // URL 查询参数是否可选,如果为空,则表示必填
14+
}
15+
16+
type QueryNames []QueryName

code-gen/src/apidesc/response.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package apidesc
2+
3+
type ApiResponseDescription struct {
4+
HeaderNames HeaderNames `yaml:"header_names,omitempty"` // HTTP 头参数列表
5+
Body *ResponseBody `yaml:"body,omitempty"` // 响应体
6+
}

0 commit comments

Comments
 (0)