1 Star 0 Fork 0

KathentGitee/go-swagger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
discriminators.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
package generator
import (
"github.com/go-swagger/go-swagger/spec"
"github.com/go-swagger/go-swagger/swag"
)
type discInfo struct {
Discriminators map[string]discor
Discriminated map[string]discee
}
type discor struct {
FieldName string `json:"fieldName"`
GoType string `json:"goType"`
JSONName string `json:"jsonName"`
Children []discee `json:"children"`
}
type discee struct {
FieldName string `json:"fieldName"`
FieldValue string `json:"fieldValue"`
GoType string `json:"goType"`
JSONName string `json:"jsonName"`
Ref spec.Ref `json:"ref"`
ParentRef spec.Ref `json:"parentRef"`
}
func discriminatorInfo(doc *spec.Document) *discInfo {
baseTypes := make(map[string]discor)
for _, sch := range doc.AllDefinitions() {
if sch.Schema.Discriminator != "" {
tpe, _ := sch.Schema.Extensions.GetString("x-go-name")
if tpe == "" {
tpe = swag.ToGoName(sch.Name)
}
baseTypes[sch.Ref.String()] = discor{
FieldName: sch.Schema.Discriminator,
GoType: tpe,
JSONName: sch.Name,
}
}
}
subTypes := make(map[string]discee)
for _, sch := range doc.SchemasWithAllOf() {
for _, ao := range sch.Schema.AllOf {
if ao.Ref.String() != "" {
if bt, ok := baseTypes[ao.Ref.String()]; ok {
name, _ := sch.Schema.Extensions.GetString("x-class")
if name == "" {
name, _ = sch.Schema.Extensions.GetString("x-go-name")
}
if name == "" {
name = swag.ToGoName(sch.Name)
}
tpe, _ := sch.Schema.Extensions.GetString("x-go-name")
if tpe == "" {
tpe = swag.ToGoName(sch.Name)
}
dce := discee{
FieldName: bt.FieldName,
FieldValue: name,
Ref: sch.Ref,
ParentRef: ao.Ref,
JSONName: sch.Name,
GoType: tpe,
}
subTypes[sch.Ref.String()] = dce
bt.Children = append(bt.Children, dce)
baseTypes[ao.Ref.String()] = bt
}
}
}
}
return &discInfo{Discriminators: baseTypes, Discriminated: subTypes}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kathent/go-swagger.git
git@gitee.com:kathent/go-swagger.git
kathent
go-swagger
go-swagger
v0.1.0

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385