goctl config
概述
goctl config 是 用于对 goctl 静态配置文件做管理。 goctl config 的目录是根据工程项目来寻找的,以 goctl 可执行文件当前所在工作目录,寻找当前工作目录处在的 go module 或者 go path空间,然后在该空间下对 goctl.yaml 进行管理。 如果当前工作目录下没有 go module 则会自动根据工作目录名称创建 go.mod 文件。
goctl config 指令
$ goctl config --helpUsage:goctl config [command]Available Commands:clean Clean goctl config fileinit Initialize goctl config fileFlags:-h, --help help for configUse "goctl config [command] --help" for more information about a command.
init
初始化 goctl 静态配置文件。
$ goctl config init --helpInitialize goctl config fileUsage:goctl config init [flags]Flags:-h, --help help for init
clean
删除 goctl 配置文件。
$ goctl config clean --helpClean goctl config fileUsage:goctl config clean [flags]Flags:-h, --help help for clean
配置说明
goctl config 目前暂时仅支持对 model 数据类型映射的配置,其他配置将根据需要加入。
goctl config 初始化后会在项目工程中创建一个 goctl.yaml 文件,其内容如下:
model:types_map:bigint:null_type: sql.NullInt64type: int64unsigned_type: uint64dec:null_type: sql.NullFloat64type: float64decimal:null_type: sql.NullFloat64type: float64double:null_type: sql.NullFloat64type: float64float:null_type: sql.NullFloat64type: float64float4:null_type: sql.NullFloat64type: float64float8:null_type: sql.NullFloat64type: float64int:null_type: sql.NullInt64type: int64unsigned_type: uint64int1:null_type: sql.NullInt64type: int64unsigned_type: uint64int2:null_type: sql.NullInt64type: int64unsigned_type: uint64int3:null_type: sql.NullInt64type: int64unsigned_type: uint64int4:null_type: sql.NullInt64type: int64unsigned_type: uint64int8:null_type: sql.NullInt64type: int64unsigned_type: uint64integer:null_type: sql.NullInt64type: int64unsigned_type: uint64mediumint:null_type: sql.NullInt64type: int64unsigned_type: uint64middleint:null_type: sql.NullInt64type: int64unsigned_type: uint64smallint:null_type: sql.NullInt64type: int64unsigned_type: uint64tinyint:null_type: sql.NullInt64type: int64unsigned_type: uint64date:null_type: sql.NullTimetype: time.Timedatetime:null_type: sql.NullTimetype: time.Timetimestamp:null_type: sql.NullTimetype: time.Timetime:null_type: sql.NullStringtype: stringyear:null_type: sql.NullInt64type: int64unsigned_type: uint64bit:null_type: sql.NullBytetype: byteunsigned_type: bytebool:null_type: sql.NullBooltype: boolboolean:null_type: sql.NullBooltype: boolchar:null_type: sql.NullStringtype: stringvarchar:null_type: sql.NullStringtype: stringnvarchar:null_type: sql.NullStringtype: stringnchar:null_type: sql.NullStringtype: stringcharacter:null_type: sql.NullStringtype: stringlongvarchar:null_type: sql.NullStringtype: stringlinestring:null_type: sql.NullStringtype: stringmultilinestring:null_type: sql.NullStringtype: stringbinary:null_type: sql.NullStringtype: stringvarbinary:null_type: sql.NullStringtype: stringtinytext:null_type: sql.NullStringtype: stringtext:null_type: sql.NullStringtype: stringmediumtext:null_type: sql.NullStringtype: stringlongtext:null_type: sql.NullStringtype: stringenum:null_type: sql.NullStringtype: stringset:null_type: sql.NullStringtype: stringjson:null_type: sql.NullStringtype: stringblob:null_type: sql.NullStringtype: stringlongblob:null_type: sql.NullStringtype: stringmediumblob:null_type: sql.NullStringtype: stringtinyblob:null_type: sql.NullStringtype: string
字段名称 | 说明 |
|---|---|
| model | model 配置 |
| model.types_map | model 配置之类型映射规则,是一个 map<string,obj> 结构,key 为数据库类型,value 为映射对象 |
model 的 golang 结构体
// Model defines the configuration for the model code generation.Model struct {// 类型映射TypesMap map[string]ModelTypeMapOption `yaml:"types_map,omitempty" `}// ModelTypeMapOption custom Type Options.ModelTypeMapOption struct {// 数据库类型名称,不需要附加约束(如:长度等)如 bigint,varcharType string `yaml:"type"`// 当数据类型为 unsigned 修饰时需要映射的 golang 映射类型UnsignedType string `yaml:"unsigned_type,omitempty"`// 当数据类型允许为 null且没有默认值时需要映射的 golang 映射类型,此优先级高于 unsigned 约束NullType string `yaml:"null_type,omitempty"`// 当被映射的 golang 类型为外部包时,需要指定包名。Pkg string `yaml:"pkg,omitempty"`}
数据库类型映射示例:将 decimal 映射为三方的 decimal.Decimal 包,其在 yaml 的表现为灰色底纹部分:
model:types_map:bigint: # 当数据字段类型为 bigint 时,# 1. 如果允许 null 且没有默认值时 golang 类型映射为 sql.NullInt64# 2. 如果不允许 null 或者有默认值,则 golang 类型映射为 int64# 3. 如果不允许 null 或者有默认值,且为 unsigned 修饰,则 golang 类型映射为 uint64null_type: sql.NullInt64type: int64unsigned_type: uint64dec:null_type: sql.NullFloat64type: float64decimal:null_type: decimal.NullDecimalpkg: github.com/shopspring/decimaltype: decimal.Decimal...
使用示例
在不存在 go module 的目录下初始化配置
$ lltotal 0$ goctl config initgoctl.yaml generated in ~/demo/goctl-config/goctl.yaml$ lsgo.mod goctl.yaml$ cat go.modmodule goctl-configgo 1.20
在存在 go module 的目录下初始化配置
$ lltotal 8-rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod$ goctl config initgoctl.yaml generated in ~/demo/goctl-config/goctl.yaml$ lltotal 16-rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod-rw-r--r-- 1 *** staff 3.3K Apr 10 16:37 goctl.yaml
清除配置
$ lltotal 16-rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod-rw-r--r-- 1 *** staff 3.3K Apr 10 16:37 goctl.yaml$ goctl config clean$ lltotal 8-rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod
字段名称