Skip to content

Commit 6f9b918

Browse files
authored
Merge pull request #44 from link-duan/main
feat(generators/ts): 类型名使用包名作为前缀
2 parents 0984ed8 + 4c11a5b commit 6f9b918

File tree

6 files changed

+64
-57
lines changed

6 files changed

+64
-57
lines changed

generators/umi/umi.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ func (p *Printer) jsDoc(item *spec.Operation) f.Doc {
318318
func (p *Printer) responseType(res *spec.Response) f.Doc {
319319
for _, mediaType := range res.Content {
320320
schema := mediaType.Schema
321+
if schema == nil {
322+
continue
323+
}
321324
schema = spec.Unref(p.schema, schema)
322325
tsPrinter := ts.NewPrinter(p.schema).SetTypeFieldsInline(true)
323326
ret := tsPrinter.PrintType(schema)

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"type": "string"
1212
}
1313
},
14-
"title": "Param",
14+
"title": "GinParam",
1515
"type": "object"
1616
},
1717
"github.com_gin-gonic_gin.Params": {
1818
"description": "is a Param-slice, as returned by the router.\n\nThe slice is ordered, the first URL parameter is also the first slice value.\n\nIt is therefore safe to read values by the index.",
1919
"items": {
2020
"$ref": "#/components/schemas/github.com_gin-gonic_gin.Param"
2121
},
22-
"title": "Params",
22+
"title": "GinParams",
2323
"type": "array"
2424
},
2525
"server_pkg_view.ErrCode": {
@@ -30,7 +30,7 @@
3030
10002,
3131
10003
3232
],
33-
"title": "ErrCode",
33+
"title": "ViewErrCode",
3434
"type": "integer"
3535
},
3636
"server_pkg_view.Error": {
@@ -42,7 +42,7 @@
4242
"type": "string"
4343
}
4444
},
45-
"title": "Error",
45+
"title": "ViewError",
4646
"type": "object"
4747
},
4848
"server_pkg_view.GoodsCreateReq": {
@@ -75,7 +75,7 @@
7575
"title",
7676
"price"
7777
],
78-
"title": "GoodsCreateReq",
78+
"title": "ViewGoodsCreateReq",
7979
"type": "object"
8080
},
8181
"server_pkg_view.GoodsCreateRes": {
@@ -102,7 +102,7 @@
102102
"type": "string"
103103
}
104104
},
105-
"title": "GoodsCreateRes",
105+
"title": "ViewGoodsCreateRes",
106106
"type": "object"
107107
},
108108
"server_pkg_view.GoodsDownRes": {
@@ -111,7 +111,7 @@
111111
"type": "string"
112112
}
113113
},
114-
"title": "GoodsDownRes",
114+
"title": "ViewGoodsDownRes",
115115
"type": "object"
116116
},
117117
"server_pkg_view.GoodsInfoRes": {
@@ -131,7 +131,7 @@
131131
"type": "string"
132132
}
133133
},
134-
"title": "GoodsInfoRes",
134+
"title": "ViewGoodsInfoRes",
135135
"type": "object"
136136
},
137137
"server_pkg_view.Image": {
@@ -145,7 +145,7 @@
145145
"required": [
146146
"url"
147147
],
148-
"title": "Image",
148+
"title": "ViewImage",
149149
"type": "object"
150150
},
151151
"server_pkg_view.Property": {
@@ -154,7 +154,7 @@
154154
"type": "string"
155155
}
156156
},
157-
"title": "Property",
157+
"title": "ViewProperty",
158158
"type": "object"
159159
},
160160
"server_pkg_view.SelfRefType": {
@@ -166,7 +166,7 @@
166166
"$ref": "#/components/schemas/server_pkg_view.SelfRefType"
167167
}
168168
},
169-
"title": "SelfRefType",
169+
"title": "ViewSelfRefType",
170170
"type": "object"
171171
}
172172
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { request } from "umi";
22
import {
3-
GoodsCreateReq,
4-
Params,
5-
SelfRefType,
6-
Property,
7-
GoodsInfoRes
3+
ViewGoodsCreateReq,
4+
GinParams,
5+
ViewSelfRefType,
6+
ViewProperty,
7+
ViewGoodsInfoRes
88
} from "./types";
99

