1 Star 1 Fork 1

xiaoyutab / xgotool

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
cpp.go 6.75 KB
Copy Edit Raw Blame History
xiaoyutab authored 2024-04-24 16:54 . 调整cpp类型信息
package xconsole
import (
"encoding/json"
"fmt"
"os"
"strings"
"gitee.com/xiaoyutab/xgotool/xstring"
)
// 导出到apipost的结构
type apipostStruct struct {
IsChecked int `json:"is_checked" form:"is_checked"` // 是否检测数据类型
Key string `json:"key" form:"key"` // 入参名
Type string `json:"type" form:"type"` // 数据类型 Text/File
NotNull int `json:"not_null" form:"not_null"` // 是否验证必填 非必填为-1 必填为1
FieldType string `json:"field_type" form:"field_type"` // 字段类型
Value string `json:"value" form:"value"` // 字段值
Description string `json:"description" form:"description"` // 字段描述
inputType string // 输入的数据类型
verifyPhone bool // 是否验证参数为手机
verifyEmail bool // 验证是否为邮箱
}
// 根据参数直接生成参数绑定、结构体等相关参数
func cpp(help bool) string {
if help {
return "根据参数直接生成参数绑定、结构体等相关参数"
}
params := os.Args[1:]
if len(params) <= 0 {
panic("请输入正确的参数列表,格式为:参数名1 [类型] [中文注释] [参数验证规则 1-必填] 参数名2 [类型]...\nPS:类型参数默认为字符串,若输入int、float等类型会自动转为json.Number,再通过新的变量转回为指定类型\nPS2:目前不支持复合类型,若需要使用复合类型请使用json2model进行生成")
}
p := getParams(params)
fmt.Println(p)
return ""
}
// 根据参数进行格式化成参数接收函数
func getParams(params []string) string {
used := false
param_length := len(params)
apipost := []apipostStruct{} // apiPost的参数描述结构
for i := 0; i < param_length; i++ {
// 如果本次循环的值为中文/纯数字,就直接跳过本次循环
if xstring.HasContains(params[i]) || xstring.CheckNumber(params[i]) || params[i] == "string" {
continue
}
if len(params[i]) > 2 && params[i][0:1] == "[" {
panic("抱歉,暂不支持以[开头的变量名称:" + fmt.Sprintf("%d: %s", i+1, params[i]))
} else if len(params[i]) > 1 && xstring.HasNumber(string(params[i][0])) {
panic("抱歉,暂不支持以数字开头的变量名称:" + fmt.Sprintf("index(%d): val(%s)", i+1, params[i]))
}
apipost_temp := apipostStruct{
IsChecked: 1,
Key: params[i],
Type: "Text",
NotNull: -1,
FieldType: "String",
inputType: "string",
}
// 存在下一个变量为变量类型
if v, ok := sql_jianrong[params[i+1]]; ok {
apipost_temp.inputType = params[i+1]
apipost_temp.FieldType = v.APIPOST
i++
}
// 存在下一个变量且下一个变量为存在中文
if param_length > i+1 && xstring.HasContains(params[i+1]) {
apipost_temp.Description = params[i+1]
i++
}
// 如果下一个参数是纯数字的话
if param_length > i+1 && xstring.CheckNumber(params[i+1]) {
// 参数验证规则(按位进行判断)
// 必填 | 是否符合手机验证规则
if len(params[i+1]) >= 1 && params[i+1][0] != '0' {
apipost_temp.NotNull = 1
}
if apipost_temp.FieldType != "Array" {
if len(params[i+1]) >= 2 && params[i+1][1] != '0' {
apipost_temp.verifyPhone = true
}
if len(params[i+1]) >= 3 && params[i+1][2] != '0' {
apipost_temp.verifyEmail = true
}
}
i++
}
apipost = append(apipost, apipost_temp)
}
// 定义第一个结构体
p := "\ttype temp_struct struct {"
for i := 0; i < len(apipost); i++ {
temp := apipost[i]
p += "\n\t\t" + xstring.CamelCase(temp.Key) + "\t"
if v, ok := sql_jianrong[temp.inputType]; ok {
p += v.JSON
if v.JSON == "[]json.Number" {
temp.Description += "(此值为数组形式的数字,建议整体使用json传输,或单独此值使用json字符串,如:" + temp.Key + "=[xxx,xxx])"
used = true
} else if v.JSON == "[]string" {
temp.Description += "(此值为数组形式的字符串,建议整体使用json传输,或单独此值使用,拼接字符串,如:" + temp.Key + "=a,b,c)"
used = true
} else if v.JSON == "bool" {
temp.Description += "此值允许传入:true、True、T、t、1、False、false、F、f、0这十个类型的值"
} else if v.JSON == "json.Number" {
used = true
}
}
p += "\t`json:\"" + temp.Key + "\" form:\"" + temp.Key + "\" xml:\"" + temp.Key + "\"`\t// " + temp.Description
apipost[i] = temp
}
p += "\n\t}"
// 定义参数接收
p += "\n\ttemp := temp_struct{}\n\tif err := xgin.Bind(c, &temp); err != nil {\n\t\treturn xgin.Resp().Error(err)\n\t}"
if used {
p += cppStruct2(apipost)
} else {
p += "\n\tparam := temp"
}
// 其他文档类参数输出
p += "\n\t// ApiPost软件所需要的参数描述格式:"
j, _ := json.Marshal(apipost)
p += "\n\t// " + string(j)
p += "\n\t// ApiFox软件所需要的参数描述格式:(此软件需要的参数为多行形式,请手动去除最前面的//和空格,再行复制)"
p += cppApiFox(apipost)
p += "\n\t// 其他软件所需要的KV键值对描述"
jsons := map[string]string{}
for i := 0; i < len(apipost); i++ {
jsons[apipost[i].Key] = apipost[i].Description
}
j, _ = json.Marshal(jsons)
p += "\n\t// " + string(j)
p += "\n\t// 本命令执行参数【留做备份,以便后期使用/修改】"
for i := 0; i < len(params); i++ {
if xstring.HasContains(params[i]) {
params[i] = "\"" + params[i] + "\""
}
}
p += "\n\t// " + strings.Join(params, " ")
// 必填项参数验证
for i := 0; i < len(apipost); i++ {
tmp1 := strings.Split(apipost[i].Description, "(")
if len(tmp1) > 1 {
tmp2 := strings.Split(strings.Join(tmp1[1:], ")"), ")")
apipost[i].Description = tmp1[0] + tmp2[len(tmp2)-1]
}
if apipost[i].NotNull == 1 && apipost[i].inputType != "bool" {
p += "\n\t// " + apipost[i].Key + "参数必填性效验"
if apipost[i].FieldType == "String" {
p += "\n\tif param." + xstring.CamelCase(apipost[i].Key) + " == \"\" {"
} else if apipost[i].FieldType == "Array" {
p += "\n\tif len(param." + xstring.CamelCase(apipost[i].Key) + ") <= 0{"
} else {
p += "\n\tif param." + xstring.CamelCase(apipost[i].Key) + " == 0 {"
}
p += "\n\t\treturn xgin.Resp().ErrorString(\"" + apipost[i].Description + "(" + apipost[i].Key + ") 不能为空\")"
p += "\n\t}"
}
if apipost[i].verifyPhone {
p += "\n\tif xstring.CheckPhone(param." + xstring.CamelCase(apipost[i].Key) + ", true) {"
p += "\n\t\treturn xgin.Resp().ErrorString(\"" + apipost[i].Description + "(" + apipost[i].Key + ") 必须为手机号格式\")\n\t}"
}
if apipost[i].verifyEmail {
p += "\n\tif xstring.CheckEmail(param." + xstring.CamelCase(apipost[i].Key) + ") {"
p += "\n\t\treturn xgin.Resp().ErrorString(\"" + apipost[i].Description + "(" + apipost[i].Key + ") 必须为邮箱格式\")\n\t}"
}
}
return p
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.13

Search