Skip to content

Commit 4060f59

Browse files
authored
Merge pull request #54 from link-duan/feature/sql-null-type
feat: support for sql.NullXx type
2 parents fd58d7b + 7e26e9c commit 4060f59

File tree

11 files changed

+61
-2
lines changed

11 files changed

+61
-2
lines changed

comment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (c *Comment) ApplyToSchema(schema *spec.SchemaRef) {
7070
}
7171
if schema.Ref != "" {
7272
schema.Description = c.text
73+
schema.Summary = c.Summary()
7374
return
7475
}
7576

@@ -131,6 +132,9 @@ func (c *Comment) Ignore() bool {
131132
}
132133

133134
func (c *Comment) Summary() string {
135+
if c == nil {
136+
return ""
137+
}
134138
for _, annot := range c.Annotations {
135139
summary, ok := annot.(*annotation.SummaryAnnotation)
136140
if ok {

generators/ts/ts.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ func (p *Printer) Print() f.Doc {
6464
}
6565

6666
func (p *Printer) definition(definition *spec.SchemaRef) f.Doc {
67+
if definition.Ref != "" {
68+
// ignore
69+
return f.Group()
70+
}
71+
6772
ext := definition.Value.ExtendedTypeInfo
6873
if ext != nil && ext.Type == spec.ExtendedTypeEnum { // enum
6974
return f.Group(
7075
f.Content("export enum ", definition.Value.Title, " "),
7176
p.PrintEnumBody(ext.EnumItems),
7277
)
7378
}
79+
7480
var description string
7581
if definition.Value != nil {
7682
description = definition.Value.Description
@@ -109,12 +115,12 @@ func (p *Printer) PrintEnumBody(enum []*spec.ExtendedEnumItem) f.Doc {
109115

110116
func (p *Printer) PrintType(definition *spec.SchemaRef) f.Doc {
111117
if definition.Ref != "" {
112-
referencedType := spec.Unref(p.schema, definition)
118+
referencedType := spec.UnrefRecursively(p.schema, definition)
113119
if referencedType == nil {
114120
return f.Content("unknown")
115121
}
116122
typeName := referencedType.Value.Title
117-
p.ReferencedTypes = append(p.ReferencedTypes, typeName)
123+
p.ReferencedTypes = lo.Uniq(append(p.ReferencedTypes, typeName))
118124
return f.Content(typeName)
119125
}
120126

plugins/gin/testdata/server/docs/openapi.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"title": "GinParams",
2323
"type": "array"
2424
},
25+
"gorm.io_gorm.DeletedAt": {
26+
"format": "date-time",
27+
"title": "GormDeletedAt",
28+
"type": "string"
29+
},
2530
"server_pkg_view.ErrCode": {
2631
"description": "\u003ctable\u003e\u003ctr\u003e\u003cth\u003eValue\u003c/th\u003e\u003cth\u003eKey\u003c/th\u003e\u003cth\u003eDescription\u003c/th\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e10000\u003c/td\u003e\u003ctd\u003eCodeNotFound\u003c/td\u003e\u003ctd\u003eResource not found\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e10001\u003c/td\u003e\u003ctd\u003eCodeCancled\u003c/td\u003e\u003ctd\u003eRequest canceld\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e10002\u003c/td\u003e\u003ctd\u003eCodeUnknown\u003c/td\u003e\u003ctd\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e10003\u003c/td\u003e\u003ctd\u003eCodeInvalidArgument\u003c/td\u003e\u003ctd\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e",
2732
"enum": [
@@ -119,6 +124,9 @@
119124
"cover": {
120125
"type": "string"
121126
},
127+
"deletedAt": {
128+
"$ref": "#/components/schemas/gorm.io_gorm.DeletedAt"
129+
},
122130
"mapInt": {
123131
"additionalProperties": {
124132
"$ref": "#/components/schemas/server_pkg_view.Property"

plugins/gin/testdata/server/eapi.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ openapi:
2020
depends:
2121
- github.com/gin-gonic/gin
2222
- encoding/json
23+
- database/sql
24+
- gorm.io/gorm
2325

2426
properties:
2527
request:

plugins/gin/testdata/server/frontend/src/requests/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type GinParam = {
1313
*/
1414
export type GinParams = GinParam[]
1515

16+
export type GormDeletedAt = string
17+
1618
export enum ViewErrCode {
1719
CodeNotFound = 10000,
1820
CodeCancled = 10001,
@@ -77,6 +79,7 @@ export type ViewGoodsDownRes = {
7779

7880
export type ViewGoodsInfoRes = {
7981
cover?: string;
82+
deletedAt?: GormDeletedAt;
8083
mapInt?: Record<number, ViewProperty>;
8184
price?: number;
8285
properties?: Record<string, ViewProperty>;

plugins/gin/testdata/server/frontend/src/types/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type GinParam = {
1313
*/
1414
export type GinParams = GinParam[]
1515

16+
export type GormDeletedAt = string
17+
1618
export enum ViewErrCode {
1719
CodeNotFound = 10000,
1820
CodeCancled = 10001,
@@ -77,6 +79,7 @@ export type ViewGoodsDownRes = {
7779

7880
export type ViewGoodsInfoRes = {
7981
cover?: string;
82+
deletedAt?: GormDeletedAt;
8083
mapInt?: Record<number, ViewProperty>;
8184
price?: number;
8285
properties?: Record<string, ViewProperty>;

plugins/gin/testdata/server/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ require (
1111
github.com/go-playground/validator/v10 v10.10.0 // indirect
1212
github.com/goccy/go-json v0.9.7 // indirect
1313
github.com/google/go-cmp v0.5.7 // indirect
14+
github.com/jinzhu/inflection v1.0.0 // indirect
15+
github.com/jinzhu/now v1.1.4 // indirect
1416
github.com/json-iterator/go v1.1.12 // indirect
1517
github.com/leodido/go-urn v1.2.1 // indirect
1618
github.com/mattn/go-isatty v0.0.14 // indirect
@@ -25,4 +27,5 @@ require (
2527
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2628
google.golang.org/protobuf v1.28.0 // indirect
2729
gopkg.in/yaml.v2 v2.4.0 // indirect
30+
gorm.io/gorm v1.24.5 // indirect
2831
)

plugins/gin/testdata/server/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
2121
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
2222
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
2323
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
24+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
25+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
26+
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
27+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
2428
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
2529
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
2630
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -91,3 +95,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
9195
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9296
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
9397
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
98+
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
99+
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=

plugins/gin/testdata/server/pkg/view/shop.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55

66
"github.com/gin-gonic/gin"
7+
"gorm.io/gorm"
78
)
89

910
// Image 商品图片
@@ -62,6 +63,7 @@ type GoodsInfoRes struct {
6263
Price int64 `json:"price"`
6364
Properties map[string]*Property `json:"properties"`
6465
MapInt map[int]*Property `json:"mapInt"`
66+
DeletedAt gorm.DeletedAt `json:"deletedAt"`
6567
}
6668

6769
type Property struct {

schema.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func (s *SchemaBuilder) FromTypeSpec(t *ast.TypeSpec) *spec.SchemaRef {
5858
return nil
5959
}
6060
comment := s.ctx.ParseComment(s.ctx.GetHeadingCommentOf(t.Pos()))
61+
if schema.Ref != "" {
62+
comment.ApplyToSchema(schema)
63+
schema.Description = strings.TrimSpace(comment.TrimPrefix(t.Name.Name))
64+
return schema
65+
}
6166
schema.Value.Title = strcase.ToCamel(s.ctx.Package().Name + t.Name.Name)
6267
schema.Value.Description = strings.TrimSpace(comment.TrimPrefix(t.Name.Name))
6368
schema.Value.Deprecated = comment.Deprecated()
@@ -199,6 +204,15 @@ var commonTypes = map[string]*spec.Schema{
199204
WithType("object").
200205
WithDescription("Any Json Type").
201206
WithExtendedType(spec.NewAnyExtendedType()),
207+
"database/sql.NullTime": spec.NewDateTimeSchema(),
208+
"database/sql.NullString": spec.NewStringSchema(),
209+
"database/sql.NullInt64": spec.NewInt64Schema(),
210+
"database/sql.NullInt32": spec.NewInt32Schema(),
211+
"database/sql.NullInt": spec.NewIntegerSchema(),
212+
"database/sql.NullInt16": spec.NewIntegerSchema(),
213+
"database/sql.NullFloat64": spec.NewFloat64Schema(),
214+
"database/sql.NullBool": spec.NewBoolSchema(),
215+
"database/sql.NullByte": spec.NewStringSchema(),
202216
}
203217

204218
func (s *SchemaBuilder) commonUsedType(t types.Type) *spec.SchemaRef {

0 commit comments

Comments
 (0)