Skip to content

Commit 16279ae

Browse files
committed
chore: add support for specifying DTO name with dto.name property
For example, for the following mapping: - path: /v1/categories/count get: query: SELECT COUNT(*) AS counter FROM categories dto: name: CounterDto the generated struct will be named "CounterDto" instead of "Dto<N>". Part of #9
1 parent 793e4fb commit 16279ae

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

examples/go/routes.go

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,30 @@ import "strconv"
99
import "github.com/go-chi/chi"
1010
import "github.com/jmoiron/sqlx"
1111

12-
type Category struct {
13-
Id int `json:"id" db:"id"`
14-
Name string `json:"name" db:"name"`
15-
NameRu *string `json:"name_ru" db:"name_ru"`
16-
Slug string `json:"slug" db:"slug"`
17-
}
18-
19-
type Dto1 struct {
12+
type CounterDto struct {
2013
Counter *string `json:"counter,omitempty" db:"counter"`
2114
}
2215

23-
type Dto3 struct {
16+
type CategoryDto struct {
2417
Id *string `json:"id,omitempty" db:"id"`
2518
Name *string `json:"name,omitempty" db:"name"`
2619
NameRu *string `json:"name_ru,omitempty" db:"name_ru"`
2720
Slug *string `json:"slug,omitempty" db:"slug"`
2821
}
2922

30-
type Dto4 struct {
23+
type CreateCategoryDto struct {
3124
Name *string `json:"name,omitempty" db:"name"`
3225
NameRu *string `json:"name_ru,omitempty" db:"name_ru"`
3326
Slug *string `json:"slug,omitempty" db:"slug"`
3427
UserId *string `json:"user_id,omitempty" db:"user_id"`
3528
}
3629

3730
func registerRoutes(r chi.Router, db *sqlx.DB) {
38-
categories := make(map[int]Category)
31+
categories := make(map[int]CategoryDto)
3932
cnt := 0
4033

4134
r.Get("/v1/categories/count", func(w http.ResponseWriter, r *http.Request) {
42-
var result Dto1
35+
var result CounterDto
4336
err := db.Get(&result, "SELECT COUNT(*) AS counter FROM categories")
4437
switch err {
4538
case sql.ErrNoRows:
@@ -61,7 +54,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
6154
return
6255
}
6356

64-
var result Dto1
57+
var result CounterDto
6558
args := map[string]interface{}{
6659
"collectionId": chi.URLParam(r, "collectionId"),
6760
}
@@ -79,7 +72,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
7972
})
8073

8174
r.Get("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
82-
var result []Dto3
75+
var result []CategoryDto
8376
err := db.Select(&result, "SELECT id , name , name_ru , slug FROM categories")
8477
switch err {
8578
case sql.ErrNoRows:
@@ -94,10 +87,11 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
9487
})
9588

9689
r.Post("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
97-
var category Category
90+
var category CategoryDto
9891
json.NewDecoder(r.Body).Decode(&category)
9992
cnt += 1
100-
category.Id = cnt
93+
id := strconv.Itoa(cnt)
94+
category.Id = &id
10195
categories[cnt] = category
10296
w.WriteHeader(http.StatusNoContent)
10397
})
@@ -110,7 +104,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
110104
return
111105
}
112106

113-
var result Dto3
107+
var result CategoryDto
114108
args := map[string]interface{}{
115109
"categoryId": chi.URLParam(r, "categoryId"),
116110
}
@@ -129,7 +123,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
129123

130124
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
131125
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
132-
var category Category
126+
var category CategoryDto
133127
json.NewDecoder(r.Body).Decode(&category)
134128
categories[id] = category
135129
w.WriteHeader(http.StatusNoContent)

examples/js/endpoints.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
- path: /v1/categories/count
22
get:
33
query: SELECT COUNT(*) AS counter FROM categories
4+
dto:
5+
name: CounterDto
46

57
- path: /v1/collections/:collectionId/categories/count
68
get:
@@ -19,6 +21,8 @@
1921
, name_ru
2022
, slug
2123
FROM categories
24+
dto:
25+
name: CategoryDto
2226
post:
2327
query: >-
2428
INSERT
@@ -40,6 +44,8 @@
4044
, NOW()
4145
, :b.user_id
4246
)
47+
dto:
48+
name: CreateCategoryDto
4349

4450
- path: /v1/categories/:categoryId
4551
get:

src/templates/routes.go.ejs

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ import "strconv"
99
import "github.com/go-chi/chi"
1010
import "github.com/jmoiron/sqlx"
1111

12-
type Category struct {
13-
Id int `json:"id" db:"id"`
14-
Name string `json:"name" db:"name"`
15-
NameRu *string `json:"name_ru" db:"name_ru"`
16-
Slug string `json:"slug" db:"slug"`
17-
}
18-
1912
<%
2013
// {'columns':
2114
// [
@@ -87,8 +80,8 @@ function addTypes(props) {
8780
});
8881
}
8982
90-
function query2dto(parser, query) {
91-
query = removePlaceholders(query);
83+
function query2dto(parser, method) {
84+
const query = removePlaceholders(method.query);
9285
const queryAst = parser.astify(query);
9386
const props = extractProperties(queryAst);
9487
if (props.length === 0) {
@@ -98,9 +91,10 @@ function query2dto(parser, query) {
9891
return null;
9992
}
10093
const propsWithTypes = addTypes(props);
94+
const hasName = method.dto && method.dto.name && method.dto.name.length > 0;
95+
const name = hasName ? method.dto.name : "Dto" + ++globalDtoCounter;
10196
return {
102-
// TODO: assign DTO name dynamically
103-
"name": "Dto" + ++globalDtoCounter,
97+
"name": name,
10498
"props": propsWithTypes,
10599
// max length is needed for proper formatting
106100
"maxFieldNameLength": lengthOfLongestString(props),
@@ -159,8 +153,7 @@ const verbs_with_dto = [ 'get', 'post', 'put' ]
159153
endpoints.forEach(function(endpoint) {
160154
const dtos = endpoint.methods
161155
.filter(method => verbs_with_dto.includes(method.verb))
162-
.map(method => method.query)
163-
.map(query => query2dto(sqlParser, query))
156+
.map(method => query2dto(sqlParser, method))
164157
.filter(elem => elem) // filter out nulls
165158
.filter(dto => !dtoInCache(dto))
166159
.map(dto => dto2struct(cacheDto(dto)))
@@ -172,7 +165,7 @@ endpoints.forEach(function(endpoint) {
172165
});
173166
-%>
174167
func registerRoutes(r chi.Router, db *sqlx.DB) {
175-
categories := make(map[int]Category)
168+
categories := make(map[int]CategoryDto)
176169
cnt := 0
177170
<%
178171
endpoints.forEach(function(endpoint) {
@@ -182,7 +175,7 @@ endpoints.forEach(function(endpoint) {
182175
const hasGetOne = method.name === 'get';
183176
const hasGetMany = method.name === 'get_list';
184177
if (hasGetOne || hasGetMany) {
185-
const dto = query2dto(sqlParser, method.query);
178+
const dto = query2dto(sqlParser, method);
186179
// TODO: do we really need signature and cache?
187180
const cacheKey = dto ? dto.signature : null;
188181
const dataType = hasGetMany ? '[]' + dtoCache[cacheKey] : dtoCache[cacheKey];
@@ -229,10 +222,11 @@ endpoints.forEach(function(endpoint) {
229222
if (method.name === 'post') {
230223
%>
231224
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
232-
var category Category
225+
var category CategoryDto
233226
json.NewDecoder(r.Body).Decode(&category)
234227
cnt += 1
235-
category.Id = cnt
228+
id := strconv.Itoa(cnt)
229+
category.Id = &id
236230
categories[cnt] = category
237231
w.WriteHeader(http.StatusNoContent)
238232
})
@@ -242,7 +236,7 @@ endpoints.forEach(function(endpoint) {
242236
%>
243237
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
244238
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
245-
var category Category
239+
var category CategoryDto
246240
json.NewDecoder(r.Body).Decode(&category)
247241
categories[id] = category
248242
w.WriteHeader(http.StatusNoContent)

0 commit comments

Comments
 (0)