Skip to content

Commit 9e28321

Browse files
committed
chore(golang): improve code formatting
Part of #9
1 parent cae5201 commit 9e28321

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

examples/go/routes.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
9595
json.NewDecoder(r.Body).Decode(&dto)
9696

9797
args := map[string]interface{}{
98-
"name": dto.Name,
98+
"name": dto.Name,
9999
"name_ru": dto.NameRu,
100-
"slug": dto.Slug,
100+
"slug": dto.Slug,
101101
"user_id": dto.UserId,
102102
}
103103
_, err := db.NamedExec(
@@ -143,10 +143,10 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
143143
json.NewDecoder(r.Body).Decode(&dto)
144144

145145
args := map[string]interface{}{
146-
"name": dto.Name,
147-
"name_ru": dto.NameRu,
148-
"slug": dto.Slug,
149-
"user_id": dto.UserId,
146+
"name": dto.Name,
147+
"name_ru": dto.NameRu,
148+
"slug": dto.Slug,
149+
"user_id": dto.UserId,
150150
"categoryId": chi.URLParam(r, "categoryId"),
151151
}
152152
_, err := db.NamedExec(

src/cli.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ const snake2camelCase = (str) => str.replace(/_([a-z])/g, (match, group1) => gro
9494
// (used only with Golang's go-chi)
9595
const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
9696

97+
// ["a", "bb", "ccc"] => 3
98+
// (used only with Golang's go-chi)
99+
const lengthOfLongestString = (arr) => arr
100+
.map(el => el.length)
101+
.reduce(
102+
(acc, val) => val > acc ? val : acc,
103+
0 /* initial value */
104+
);
105+
97106
const createEndpoints = async (destDir, lang, config) => {
98107
const fileName = `routes.${lang}`
99108
console.log('Generate', fileName);
@@ -156,20 +165,25 @@ const createEndpoints = async (destDir, lang, config) => {
156165
"removePlaceholders": removePlaceholders,
157166
"snake2camelCase": snake2camelCase,
158167
"capitalize": capitalize,
168+
"lengthOfLongestString": lengthOfLongestString,
159169

160170
// [ "p.page", "b.num" ] => '"page": dto.Page),\n\t\t\t"num": dto.Num),'
161171
// (used only with Golang's go-chi)
162172
"formatParamsAsGolangMap": (params) => {
163173
if (params.length === 0) {
164174
return params;
165175
}
176+
const maxParamNameLength = lengthOfLongestString(params);
166177
return Array.from(
167178
new Set(params),
168179
p => {
169180
const bindTarget = p.substring(0, 1);
170181
const paramName = p.substring(2);
171182
const formatFunc = placeholdersMap['go'][bindTarget];
172-
return `"${paramName}": ${formatFunc(paramName)},`
183+
const quotedParam = '"' + paramName + '":';
184+
// We don't count quotes and colon because they are compensated by "p." prefix.
185+
// We do +1 because the longest parameter will also have an extra space as a delimiter.
186+
return `${quotedParam.padEnd(maxParamNameLength+1)} ${formatFunc(paramName)},`
173187
}
174188
).join('\n\t\t\t');
175189
},

src/templates/routes.go.ejs

+1-15
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,14 @@ function query2dto(parser, method) {
106106
"hasUserProvidedName": hasName,
107107
"props": propsWithTypes,
108108
// max length is needed for proper formatting
109-
"maxFieldNameLength": lengthOfLongestString(props),
109+
"maxFieldNameLength": lengthOfLongestString(props.map(el => el.indexOf('_') < 0 ? el : el.replace(/_/g, ''))),
110110
// required for de-duplication
111111
// [ {name:foo, type:int}, {name:bar, type:string} ] => "foo=int bar=string"
112112
// TODO: sort before join
113113
"signature": propsWithTypes.map(field => `${field.name}=${field.type}`).join(' ')
114114
};
115115
}
116116
117-
// ["a", "b__b", "ccc"] => 3
118-
// Note that it doesn't count underscores.
119-
function lengthOfLongestString(arr) {
120-
return arr
121-
.map(el => el.indexOf('_') < 0 ? el : el.replace(/_/g, ''))
122-
.map(el => el.length)
123-
.reduce(
124-
(acc, val) => val > acc ? val : acc,
125-
0 /* initial value */
126-
);
127-
}
128-
129117
function dto2struct(dto) {
130118
let result = `type ${dto.name} struct {\n`;
131119
dto.props.forEach(prop => {
@@ -225,7 +213,6 @@ endpoints.forEach(function(endpoint) {
225213
// TODO: do we really need signature and cache?
226214
const cacheKey = dto ? dto.signature : null;
227215
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
228-
// TODO: align args properly (like gofmt does)
229216
%>
230217
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
231218
var dto <%- dataType %>
@@ -253,7 +240,6 @@ endpoints.forEach(function(endpoint) {
253240
// TODO: do we really need signature and cache?
254241
const cacheKey = dto ? dto.signature : null;
255242
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
256-
// TODO: align args properly (like gofmt does)
257243
%>
258244
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
259245
var dto <%- dataType %>

0 commit comments

Comments
 (0)