|
| 1 | +package generator |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/hcl/v2" |
| 6 | + "github.com/hashicorp/hcl/v2/hclwrite" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func (cg *CodeGenerator) ReadFromSchema(schema string, table string) { |
| 12 | + file, hclErr := cg.getHclFile(schema) |
| 13 | + if hclErr != nil { |
| 14 | + fmt.Println("Error validating files:", hclErr) |
| 15 | + return |
| 16 | + } |
| 17 | + for _, block := range file.Body().Blocks() { |
| 18 | + if table != "" && block.Labels()[0] != table { |
| 19 | + continue |
| 20 | + } |
| 21 | + err := cg.handleHclBlock(block) |
| 22 | + if err != nil { |
| 23 | + fmt.Println("Error validating files:", err) |
| 24 | + return |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (cg *CodeGenerator) getHclFile(schema string) (*hclwrite.File, error) { |
| 30 | + filepath := fmt.Sprintf("tools/migrator/schema/%s.my.hcl", schema) |
| 31 | + content, err := os.ReadFile(filepath) |
| 32 | + if err != nil { |
| 33 | + fmt.Println("Error reading file:", err) |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + file, _ := hclwrite.ParseConfig(content, filepath, hcl.Pos{Line: 1, Column: 1}) |
| 37 | + return file, err |
| 38 | +} |
| 39 | + |
| 40 | +func (cg *CodeGenerator) handleHclBlock(block *hclwrite.Block) error { |
| 41 | + if block.Type() == "schema" { |
| 42 | + return nil |
| 43 | + } |
| 44 | + if len(block.Labels()) == 0 { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + tableName := block.Labels()[0] |
| 49 | + replacers := cg.generateDomainFromHclBlock(block, tableName) |
| 50 | + validateErr := cg.validateFiles(tableName) |
| 51 | + if validateErr != nil { |
| 52 | + return validateErr |
| 53 | + } |
| 54 | + stubs := GetStubsConfig(cg.Logger, cg.config, cg.domainType) |
| 55 | + cg.GenerateFilesFromStubs(stubs, replacers) |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (cg *CodeGenerator) generateDomainFromHclBlock(block *hclwrite.Block, tableName string) map[string]string { |
| 60 | + cg.needImportTime = new(bool) |
| 61 | + cg.primaryKey = new(string) |
| 62 | + cg.pkType = new(string) |
| 63 | + cg.isIntId = new(bool) |
| 64 | + *cg.needImportTime = false |
| 65 | + *cg.isIntId = false |
| 66 | + domain := cg.generateDomainStruct(block.Body().Blocks(), tableName, cg.primaryKey, cg.pkType) |
| 67 | + 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) |
| 70 | + replacers := GetReplacersConfig(cg.config, cg.domainType, []string{tableName}) |
| 71 | + replacers["{{domainType}}"] = domain |
| 72 | + replacers["{{dataType}}"] = dataType |
| 73 | + replacers["{{pkDbName}}"] = *cg.primaryKey |
| 74 | + replacers["{{pkName}}"] = PascalCase(*cg.primaryKey) |
| 75 | + replacers["{{pkType}}"] = *cg.pkType |
| 76 | + replacers["{{createServiceData}}"] = createAttrData |
| 77 | + replacers["{{editServiceData}}"] = editAttrData |
| 78 | + if *cg.needImportTime { |
| 79 | + replacers["{{optionalImports}}"] = "\"time\"" |
| 80 | + } |
| 81 | + if !*cg.isIntId { |
| 82 | + replacers["{{idVar}}"] = "id := s.idCreator.Create()" |
| 83 | + } |
| 84 | + return replacers |
| 85 | +} |
| 86 | + |
| 87 | +func (cg *CodeGenerator) generateDomainStruct(blocks []*hclwrite.Block, tableName string, pk, pkType *string) string { |
| 88 | + *pk = cg.findPkOnBlocks(blocks) |
| 89 | + structString := "type " + PascalCase(tableName) + " struct {\n" |
| 90 | + structString += cg.generateStruct(blocks, pk, pkType, cg.generateDeclarationLine) |
| 91 | + structString += "}" |
| 92 | + return structString |
| 93 | +} |
| 94 | + |
| 95 | +func (cg *CodeGenerator) generateStruct(blocks []*hclwrite.Block, pk, pkType *string, strFormationFunc func(string, string, string, string) string) string { |
| 96 | + declarationString := "\n" |
| 97 | + for _, block := range blocks { |
| 98 | + if block.Type() == "column" { |
| 99 | + token, ok := block.Body().Attributes()["type"] |
| 100 | + if !ok { |
| 101 | + continue |
| 102 | + } |
| 103 | + tokenStr := string(token.Expr().BuildTokens(nil).Bytes()) |
| 104 | + goType := cg.dbTypesToGoTypes(tokenStr) |
| 105 | + |
| 106 | + if pk != nil && block.Labels()[0] == *pk { |
| 107 | + *pkType = fmt.Sprintf("%s string `param:\"id\"`", PascalCase(*pk)) |
| 108 | + } |
| 109 | + |
| 110 | + declarationString = strFormationFunc( |
| 111 | + declarationString, |
| 112 | + PascalCase(block.Labels()[0]), |
| 113 | + goType, |
| 114 | + block.Labels()[0], |
| 115 | + ) |
| 116 | + } |
| 117 | + } |
| 118 | + return declarationString |
| 119 | +} |
| 120 | + |
| 121 | +func (cg *CodeGenerator) generateDeclarationLine(str, name, goType, dbTag string) string { |
| 122 | + if name == PascalCase(*cg.primaryKey) && strings.Contains(goType, "int") { |
| 123 | + return fmt.Sprintf( |
| 124 | + "%s %s %s `db:\"%s\"`\n", |
| 125 | + str, |
| 126 | + name, |
| 127 | + "*string", |
| 128 | + dbTag, |
| 129 | + ) |
| 130 | + } |
| 131 | + return fmt.Sprintf( |
| 132 | + "%s %s %s `db:\"%s\"`\n", |
| 133 | + str, |
| 134 | + name, |
| 135 | + goType, |
| 136 | + dbTag, |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +func (cg *CodeGenerator) generateCreateAttributionLine(str, name, goType, _ string) string { |
| 141 | + 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 | + ) |
| 151 | + } |
| 152 | + 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", |
| 173 | + str, |
| 174 | + name, |
| 175 | + name, |
| 176 | + ) |
| 177 | +} |
| 178 | + |
| 179 | +func (cg *CodeGenerator) findPkOnBlocks(blocks []*hclwrite.Block) string { |
| 180 | + str := "" |
| 181 | + for _, block := range blocks { |
| 182 | + if block.Type() == "primary_key" { |
| 183 | + token, ok := block.Body().Attributes()["columns"] |
| 184 | + if !ok { |
| 185 | + return "" |
| 186 | + } |
| 187 | + pkAttr := string(token.Expr().BuildTokens(nil).Bytes()) |
| 188 | + str = cg.getColumnFromAttrString(pkAttr) |
| 189 | + } |
| 190 | + } |
| 191 | + return str |
| 192 | +} |
| 193 | + |
| 194 | +func (cg *CodeGenerator) getColumnFromAttrString(attrStr string) string { |
| 195 | + str := strings.Replace(attrStr, "[", "", -1) |
| 196 | + str = strings.Replace(str, "]", "", -1) |
| 197 | + str = strings.Split(str, ".")[1] |
| 198 | + return str |
| 199 | +} |
| 200 | + |
| 201 | +func (cg *CodeGenerator) dbTypesToGoTypes(typo string) string { |
| 202 | + 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", |
| 224 | + } |
| 225 | + |
| 226 | + GolangType, ok := dbTypesMap[typo] |
| 227 | + if ok { |
| 228 | + if GolangType == "*time.Time" { |
| 229 | + *cg.needImportTime = true |
| 230 | + } |
| 231 | + return GolangType |
| 232 | + } |
| 233 | + |
| 234 | + if strings.Contains(typo, "char") { |
| 235 | + return "*string" |
| 236 | + } |
| 237 | + |
| 238 | + if strings.Contains(typo, "double") { |
| 239 | + return "*float64" |
| 240 | + } |
| 241 | + |
| 242 | + if strings.Contains(typo, "year") { |
| 243 | + return "*string" |
| 244 | + } |
| 245 | + |
| 246 | + if strings.Contains(typo, "decimal") { |
| 247 | + return "*float64" |
| 248 | + } |
| 249 | + |
| 250 | + return typo |
| 251 | +} |
0 commit comments