Skip to content

Commit b13625b

Browse files
committed
chore(golang): dto.name should always be used even if another struct can be used instead
Part of #9
1 parent 16279ae commit b13625b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

examples/go/routes.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ type CreateCategoryDto struct {
2727
UserId *string `json:"user_id,omitempty" db:"user_id"`
2828
}
2929

30+
type CategoryInfoDto struct {
31+
Id *string `json:"id,omitempty" db:"id"`
32+
Name *string `json:"name,omitempty" db:"name"`
33+
NameRu *string `json:"name_ru,omitempty" db:"name_ru"`
34+
Slug *string `json:"slug,omitempty" db:"slug"`
35+
}
36+
3037
func registerRoutes(r chi.Router, db *sqlx.DB) {
3138
categories := make(map[int]CategoryDto)
3239
cnt := 0
@@ -104,7 +111,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
104111
return
105112
}
106113

107-
var result CategoryDto
114+
var result CategoryInfoDto
108115
args := map[string]interface{}{
109116
"categoryId": chi.URLParam(r, "categoryId"),
110117
}

examples/js/endpoints.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
, slug
5757
FROM categories
5858
WHERE id = :p.categoryId
59+
dto:
60+
name: CategoryInfoDto
5961
put:
6062
query: >-
6163
UPDATE categories

src/templates/routes.go.ejs

+7-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function query2dto(parser, method) {
9595
const name = hasName ? method.dto.name : "Dto" + ++globalDtoCounter;
9696
return {
9797
"name": name,
98+
"hasUserProvidedName": hasName,
9899
"props": propsWithTypes,
99100
// max length is needed for proper formatting
100101
"maxFieldNameLength": lengthOfLongestString(props),
@@ -146,6 +147,10 @@ function cacheDto(dto) {
146147
return dto;
147148
}
148149
function dtoInCache(dto) {
150+
// always prefer user specified name even when we have a similar DTO in cache
151+
if (dto.hasUserProvidedName) {
152+
return false;
153+
}
149154
return dtoCache.hasOwnProperty(dto.signature);
150155
}
151156
@@ -178,7 +183,8 @@ endpoints.forEach(function(endpoint) {
178183
const dto = query2dto(sqlParser, method);
179184
// TODO: do we really need signature and cache?
180185
const cacheKey = dto ? dto.signature : null;
181-
const dataType = hasGetMany ? '[]' + dtoCache[cacheKey] : dtoCache[cacheKey];
186+
const dtoName = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
187+
const dataType = hasGetMany ? '[]' + dtoName : dtoName;
182188
183189
const params = extractParams(method.query);
184190
const formattedParams = formatParamsAsGolangVararg(params);

0 commit comments

Comments
 (0)