Conditions | 11 |
Total Lines | 78 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 11.0411 |
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 |
||
86 | func createTableStructString(settings *settings.Settings, db database.Database, table *database.Table) (string, string) { |
||
87 | |||
88 | 1 | var structFields strings.Builder |
|
89 | |||
90 | 1 | columnInfo := columnInfo{} |
|
91 | 1 | columns := map[string]struct{}{} |
|
92 | |||
93 | 1 | for _, column := range table.Columns { |
|
94 | |||
95 | 1 | columnName := strings.Title(column.Name) |
|
96 | 1 | if settings.IsOutputFormatCamelCase() { |
|
97 | 1 | columnName = camelCaseString(column.Name) |
|
98 | } |
||
99 | 1 | if settings.ShouldInitialism() { |
|
100 | 1 | columnName = toInitialisms(columnName) |
|
101 | } |
||
102 | |||
103 | // ISSUE-4: if columns are part of multiple constraints |
||
104 | // then the sql returns multiple rows per column name. |
||
105 | // Therefore we check if we already added a column with |
||
106 | // that name to the struct, if so, skip. |
||
107 | 1 | if _, ok := columns[columnName]; ok { |
|
108 | continue |
||
109 | } |
||
110 | 1 | columns[columnName] = struct{}{} |
|
111 | |||
112 | 1 | if settings.VVerbose { |
|
113 | fmt.Printf("\t\t> %v\r\n", column.Name) |
||
114 | } |
||
115 | |||
116 | 1 | columnType, col := mapDbColumnTypeToGoType(settings, db, column) |
|
117 | |||
118 | // save that we saw types of columns at least once |
||
119 | 1 | if !columnInfo.isTemporal { |
|
120 | 1 | columnInfo.isTemporal = col.isTemporal |
|
121 | } |
||
122 | 1 | if !columnInfo.isNullableTemporal { |
|
123 | 1 | columnInfo.isNullableTemporal = col.isNullableTemporal |
|
124 | } |
||
125 | 1 | if !columnInfo.isNullablePrimitive { |
|
126 | 1 | columnInfo.isNullablePrimitive = col.isNullablePrimitive |
|
127 | } |
||
128 | |||
129 | 1 | structFields.WriteString(columnName) |
|
130 | 1 | structFields.WriteString(" ") |
|
131 | 1 | structFields.WriteString(columnType) |
|
132 | 1 | structFields.WriteString(" ") |
|
133 | 1 | structFields.WriteString(taggers.GenerateTag(db, column)) |
|
134 | 1 | structFields.WriteString("\n") |
|
135 | } |
||
136 | |||
137 | 1 | if settings.IsMastermindStructableRecorder { |
|
138 | structFields.WriteString("\t\nstructable.Recorder\n") |
||
139 | } |
||
140 | |||
141 | 1 | var fileContent strings.Builder |
|
142 | |||
143 | // write header infos |
||
144 | 1 | fileContent.WriteString("package ") |
|
145 | 1 | fileContent.WriteString(settings.PackageName) |
|
146 | 1 | fileContent.WriteString("\n\n") |
|
147 | |||
148 | // write imports |
||
149 | 1 | generateImports(&fileContent, settings, db, columnInfo) |
|
150 | |||
151 | 1 | tableName := strings.Title(settings.Prefix + table.Name + settings.Suffix) |
|
152 | 1 | if settings.IsOutputFormatCamelCase() { |
|
153 | 1 | tableName = camelCaseString(tableName) |
|
154 | } |
||
155 | |||
156 | // write struct with fields |
||
157 | 1 | fileContent.WriteString("type ") |
|
158 | 1 | fileContent.WriteString(tableName) |
|
159 | 1 | fileContent.WriteString(" struct {\n") |
|
160 | 1 | fileContent.WriteString(structFields.String()) |
|
161 | 1 | fileContent.WriteString("}") |
|
162 | |||
163 | 1 | return tableName, fileContent.String() |
|
164 | } |
||
284 |