Skip to content

Commit 584d9f0

Browse files
committed
fix(golang): correct struct format when a member of type int/bool is present
1 parent ff69f70 commit 584d9f0

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

examples/go/chi/mysql/routes.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ type CounterDto struct {
1414
}
1515

1616
type CategoryDto struct {
17-
Id *int `json:"id" db:"id"`
17+
Id *int `json:"id" db:"id"`
1818
Name *string `json:"name" db:"name"`
1919
NameRu *string `json:"name_ru" db:"name_ru"`
2020
Slug *string `json:"slug" db:"slug"`
21-
Hidden *bool `json:"hidden" db:"hidden"`
21+
Hidden *bool `json:"hidden" db:"hidden"`
2222
}
2323

2424
type CreateCategoryDto struct {
2525
Name *string `json:"name" db:"name"`
2626
NameRu *string `json:"name_ru" db:"name_ru"`
2727
Slug *string `json:"slug" db:"slug"`
28-
Hidden *bool `json:"hidden" db:"hidden"`
29-
UserId *int `json:"user_id" db:"user_id"`
28+
Hidden *bool `json:"hidden" db:"hidden"`
29+
UserId *int `json:"user_id" db:"user_id"`
3030
}
3131

3232
func registerRoutes(r chi.Router, db *sqlx.DB) {

src/templates/routes.go.ejs

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ function query2dto(parser, method) {
112112
"name": name,
113113
"hasUserProvidedName": hasName,
114114
"props": propsWithTypes,
115-
// max length is needed for proper formatting
115+
// max lengths are needed for proper formatting
116116
"maxFieldNameLength": lengthOfLongestString(props.map(el => el.indexOf('_') < 0 ? el : el.replace(/_/g, ''))),
117+
"maxFieldTypeLength": lengthOfLongestString(propsWithTypes.map(el => el.type)),
117118
// required for de-duplication
118119
// [ {name:foo, type:int}, {name:bar, type:string} ] => "foo=int bar=string"
119120
// LATER: sort before join
@@ -125,7 +126,8 @@ function dto2struct(dto) {
125126
let result = `type ${dto.name} struct {\n`
126127
dto.props.forEach(prop => {
127128
const fieldName = capitalize(snake2camelCase(prop.name)).padEnd(dto.maxFieldNameLength)
128-
result += `\t${fieldName} ${prop.type} \`json:"${prop.name}" db:"${prop.name}"\`\n`
129+
const fieldType = prop.type.padEnd(dto.maxFieldTypeLength)
130+
result += `\t${fieldName} ${fieldType} \`json:"${prop.name}" db:"${prop.name}"\`\n`
129131
})
130132
result += '}\n'
131133

0 commit comments

Comments
 (0)