goctl config

Overview

Goctl config is used to manage goctl static configuration files. The directory of goctl config is found according to the project. In the working directory where the goctl executable is currently located, find the go module or go where the current working directory is located. Path space, and then manage goctl.yaml under that space. If there is no go module in the current working directory, a go.mod file is automatically created based on the working directory name.

goctl config command

  1. $ goctl config --help
  2. Usage:
  3. goctl config [command]
  4. Available Commands:
  5. clean Clean goctl config file
  6. init Initialize goctl config file
  7. Flags:
  8. -h, --help help for config
  9. Use "goctl config [command] --help" for more information about a command.

init

Initialize the goctl static configuration file.

  1. $ goctl config init --help
  2. Initialize goctl config file
  3. Usage:
  4. goctl config init [flags]
  5. Flags:
  6. -h, --help help for init

clean

Delete the goctl configuration file.

  1. $ goctl config clean --help
  2. Clean goctl config file
  3. Usage:
  4. goctl config clean [flags]
  5. Flags:
  6. -h, --help help for clean

configuration instructions

The goctl config currently only supports the configuration of the model data type mapping, and other configurations will be added as needed.

After goctl config is initialized, a goctl.yaml file will be created in the project, with the following contents:

  1. model:
  2. types_map:
  3. bigint:
  4. null_type: sql.NullInt64
  5. type: int64
  6. unsigned_type: uint64
  7. dec:
  8. null_type: sql.NullFloat64
  9. type: float64
  10. decimal:
  11. null_type: sql.NullFloat64
  12. type: float64
  13. double:
  14. null_type: sql.NullFloat64
  15. type: float64
  16. float:
  17. null_type: sql.NullFloat64
  18. type: float64
  19. float4:
  20. null_type: sql.NullFloat64
  21. type: float64
  22. float8:
  23. null_type: sql.NullFloat64
  24. type: float64
  25. int:
  26. null_type: sql.NullInt64
  27. type: int64
  28. unsigned_type: uint64
  29. int1:
  30. null_type: sql.NullInt64
  31. type: int64
  32. unsigned_type: uint64
  33. int2:
  34. null_type: sql.NullInt64
  35. type: int64
  36. unsigned_type: uint64
  37. int3:
  38. null_type: sql.NullInt64
  39. type: int64
  40. unsigned_type: uint64
  41. int4:
  42. null_type: sql.NullInt64
  43. type: int64
  44. unsigned_type: uint64
  45. int8:
  46. null_type: sql.NullInt64
  47. type: int64
  48. unsigned_type: uint64
  49. integer:
  50. null_type: sql.NullInt64
  51. type: int64
  52. unsigned_type: uint64
  53. mediumint:
  54. null_type: sql.NullInt64
  55. type: int64
  56. unsigned_type: uint64
  57. middleint:
  58. null_type: sql.NullInt64
  59. type: int64
  60. unsigned_type: uint64
  61. smallint:
  62. null_type: sql.NullInt64
  63. type: int64
  64. unsigned_type: uint64
  65. tinyint:
  66. null_type: sql.NullInt64
  67. type: int64
  68. unsigned_type: uint64
  69. date:
  70. null_type: sql.NullTime
  71. type: time.Time
  72. datetime:
  73. null_type: sql.NullTime
  74. type: time.Time
  75. timestamp:
  76. null_type: sql.NullTime
  77. type: time.Time
  78. time:
  79. null_type: sql.NullString
  80. type: string
  81. year:
  82. null_type: sql.NullInt64
  83. type: int64
  84. unsigned_type: uint64
  85. bit:
  86. null_type: sql.NullByte
  87. type: byte
  88. unsigned_type: byte
  89. bool:
  90. null_type: sql.NullBool
  91. type: bool
  92. boolean:
  93. null_type: sql.NullBool
  94. type: bool
  95. char:
  96. null_type: sql.NullString
  97. type: string
  98. varchar:
  99. null_type: sql.NullString
  100. type: string
  101. nvarchar:
  102. null_type: sql.NullString
  103. type: string
  104. nchar:
  105. null_type: sql.NullString
  106. type: string
  107. character:
  108. null_type: sql.NullString
  109. type: string
  110. longvarchar:
  111. null_type: sql.NullString
  112. type: string
  113. linestring:
  114. null_type: sql.NullString
  115. type: string
  116. multilinestring:
  117. null_type: sql.NullString
  118. type: string
  119. binary:
  120. null_type: sql.NullString
  121. type: string
  122. varbinary:
  123. null_type: sql.NullString
  124. type: string
  125. tinytext:
  126. null_type: sql.NullString
  127. type: string
  128. text:
  129. null_type: sql.NullString
  130. type: string
  131. mediumtext:
  132. null_type: sql.NullString
  133. type: string
  134. longtext:
  135. null_type: sql.NullString
  136. type: string
  137. enum:
  138. null_type: sql.NullString
  139. type: string
  140. set:
  141. null_type: sql.NullString
  142. type: string
  143. json:
  144. null_type: sql.NullString
  145. type: string
  146. blob:
  147. null_type: sql.NullString
  148. type: string
  149. longblob:
  150. null_type: sql.NullString
  151. type: string
  152. mediumblob:
  153. null_type: sql.NullString
  154. type: string
  155. tinyblob:
  156. null_type: sql.NullString
  157. type: string
