Skip to content

Commit

Permalink
Merge pull request #54 from link-duan/feature/sql-null-type
Browse files Browse the repository at this point in the history
feat: support for sql.NullXx type
  • Loading branch information
link-duan authored Feb 17, 2023
2 parents fd58d7b + 7e26e9c commit 4060f59
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (c *Comment) ApplyToSchema(schema *spec.SchemaRef) {
}
if schema.Ref != "" {
schema.Description = c.text
schema.Summary = c.Summary()
return
}

Expand Down Expand Up @@ -131,6 +132,9 @@ func (c *Comment) Ignore() bool {
}

func (c *Comment) Summary() string {
if c == nil {
return ""
}
for _, annot := range c.Annotations {
summary, ok := annot.(*annotation.SummaryAnnotation)
if ok {
Expand Down
10 changes: 8 additions & 2 deletions generators/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ func (p *Printer) Print() f.Doc {
}

func (p *Printer) definition(definition *spec.SchemaRef) f.Doc {
if definition.Ref != "" {
// ignore
return f.Group()
}

ext := definition.Value.ExtendedTypeInfo
if ext != nil && ext.Type == spec.ExtendedTypeEnum { // enum
return f.Group(
f.Content("export enum ", definition.Value.Title, " "),
p.PrintEnumBody(ext.EnumItems),
)
}

var description string
if definition.Value != nil {
description = definition.Value.Description
Expand Down Expand Up @@ -109,12 +115,12 @@ func (p *Printer) PrintEnumBody(enum []*spec.ExtendedEnumItem) f.Doc {

func (p *Printer) PrintType(definition *spec.SchemaRef) f.Doc {
if definition.Ref != "" {
referencedType := spec.Unref(p.schema, definition)
referencedType := spec.UnrefRecursively(p.schema, definition)
if referencedType == nil {
return f.Content("unknown")
}
typeName := referencedType.Value.Title
p.ReferencedTypes = append(p.ReferencedTypes, typeName)
p.ReferencedTypes = lo.Uniq(append(p.ReferencedTypes, typeName))
return f.Content(typeName)
}

Expand Down
8 changes: 8 additions & 0 deletions plugins/gin/testdata/server/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"title": "GinParams",
"type": "array"
},
"gorm.io_gorm.DeletedAt": {
"format": "date-time",
"title": "GormDeletedAt",
"type": "string"
},
"server_pkg_view.ErrCode": {
"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",
"enum": [
Expand Down Expand Up @@ -119,6 +124,9 @@
"cover": {
"type": "string"
},
"deletedAt": {
"$ref": "#/components/schemas/gorm.io_gorm.DeletedAt"
},
"mapInt": {
"additionalProperties": {
"$ref": "#/components/schemas/server_pkg_view.Property"
Expand Down
2 changes: 2 additions & 0 deletions plugins/gin/testdata/server/eapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ openapi:
depends:
- github.com/gin-gonic/gin
- encoding/json
- database/sql
- gorm.io/gorm

properties:
request:
Expand Down
3 changes: 3 additions & 0 deletions plugins/gin/testdata/server/frontend/src/requests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type GinParam = {
*/
export type GinParams = GinParam[]

export type GormDeletedAt = string

export enum ViewErrCode {
CodeNotFound = 10000,
CodeCancled = 10001,
Expand Down Expand Up @@ -77,6 +79,7 @@ export type ViewGoodsDownRes = {

export type ViewGoodsInfoRes = {
cover?: string;
deletedAt?: GormDeletedAt;
mapInt?: Record<number, ViewProperty>;
price?: number;
properties?: Record<string, ViewProperty>;
Expand Down
3 changes: 3 additions & 0 deletions plugins/gin/testdata/server/frontend/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type GinParam = {
*/
export type GinParams = GinParam[]

export type GormDeletedAt = string

export enum ViewErrCode {
CodeNotFound = 10000,
CodeCancled = 10001,
Expand Down Expand Up @@ -77,6 +79,7 @@ export type ViewGoodsDownRes = {

export type ViewGoodsInfoRes = {
cover?: string;
deletedAt?: GormDeletedAt;
mapInt?: Record<number, ViewProperty>;
price?: number;
properties?: Record<string, ViewProperty>;
Expand Down
3 changes: 3 additions & 0 deletions plugins/gin/testdata/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -25,4 +27,5 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/gorm v1.24.5 // indirect
)
6 changes: 6 additions & 0 deletions plugins/gin/testdata/server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -91,3 +95,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
2 changes: 2 additions & 0 deletions plugins/gin/testdata/server/pkg/view/shop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

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

type Property struct {
Expand Down
14 changes: 14 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (s *SchemaBuilder) FromTypeSpec(t *ast.TypeSpec) *spec.SchemaRef {
return nil
}
comment := s.ctx.ParseComment(s.ctx.GetHeadingCommentOf(t.Pos()))
if schema.Ref != "" {
comment.ApplyToSchema(schema)
schema.Description = strings.TrimSpace(comment.TrimPrefix(t.Name.Name))
return schema
}
schema.Value.Title = strcase.ToCamel(s.ctx.Package().Name + t.Name.Name)
schema.Value.Description = strings.TrimSpace(comment.TrimPrefix(t.Name.Name))
schema.Value.Deprecated = comment.Deprecated()
Expand Down Expand Up @@ -199,6 +204,15 @@ var commonTypes = map[string]*spec.Schema{
WithType("object").
WithDescription("Any Json Type").
WithExtendedType(spec.NewAnyExtendedType()),
"database/sql.NullTime": spec.NewDateTimeSchema(),
"database/sql.NullString": spec.NewStringSchema(),
"database/sql.NullInt64": spec.NewInt64Schema(),
"database/sql.NullInt32": spec.NewInt32Schema(),
"database/sql.NullInt": spec.NewIntegerSchema(),
"database/sql.NullInt16": spec.NewIntegerSchema(),
"database/sql.NullFloat64": spec.NewFloat64Schema(),
"database/sql.NullBool": spec.NewBoolSchema(),
"database/sql.NullByte": spec.NewStringSchema(),
}

func (s *SchemaBuilder) commonUsedType(t types.Type) *spec.SchemaRef {
Expand Down
8 changes: 8 additions & 0 deletions spec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ func Unref(t *T, schema *SchemaRef) *SchemaRef {

return nil
}

// UnrefRecursively 递归 unref
func UnrefRecursively(t *T, schema *SchemaRef) *SchemaRef {
for schema != nil && schema.Ref != "" {
schema = Unref(t, schema)
}
return schema
}

0 comments on commit 4060f59

Please sign in to comment.