1010
/*
1111
* @description 创建商品接口
1212
*/
13-
export function shopGoodsCreate(data: GoodsCreateReq) {
14-
return request<{ Status?: Params; guid?: string; raw?: any; selfRef?: SelfRefType; stringAlias?: string; }>(`/api/goods`, {
13+
export function shopGoodsCreate(data: ViewGoodsCreateReq) {
14+
return request<{ Status?: GinParams; guid?: string; raw?: any; selfRef?: ViewSelfRefType; stringAlias?: string; }>(`/api/goods`, {
1515
method: "post",
1616
data,
1717
});
@@ -43,13 +43,13 @@ export function shopGoodsDown(guid: string, data: { dateRange?: string[]; operat
4343
* @description 商品详情
4444
*/
4545
export function shopGoodsInfo(guid: string) {
46-
return request<{ cover?: string; mapInt?: Record<number, Property>; price?: number; properties?: Record<string, Property>; subTitle?: string; title?: string; }>(`/api/v2/goods/${guid}`, {
46+
return request<{ cover?: string; mapInt?: Record<number, ViewProperty>; price?: number; properties?: Record<string, ViewProperty>; subTitle?: string; title?: string; }>(`/api/v2/goods/${guid}`, {
4747
method: "get",
4848
});
4949
}
5050

5151
export function shopWrappedHandler(query: { hello?: string; world?: string }) {
52-
return request<{ code: number; data: GoodsInfoRes; msg: string; }>(`/wrapped-handler`, {
52+
return request<{ code: number; data: ViewGoodsInfoRes; msg: string; }>(`/wrapped-handler`, {
5353
method: "get",
5454
params: query,
5555
});
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @description is a single URL parameter, consisting of a key and a value.
33
*/
4-
export type Param = {
4+
export type GinParam = {
55
Key?: string;
66
Value?: string;
77
}
@@ -11,29 +11,29 @@ export type Param = {
1111
* The slice is ordered, the first URL parameter is also the first slice value.
1212
* It is therefore safe to read values by the index.
1313
*/
14-
export type Params = Param[]
14+
export type GinParams = GinParam[]
1515

16-
export enum ErrCode {
16+
export enum ViewErrCode {
1717
CodeNotFound = 10000,
1818
CodeCancled = 10001,
1919
CodeUnknown = 10002,
2020
CodeInvalidArgument = 10003,
2121
}
2222

23-
export type Error = {
24-
code?: ErrCode;
23+
export type ViewError = {
24+
code?: ViewErrCode;
2525
msg?: string;
2626
}
2727

28-
export type GoodsCreateReq = {
28+
export type ViewGoodsCreateReq = {
2929
/*
3030
* @description 封面图
3131
*/
3232
cover?: string;
3333
/*
3434
* @description 详情图
3535
*/
36-
images?: Image[];
36+
images?: ViewImage[];
3737
/*
3838
* @description 价格(分)
3939
*/
@@ -48,11 +48,11 @@ export type GoodsCreateReq = {
4848
title: string;
4949
}
5050

51-
export type GoodsCreateRes = {
51+
export type ViewGoodsCreateRes = {
5252
/*
5353
* @description 测试引用第三方包
5454
*/
55-
Status?: Params;
55+
Status?: GinParams;
5656
/*
5757
* @description 商品 GUID
5858
*/
@@ -64,41 +64,41 @@ export type GoodsCreateRes = {
6464
/*
6565
* @description 测试循环引用
6666
*/
67-
selfRef?: SelfRefType;
67+
selfRef?: ViewSelfRefType;
6868
/*
6969
* @description 测试类型别名
7070
*/
7171
stringAlias?: string;
7272
}
7373

74-
export type GoodsDownRes = {
74+
export type ViewGoodsDownRes = {
7575
Status?: string;
7676
}
7777

78-
export type GoodsInfoRes = {
78+
export type ViewGoodsInfoRes = {
7979
cover?: string;
80-
mapInt?: Record<number, Property>;
80+
mapInt?: Record<number, ViewProperty>;
8181
price?: number;
82-
properties?: Record<string, Property>;
82+
properties?: Record<string, ViewProperty>;
8383
subTitle?: string;
8484
title?: string;
8585
}
8686

8787
/*
8888
* @description 商品图片
8989
*/
90-
export type Image = {
90+
export type ViewImage = {
9191
/*
9292
* @description 图片链接
9393
*/
9494
url: string;
9595
}
9696

97-
export type Property = {
97+
export type ViewProperty = {
9898
title?: string;
9999
}
100100

101-
export type SelfRefType = {
101+
export type ViewSelfRefType = {
102102
data?: string;
103-
parent?: SelfRefType;
103+
parent?: ViewSelfRefType;
104104
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @description is a single URL parameter, consisting of a key and a value.
33
*/
4-
export type Param = {
4+
export type GinParam = {
55
Key?: string;
66
Value?: string;
77
}
@@ -11,29 +11,29 @@ export type Param = {
1111
* The slice is ordered, the first URL parameter is also the first slice value.
1212
* It is therefore safe to read values by the index.
1313
*/
14-
export type Params = Param[]
14+
export type GinParams = GinParam[]
1515

16-
export enum ErrCode {
16+
export enum ViewErrCode {
1717
CodeNotFound = 10000,
1818
CodeCancled = 10001,
1919
CodeUnknown = 10002,
2020
CodeInvalidArgument = 10003,
2121
}
2222

23-
export type Error = {
24-
code?: ErrCode;
23+
export type ViewError = {
24+
code?: ViewErrCode;
2525
msg?: string;
2626
}
2727

28-
export type GoodsCreateReq = {
28+
export type ViewGoodsCreateReq = {
2929
/*
3030
* @description 封面图
3131
*/
3232
cover?: string;
3333
/*
3434
* @description 详情图
3535
*/
36-
images?: Image[];
36+
images?: ViewImage[];
3737
/*
3838
* @description 价格(分)
3939
*/
@@ -48,11 +48,11 @@ export type GoodsCreateReq = {
4848
title: string;
4949
}
5050

51-
export type GoodsCreateRes = {
51+
export type ViewGoodsCreateRes = {
5252
/*
5353
* @description 测试引用第三方包
5454
*/
55-
Status?: Params;
55+
Status?: GinParams;
5656
/*
5757
* @description 商品 GUID
5858
*/
@@ -64,41 +64,41 @@ export type GoodsCreateRes = {
6464
/*
6565
* @description 测试循环引用
6666
*/
67-
selfRef?: SelfRefType;
67+
selfRef?: ViewSelfRefType;
6868
/*
6969
* @description 测试类型别名
7070
*/
7171
stringAlias?: string;
7272
}
7373

74-
export type GoodsDownRes = {
74+
export type ViewGoodsDownRes = {
7575
Status?: string;
7676
}
7777

78-
export type GoodsInfoRes = {
78+
export type ViewGoodsInfoRes = {
7979
cover?: string;
80-
mapInt?: Record<number, Property>;
80+
mapInt?: Record<number, ViewProperty>;
8181
price?: number;
82-
properties?: Record<string, Property>;
82+
properties?: Record<string, ViewProperty>;
8383
subTitle?: string;
8484
title?: string;
8585
}
8686

8787
/*
8888
* @description 商品图片
8989
*/
90-
export type Image = {
90+
export type ViewImage = {
9191
/*
9292
* @description 图片链接
9393
*/
9494
url: string;
9595
}
9696

97-
export type Property = {
97+
export type ViewProperty = {
9898
title?: string;
9999
}
100100

101-
export type SelfRefType = {
101+
export type ViewSelfRefType = {
102102
data?: string;
103-
parent?: SelfRefType;
103+
parent?: ViewSelfRefType;
104104
}

schema.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gotomicro/eapi/spec"
1010
"github.com/gotomicro/eapi/tag"
11+
"github.com/iancoleman/strcase"
1112
"github.com/samber/lo"
1213
)
1314

@@ -57,7 +58,7 @@ func (s *SchemaBuilder) FromTypeSpec(t *ast.TypeSpec) *spec.SchemaRef {
5758
return nil
5859
}
5960
comment := ParseComment(s.ctx.GetHeadingCommentOf(t.Pos()))
60-
schema.Value.Title = t.Name.Name
61+
schema.Value.Title = strcase.ToCamel(s.ctx.Package().Name + t.Name.Name)
6162
schema.Value.Description = strings.TrimSpace(comment.TrimPrefix(t.Name.Name))
6263
schema.Value.Deprecated = comment.Deprecated()
6364
return schema
@@ -139,6 +140,9 @@ func (s *SchemaBuilder) parseStruct(expr *ast.StructType) *spec.SchemaRef {
139140
}
140141

141142
for _, name := range field.Names {
143+
if !name.IsExported() {
144+
continue
145+
}
142146
fieldSchema := s.ParseExpr(field.Type)
143147
if fieldSchema == nil {
144148
fmt.Printf("unknown field type %s at %s\n", name.Name, s.ctx.LineColumn(field.Type.Pos()))

0 commit comments

Comments
 (0)