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 |
||
79 | func createTableStructString(settings *settings.Settings, db database.Database, table *database.Table) (string, string) { |
||
80 | |||
81 | 1 | var structFields strings.Builder |
|
82 | |||
83 | 1 | columnInfo := columnInfo{} |
|
84 | 1 | columns := map[string]struct{}{} |
|
85 | |||
86 | 1 | for _, column := range table.Columns { |
|
87 | |||
88 | 1 | columnName := strings.Title(column.Name) |
|
89 | 1 | if settings.IsOutputFormatCamelCase() { |
|
90 | 1 | columnName = camelCaseString(column.Name) |
|
91 | } |
||
92 | 1 | if settings.ShouldInitialism() { |
|
93 | 1 | 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 | 1 | if _, ok := columns[columnName]; ok { |
|
101 | continue |
||
102 | } |
||
103 | 1 | columns[columnName] = struct{}{} |
|
104 | |||
105 | 1 | if settings.VVerbose { |
|
106 | fmt.Printf("\t\t> %v\r\n", column.Name) |
||
107 | } |
||
108 | |||
109 | 1 | columnType, col := mapDbColumnTypeToGoType(settings, db, column) |
|
110 | |||
111 | // save that we saw types of columns at least once |
||
112 | 1 | if !columnInfo.isTemporal { |
|
113 | 1 | columnInfo.isTemporal = col.isTemporal |
|
114 | } |
||
115 | 1 | if !columnInfo.isNullableTemporal { |
|
116 | 1 | columnInfo.isNullableTemporal = col.isNullableTemporal |
|
117 | } |
||
118 | 1 | if !columnInfo.isNullablePrimitive { |
|
119 | 1 | columnInfo.isNullablePrimitive = col.isNullablePrimitive |
|
120 | } |
||
121 | |||
122 | 1 | structFields.WriteString(columnName) |
|
123 | 1 | structFields.WriteString(" ") |
|
124 | 1 | structFields.WriteString(columnType) |
|
125 | 1 | structFields.WriteString(" ") |
|
126 | 1 | structFields.WriteString(taggers.GenerateTag(db, column)) |
|
127 | 1 | structFields.WriteString("\n") |
|
128 | } |
||
129 | |||
130 | 1 | if settings.IsMastermindStructableRecorder { |
|
131 | structFields.WriteString("\t\nstructable.Recorder\n") |
||
132 | } |
||
133 | |||
134 | 1 | var fileContent strings.Builder |
|
135 | |||
136 | // write header infos |
||
137 | 1 | fileContent.WriteString("package ") |
|
138 | 1 | fileContent.WriteString(settings.PackageName) |
|
139 | 1 | fileContent.WriteString("\n\n") |
|
140 | |||
141 | // write imports |
||
142 | 1 | generateImports(&fileContent, settings, db, columnInfo) |
|
143 | |||
144 | 1 | tableName := strings.Title(settings.Prefix + table.Name + settings.Suffix) |
|
145 | 1 | if settings.IsOutputFormatCamelCase() { |
|
146 | 1 | tableName = camelCaseString(tableName) |
|
147 | } |
||
148 | |||
149 | // write struct with fields |
||
150 | 1 | fileContent.WriteString("type ") |
|
151 | 1 | fileContent.WriteString(tableName) |
|
152 | 1 | fileContent.WriteString(" struct {\n") |
|
153 | 1 | fileContent.WriteString(structFields.String()) |
|
154 | 1 | fileContent.WriteString("}") |
|
155 | |||
156 | 1 | return tableName, fileContent.String() |
|
157 | } |
||
272 |