| Conditions | 15 |
| Total Lines | 98 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 45 |
| CRAP Score | 15.7731 |
| 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 |
||
| 94 | func createTableStructString(settings *settings.Settings, db database.Database, table *database.Table) (string, string, error) { |
||
| 95 | |||
| 96 | 1 | var structFields strings.Builder |
|
| 97 | 1 | tableName := strings.Title(settings.Prefix + table.Name + settings.Suffix) |
|
| 98 | // Replace any whitespace with underscores |
||
| 99 | 1 | tableName = strings.Map(replaceSpace, tableName) |
|
| 100 | 1 | if settings.IsOutputFormatCamelCase() { |
|
| 101 | 1 | tableName = camelCaseString(tableName) |
|
| 102 | } |
||
| 103 | |||
| 104 | // Check that the table name doesn't contain any invalid characters for Go variables |
||
| 105 | 1 | if !validVariableName(tableName) { |
|
| 106 | return "", "", fmt.Errorf("Table name %q contains invalid characters", table.Name) |
||
| 107 | } |
||
| 108 | |||
| 109 | 1 | columnInfo := columnInfo{} |
|
| 110 | 1 | columns := map[string]struct{}{} |
|
| 111 | |||
| 112 | 1 | for _, column := range table.Columns { |
|
| 113 | |||
| 114 | 1 | columnName := strings.Title(column.Name) |
|
| 115 | // Replace any whitespace with underscores |
||
| 116 | 1 | columnName = strings.Map(replaceSpace, columnName) |
|
| 117 | 1 | if settings.IsOutputFormatCamelCase() { |
|
| 118 | 1 | columnName = camelCaseString(columnName) |
|
| 119 | } |
||
| 120 | 1 | if settings.ShouldInitialism() { |
|
| 121 | 1 | columnName = toInitialisms(columnName) |
|
| 122 | } |
||
| 123 | |||
| 124 | // Check that the column name doesn't contain any invalid characters for Go variables |
||
| 125 | 1 | if !validVariableName(columnName) { |
|
| 126 | return "", "", fmt.Errorf("Column name %q in table %q contains invalid characters", column.Name, table.Name) |
||
| 127 | } |
||
| 128 | // First character of an identifier in Go must be letter or _ |
||
| 129 | // We want it to be an uppercase letter to be a public field |
||
| 130 | 1 | if !unicode.IsLetter([]rune(columnName)[0]) { |
|
| 131 | if settings.Verbose { |
||
| 132 | fmt.Printf("\t\t>Column %q in table %q doesn't start with a letter; prepending with X_\n", column.Name, table.Name) |
||
| 133 | } |
||
| 134 | columnName = "X_" + columnName |
||
| 135 | } |
||
| 136 | // ISSUE-4: if columns are part of multiple constraints |
||
| 137 | // then the sql returns multiple rows per column name. |
||
| 138 | // Therefore we check if we already added a column with |
||
| 139 | // that name to the struct, if so, skip. |
||
| 140 | 1 | if _, ok := columns[columnName]; ok { |
|
| 141 | continue |
||
| 142 | } |
||
| 143 | 1 | columns[columnName] = struct{}{} |
|
| 144 | |||
| 145 | 1 | if settings.VVerbose { |
|
| 146 | fmt.Printf("\t\t> %v\r\n", column.Name) |
||
| 147 | } |
||
| 148 | |||
| 149 | 1 | columnType, col := mapDbColumnTypeToGoType(settings, db, column) |
|
| 150 | |||
| 151 | // save that we saw types of columns at least once |
||
| 152 | 1 | if !columnInfo.isTemporal { |
|
| 153 | 1 | columnInfo.isTemporal = col.isTemporal |
|
| 154 | } |
||
| 155 | 1 | if !columnInfo.isNullableTemporal { |
|
| 156 | 1 | columnInfo.isNullableTemporal = col.isNullableTemporal |
|
| 157 | } |
||
| 158 | 1 | if !columnInfo.isNullablePrimitive { |
|
| 159 | 1 | columnInfo.isNullablePrimitive = col.isNullablePrimitive |
|
| 160 | } |
||
| 161 | |||
| 162 | 1 | structFields.WriteString(columnName) |
|
| 163 | 1 | structFields.WriteString(" ") |
|
| 164 | 1 | structFields.WriteString(columnType) |
|
| 165 | 1 | structFields.WriteString(" ") |
|
| 166 | 1 | structFields.WriteString(taggers.GenerateTag(db, column)) |
|
| 167 | 1 | structFields.WriteString("\n") |
|
| 168 | } |
||
| 169 | |||
| 170 | 1 | if settings.IsMastermindStructableRecorder { |
|
| 171 | structFields.WriteString("\t\nstructable.Recorder\n") |
||
| 172 | } |
||
| 173 | |||
| 174 | 1 | var fileContent strings.Builder |
|
| 175 | |||
| 176 | // write header infos |
||
| 177 | 1 | fileContent.WriteString("package ") |
|
| 178 | 1 | fileContent.WriteString(settings.PackageName) |
|
| 179 | 1 | fileContent.WriteString("\n\n") |
|
| 180 | |||
| 181 | // write imports |
||
| 182 | 1 | generateImports(&fileContent, settings, db, columnInfo) |
|
| 183 | |||
| 184 | // write struct with fields |
||
| 185 | 1 | fileContent.WriteString("type ") |
|
| 186 | 1 | fileContent.WriteString(tableName) |
|
| 187 | 1 | fileContent.WriteString(" struct {\n") |
|
| 188 | 1 | fileContent.WriteString(structFields.String()) |
|
| 189 | 1 | fileContent.WriteString("}") |
|
| 190 | |||
| 191 | 1 | return tableName, fileContent.String(), nil |
|
| 192 | } |
||
| 332 |