Skip to content

Commit dd8e260

Browse files
committed
refactor(golang,python): extract method for getting field's type
1 parent 976aad4 commit dd8e260

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/cli.js

+11
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ const lengthOfLongestString = (arr) => arr
157157
0 /* initial value */
158158
)
159159

160+
// returns user-defined variable's type or null
161+
// Accepts method.dto.fields fieldsInfo
162+
const retrieveType = (fieldsInfo, fieldName) => {
163+
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type')
164+
if (hasTypeInfo) {
165+
return fieldsInfo[fieldName].type
166+
}
167+
return null
168+
}
169+
160170
const createEndpoints = async (destDir, { lang }, config) => {
161171
const ext = lang2extension(lang)
162172
const fileName = `routes.${ext}`
@@ -299,6 +309,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
299309

300310
"placeholdersMap": placeholdersMap,
301311
"removeComments": removeComments,
312+
"retrieveType": retrieveType,
302313
}
303314
)
304315

src/templates/routes.go.ejs

+4-8
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,14 @@ function extractProperties(queryAst) {
7171
}
7272
7373
function findOutType(fieldsInfo, fieldName) {
74-
const defaultType = '*string'
75-
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type')
76-
if (!hasTypeInfo) {
77-
return defaultType
78-
}
79-
if (hasTypeInfo && fieldsInfo[fieldName].type === 'integer') {
74+
const fieldType = retrieveType(fieldsInfo, fieldName)
75+
if (fieldType === 'integer') {
8076
return '*int'
8177
}
82-
if (hasTypeInfo && fieldsInfo[fieldName].type === 'boolean') {
78+
if (fieldType === 'boolean') {
8379
return '*bool'
8480
}
85-
return defaultType
81+
return '*string'
8682
}
8783
8884
function addTypes(props, fieldsInfo) {

src/templates/routes.py.ejs

+4-9
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,15 @@ function extractProperties(queryAst) {
8787
return []
8888
}
8989
90-
// LATER: try to reduce duplication with routes.go.ejs
9190
function findOutType(fieldsInfo, fieldName) {
92-
const defaultType = 'str'
93-
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type')
94-
if (!hasTypeInfo) {
95-
return defaultType
96-
}
97-
if (fieldsInfo[fieldName].type === 'integer') {
91+
const fieldType = retrieveType(fieldsInfo, fieldName)
92+
if (fieldType === 'integer') {
9893
return 'int'
9994
}
100-
if (fieldsInfo[fieldName].type === 'boolean') {
95+
if (fieldType === 'boolean') {
10196
return 'bool'
10297
}
103-
return defaultType
98+
return 'str'
10499
}
105100
106101
// LATER: reduce duplication with routes.go.ejs

0 commit comments

Comments
 (0)