1 Star 2 Fork 5

北京小程科技有限公司/常量和模型生成工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
vartype.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
shallot 提交于 2022-05-26 13:12 . 清理static-check警告。
package golang
import (
"fmt"
"log"
"strings"
"gitee.com/xiaochengtech/unified-structure/src/common"
"gitee.com/xiaochengtech/unified-structure/src/util"
)
// 基础类型转换
func convertBasicType(varType string) string {
switch varType {
case common.VarTypeString, common.VarTypeJson:
return "string"
case common.VarTypeUint8, common.VarTypeUint16, common.VarTypeUint32, common.VarTypeUint64, common.VarTypeInt8, common.VarTypeInt16, common.VarTypeInt32, common.VarTypeInt64, common.VarTypeFloat32, common.VarTypeFloat64:
return varType
case common.VarTypeBool:
return "bool"
case common.VarTypeAny:
return "interface{}"
default:
return ""
}
}
// 复杂类型转换
func convertComplexType(varType string, item common.Field) string {
if util.IsOther(varType) {
return getReferName(item)
} else if util.IsArray(varType) {
innerType := varType[2:]
return "[]" + convertComplexType(innerType, item)
} else if util.IsMap(varType) {
mapParts := strings.Split(varType, "]")
if len(mapParts) != 2 || len(mapParts[0]) <= 4 {
log.Fatalln("错误的map类型", item.Name)
}
keyType := convertBasicType(mapParts[0][4:])
valueType := convertBasicAndReferenceType(mapParts[1], item)
return fmt.Sprintf("map[%s]%s", keyType, valueType)
} else {
return convertBasicAndReferenceType(varType, item)
}
}
func convertBasicAndReferenceType(varType string, item common.Field) string {
basicType := convertBasicType(varType)
if len(basicType) != 0 {
return basicType
}
if util.IsReference(varType) {
return getReferName(item)
}
log.Fatalln("错误的最终类型" + varType)
return ""
}
func getReferName(item common.Field) string {
if len(item.Refer.Name) == 0 {
log.Fatalln("refer.name必须存在", item.Name)
}
return item.Refer.Name
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiaochengtech/unified-structure.git
git@gitee.com:xiaochengtech/unified-structure.git
xiaochengtech
unified-structure
常量和模型生成工具
2b090969dddc

搜索帮助