@@ -27,7 +27,7 @@ func (cg *CodeGenerator) ReadFromSchema(schema string, table string) {
2727}
2828
2929func (cg * CodeGenerator ) getHclFile (schema string ) (* hclwrite.File , error ) {
30- filepath := fmt .Sprintf ("tools/migrator/schema /%s.my.hcl" , schema )
30+ filepath := fmt .Sprintf ("schemas /%s.my.hcl" , schema )
3131 content , err := os .ReadFile (filepath )
3232 if err != nil {
3333 fmt .Println ("Error reading file:" , err )
@@ -45,7 +45,7 @@ func (cg *CodeGenerator) handleHclBlock(block *hclwrite.Block) error {
4545 return nil
4646 }
4747
48- tableName := block .Labels ()[0 ]
48+ tableName := CamelCase ( block .Labels ()[0 ])
4949 replacers := cg .generateDomainFromHclBlock (block , tableName )
5050 validateErr := cg .validateFiles (tableName )
5151 if validateErr != nil {
@@ -65,21 +65,21 @@ func (cg *CodeGenerator) generateDomainFromHclBlock(block *hclwrite.Block, table
6565 * cg .isIntId = false
6666 domain := cg .generateDomainStruct (block .Body ().Blocks (), tableName , cg .primaryKey , cg .pkType )
6767 dataType := cg .generateStruct (block .Body ().Blocks (), nil , nil , cg .generateDeclarationLine )
68- createAttrData := cg .generateStruct (block .Body ().Blocks (), nil , nil , cg .generateCreateAttributionLine )
69- editAttrData := cg .generateStruct (block .Body ().Blocks (), nil , nil , cg .generateEditAttributionLine )
68+ createAttrData := cg .generateStruct (block .Body ().Blocks (), nil , nil , cg .generateAttributionLine )
69+ editAttrData := cg .generateStruct (block .Body ().Blocks (), nil , nil , cg .generateAttributionLine )
7070 replacers := GetReplacersConfig (cg .config , cg .domainType , []string {tableName })
7171 replacers ["{{domainType}}" ] = domain
7272 replacers ["{{dataType}}" ] = dataType
7373 replacers ["{{pkDbName}}" ] = * cg .primaryKey
74- replacers ["{{pkName}}" ] = PascalCase ( * cg .primaryKey )
74+ replacers ["{{pkName}}" ] = * cg .primaryKey
7575 replacers ["{{pkType}}" ] = * cg .pkType
7676 replacers ["{{createServiceData}}" ] = createAttrData
7777 replacers ["{{editServiceData}}" ] = editAttrData
7878 if * cg .needImportTime {
7979 replacers ["{{optionalImports}}" ] = "\" time\" "
8080 }
8181 if ! * cg .isIntId {
82- replacers ["{{idVar}}" ] = "id : = s.idCreator.Create()"
82+ replacers ["{{idVar}}" ] = "domain." + PascalCase ( * cg . primaryKey ) + " = s.idCreator.Create()"
8383 }
8484 return replacers
8585}
@@ -88,12 +88,13 @@ func (cg *CodeGenerator) generateDomainStruct(blocks []*hclwrite.Block, tableNam
8888 * pk = cg .findPkOnBlocks (blocks )
8989 structString := "type " + PascalCase (tableName ) + " struct {\n "
9090 structString += cg .generateStruct (blocks , pk , pkType , cg .generateDeclarationLine )
91+ structString += "\t client string\n \t filters *filters.Filters\n "
9192 structString += "}"
9293 return structString
9394}
9495
9596func (cg * CodeGenerator ) generateStruct (blocks []* hclwrite.Block , pk , pkType * string , strFormationFunc func (string , string , string , string ) string ) string {
96- declarationString := "\n "
97+ declarationString := ""
9798 for _ , block := range blocks {
9899 if block .Type () == "column" {
99100 token , ok := block .Body ().Attributes ()["type" ]
@@ -102,9 +103,14 @@ func (cg *CodeGenerator) generateStruct(blocks []*hclwrite.Block, pk, pkType *st
102103 }
103104 tokenStr := string (token .Expr ().BuildTokens (nil ).Bytes ())
104105 goType := cg .dbTypesToGoTypes (tokenStr )
106+ nullable , nullOk := block .Body ().Attributes ()["null" ]
107+ isNullable := cg .verifyIsNullable (nullable , nullOk )
108+ if isNullable {
109+ goType = "*" + goType
110+ }
105111
106112 if pk != nil && block .Labels ()[0 ] == * pk {
107- * pkType = fmt .Sprintf ("%s string `param:\" id\" `" , PascalCase (* pk ))
113+ * pkType = fmt .Sprintf ("%s string `param:\" id\" `\n " , PascalCase (* pk ))
108114 }
109115
110116 declarationString = strFormationFunc (
@@ -137,39 +143,12 @@ func (cg *CodeGenerator) generateDeclarationLine(str, name, goType, dbTag string
137143 )
138144}
139145
140- func (cg * CodeGenerator ) generateCreateAttributionLine (str , name , goType , _ string ) string {
146+ func (cg * CodeGenerator ) generateAttributionLine (str , name , _ , _ string ) string {
141147 if name == PascalCase (* cg .primaryKey ) {
142- if strings .Contains (goType , "int" ) {
143- * cg .isIntId = true
144- return str
145- }
146- return fmt .Sprintf (
147- "%s %s: &id,\n " ,
148- str ,
149- name ,
150- )
148+ return str
151149 }
152150 return fmt .Sprintf (
153- "%s %s: data.%s,\n " ,
154- str ,
155- name ,
156- name ,
157- )
158- }
159-
160- func (cg * CodeGenerator ) generateEditAttributionLine (str , name , goType , _ string ) string {
161- if name == PascalCase (* cg .primaryKey ) {
162- if strings .Contains (goType , "int" ) {
163- return str
164- }
165- return fmt .Sprintf (
166- "%s %s: &id,\n " ,
167- str ,
168- name ,
169- )
170- }
171- return fmt .Sprintf (
172- "%s %s: data.%s,\n " ,
151+ "%s domain.%s = data.%s\n " ,
173152 str ,
174153 name ,
175154 name ,
@@ -188,7 +167,7 @@ func (cg *CodeGenerator) findPkOnBlocks(blocks []*hclwrite.Block) string {
188167 str = cg .getColumnFromAttrString (pkAttr )
189168 }
190169 }
191- return str
170+ return PascalCase ( str )
192171}
193172
194173func (cg * CodeGenerator ) getColumnFromAttrString (attrStr string ) string {
@@ -200,52 +179,63 @@ func (cg *CodeGenerator) getColumnFromAttrString(attrStr string) string {
200179
201180func (cg * CodeGenerator ) dbTypesToGoTypes (typo string ) string {
202181 dbTypesMap := map [string ]string {
203- " bigint" : "* int64" ,
204- " bit" : "* " ,
205- " char" : "* string" ,
206- " decimal" : "* float64" ,
207- " float" : "* float32" ,
208- " double" : "* float64" ,
209- " int" : "* int" ,
210- " longtext" : "* string" ,
211- " mediumint" : "* int" ,
212- " mediumtext" : "* string" ,
213- " smallint" : "* int16" ,
214- " text" : "* string" ,
215- " time" : "* string" ,
216- " timestamp" : "* string" ,
217- " datetime" : "* time.Time" ,
218- " date" : "* string" ,
219- " tinyint" : "* int8" ,
220- " tinytext" : "* string" ,
221- " varbinary" : "* string" ,
222- " varchar" : "* string" ,
223- " json" : "* string" ,
182+ " bigint" : "int64" ,
183+ " bit" : " " ,
184+ " char" : "string" ,
185+ " decimal" : "float64" ,
186+ " float" : "float32" ,
187+ " double" : "float64" ,
188+ " int" : "int" ,
189+ " longtext" : "string" ,
190+ " mediumint" : "int" ,
191+ " mediumtext" : "string" ,
192+ " smallint" : "int16" ,
193+ " text" : "string" ,
194+ " time" : "string" ,
195+ " timestamp" : "string" ,
196+ " datetime" : "time.Time" ,
197+ " date" : "string" ,
198+ " tinyint" : "int8" ,
199+ " tinytext" : "string" ,
200+ " varbinary" : "string" ,
201+ " varchar" : "string" ,
202+ " json" : "string" ,
224203 }
225204
226205 GolangType , ok := dbTypesMap [typo ]
227206 if ok {
228- if GolangType == "* time.Time" {
207+ if GolangType == "time.Time" {
229208 * cg .needImportTime = true
230209 }
231210 return GolangType
232211 }
233212
234213 if strings .Contains (typo , "char" ) {
235- return "* string"
214+ return "string"
236215 }
237216
238217 if strings .Contains (typo , "double" ) {
239- return "* float64"
218+ return "float64"
240219 }
241220
242221 if strings .Contains (typo , "year" ) {
243- return "* string"
222+ return "string"
244223 }
245224
246225 if strings .Contains (typo , "decimal" ) {
247- return "* float64"
226+ return "float64"
248227 }
249228
250229 return typo
251230}
231+
232+ func (cg * CodeGenerator ) verifyIsNullable (token * hclwrite.Attribute , ok bool ) bool {
233+ if ! ok {
234+ return false
235+ }
236+ value := token .Expr ().BuildTokens (nil ).Bytes ()
237+ if string (value ) == "true" {
238+ return true
239+ }
240+ return false
241+ }
0 commit comments