goctl config - 图1 Fieldgoctl config - 图2 Description
modelModel code generation related configuration
model.types_mapThe type mapping rule of the model configuration is a map <string, obj> structure, the key is the database type, and the value is the mapping object

Model golang structure

  1. // Model defines the configuration for the model code generation.
  2. Model struct {
  3. // type mapping
  4. TypesMap map[string]ModelTypeMapOption `yaml:"types_map,omitempty" `
  5. }
  6. // ModelTypeMapOption custom Type Options.
  7. ModelTypeMapOption struct {
  8. // Database type name, no additional constraints (e.g. length, etc.) such as bigint, varchar
  9. Type string `yaml:"type"`
  10. // The golang mapping type that needs to be mapped when the data type is unsigned
  11. UnsignedType string `yaml:"unsigned_type,omitempty"`
  12. // The golang mapped type that needs to be mapped when the data type is allowed to be null and there is no default value, this takes precedence over the unsigned constraint
  13. NullType string `yaml:"null_type,omitempty"`
  14. // When the mapped golang type is an external package, you need to specify the package name.
  15. Pkg string `yaml:"pkg,omitempty"`
  16. }

Database type mapping example: Mapping decimal to a three-way decimal. Decimal package, which appears as a gray shading section in yaml:

  1. model:
  2. types_map:
  3. bigint: # When the data field type is bigint,
  4. # 1. If null is allowed and there is no default value, the golang type maps to sql.NullInt64.
  5. # 2. If null is not allowed or there is a default value, the golang type maps to int64.
  6. # 3. If null is not allowed or has a default value and is unsigned, the golang type maps to uint64.
  7. null_type: sql.NullInt64
  8. type: int64
  9. unsigned_type: uint64
  10. dec:
  11. null_type: sql.NullFloat64
  12. type: float64
  13. decimal:
  14. null_type: decimal.NullDecimal
  15. pkg: github.com/shopspring/decimal
  16. type: decimal.Decimal
  17. ...

Example

Initialize the configuration in a directory where there is no go module

  1. $ ll
  2. total 0
  3. $ goctl config init
  4. goctl.yaml generated in ~/demo/goctl-config/goctl.yaml
  5. $ ls
  6. go.mod goctl.yaml
  7. $ cat go.mod
  8. module goctl-config
  9. go 1.20

Initialize the configuration in the directory where the go module exists

  1. $ ll
  2. total 8
  3. -rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod
  4. $ goctl config init
  5. goctl.yaml generated in ~/demo/goctl-config/goctl.yaml
  6. $ ll
  7. total 16
  8. -rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod
  9. -rw-r--r-- 1 *** staff 3.3K Apr 10 16:37 goctl.yaml

clear configuration

  1. $ ll
  2. total 16
  3. -rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod
  4. -rw-r--r-- 1 *** staff 3.3K Apr 10 16:37 goctl.yaml
  5. $ goctl config clean
  6. $ ll
  7. total 8
  8. -rw-r--r-- 1 *** staff 29B Apr 10 16:35 go.mod