Skip to content

Commit 7f28351

Browse files
committed
chore(golang): user can provide a type of DTO member
By default, string type is used. Also, the value is optional (nullable). Example of specifying an integer type: dto: fields: counter: type: integer Part of #9
1 parent b13625b commit 7f28351

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

examples/go/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "github.com/go-chi/chi"
1010
import "github.com/jmoiron/sqlx"
1111

1212
type CounterDto struct {
13-
Counter *string `json:"counter,omitempty" db:"counter"`
13+
Counter *integer `json:"counter,omitempty" db:"counter"`
1414
}
1515

1616
type CategoryDto struct {

examples/js/endpoints.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
query: SELECT COUNT(*) AS counter FROM categories
44
dto:
55
name: CounterDto
6+
fields:
7+
counter:
8+
type: integer
69

710
- path: /v1/collections/:collectionId/categories/count
811
get:
@@ -12,6 +15,10 @@
1215
JOIN series s
1316
ON s.id = cs.series_id
1417
WHERE cs.collection_id = :p.collectionId
18+
dto:
19+
fields:
20+
counter:
21+
type: integer
1522

1623
- path: /v1/categories
1724
get_list:

src/templates/routes.go.ejs

+5-4
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ function extractProperties(queryAst) {
7070
return [];
7171
}
7272
73-
function addTypes(props) {
73+
function addTypes(props, fieldsInfo) {
7474
return props.map(prop => {
75+
const hasTypeInfo = fieldsInfo.hasOwnProperty(prop) && fieldsInfo[prop].hasOwnProperty('type');
7576
return {
7677
"name": prop,
77-
// TODO: resolve/autoguess types
78-
"type": "*string"
78+
"type": hasTypeInfo ? '*' + fieldsInfo[prop].type : '*string'
7979
}
8080
});
8181
}
@@ -90,7 +90,8 @@ function query2dto(parser, method) {
9090
console.debug(queryAst);
9191
return null;
9292
}
93-
const propsWithTypes = addTypes(props);
93+
const fieldsInfo = method.dto && method.dto.fields ? method.dto.fields : {};
94+
const propsWithTypes = addTypes(props, fieldsInfo);
9495
const hasName = method.dto && method.dto.name && method.dto.name.length > 0;
9596
const name = hasName ? method.dto.name : "Dto" + ++globalDtoCounter;
9697
return {

0 commit comments

Comments
 (0)