| Conditions | 11 |
| Total Lines | 78 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like cli.createTableStructString often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package cli |
||
| 79 | func createTableStructString(settings *config.Settings, db database.Database, table *database.Table) (string, string) { |
||
| 80 | |||
| 81 | var structFields strings.Builder |
||
| 82 | |||
| 83 | columnInfo := columnInfo{} |
||
| 84 | columns := map[string]struct{}{} |
||
| 85 | |||
| 86 | for _, column := range table.Columns { |
||
| 87 | |||
| 88 | columnName := strings.Title(column.Name) |
||
| 89 | if settings.IsOutputFormatCamelCase() { |
||
| 90 | columnName = camelCaseString(column.Name) |
||
| 91 | } |
||
| 92 | if settings.ShouldInitialism() { |
||
| 93 | columnName = toInitialisms(columnName) |
||
| 94 | } |
||
| 95 | |||
| 96 | // ISSUE-4: if columns are part of multiple constraints |
||
| 97 | // then the sql returns multiple rows per column name. |
||
| 98 | // Therefore we check if we already added a column with |
||
| 99 | // that name to the struct, if so, skip. |
||
| 100 | if _, ok := columns[columnName]; ok { |
||
| 101 | continue |
||
| 102 | } |
||
| 103 | columns[columnName] = struct{}{} |
||
| 104 | |||
| 105 | if settings.VVerbose { |
||
| 106 | fmt.Printf("\t\t> %v\r\n", column.Name) |
||
| 107 | } |
||
| 108 | |||
| 109 | columnType, col := mapDbColumnTypeToGoType(settings, db, column) |
||
| 110 | |||
| 111 | // save that we saw types of columns at least once |
||
| 112 | if !columnInfo.isTemporal { |
||
| 113 | columnInfo.isTemporal = col.isTemporal |
||
| 114 | } |
||
| 115 | if !columnInfo.isNullableTemporal { |
||
| 116 | columnInfo.isNullableTemporal = col.isNullableTemporal |
||
| 117 | } |
||
| 118 | if !columnInfo.isNullablePrimitive { |
||
| 119 | columnInfo.isNullablePrimitive = col.isNullablePrimitive |
||
| 120 | } |
||
| 121 | |||
| 122 | structFields.WriteString(columnName) |
||
| 123 | structFields.WriteString(" ") |
||
| 124 | structFields.WriteString(columnType) |
||
| 125 | structFields.WriteString(" ") |
||
| 126 | structFields.WriteString(taggers.GenerateTag(db, column)) |
||
| 127 | structFields.WriteString("\n") |
||
| 128 | } |
||
| 129 | |||
| 130 | if settings.IsMastermindStructableRecorder { |
||
| 131 | structFields.WriteString("\t\nstructable.Recorder\n") |
||
| 132 | } |
||
| 133 | |||
| 134 | var fileContent strings.Builder |
||
| 135 | |||
| 136 | // write header infos |
||
| 137 | fileContent.WriteString("package ") |
||
| 138 | fileContent.WriteString(settings.PackageName) |
||
| 139 | fileContent.WriteString("\n\n") |
||
| 140 | |||
| 141 | // write imports |
||
| 142 | generateImports(&fileContent, settings, db, columnInfo) |
||
| 143 | |||
| 144 | tableName := strings.Title(settings.Prefix + table.Name + settings.Suffix) |
||
| 145 | if settings.IsOutputFormatCamelCase() { |
||
| 146 | tableName = camelCaseString(tableName) |
||
| 147 | } |
||
| 148 | |||
| 149 | // write struct with fields |
||
| 150 | fileContent.WriteString("type ") |
||
| 151 | fileContent.WriteString(tableName) |
||
| 152 | fileContent.WriteString(" struct {\n") |
||
| 153 | fileContent.WriteString(structFields.String()) |
||
| 154 | fileContent.WriteString("}") |
||
| 155 | |||
| 156 | return tableName, fileContent.String() |
||
| 157 | } |
||
| 272 |