mirror of
https://github.com/beego/bee.git
synced 2024-11-22 10:10:53 +00:00
Merge pull request #512 from WUMUXIAN/features/swagger-example-enum-support
Swagger: Enum improvement, example value improvement.
This commit is contained in:
commit
3469f0ae4c
@ -925,15 +925,24 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string
|
|||||||
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
|
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
|
||||||
switch t := ts.Type.(type) {
|
switch t := ts.Type.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
parseIdent(k, m, astPkgs)
|
parseIdent(t, k, m, astPkgs)
|
||||||
case *ast.StructType:
|
case *ast.StructType:
|
||||||
parseStruct(t, k, m, realTypes, astPkgs, packageName)
|
parseStruct(t, k, m, realTypes, astPkgs, packageName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse as enum
|
// parse as enum
|
||||||
func parseIdent(k string, m *swagger.Schema, astPkgs []*ast.Package) {
|
func parseIdent(st *ast.Ident, k string, m *swagger.Schema, astPkgs []*ast.Package) {
|
||||||
m.Title = k
|
m.Title = k
|
||||||
|
basicType := fmt.Sprint(st)
|
||||||
|
if object, isStdLibObject := stdlibObject[basicType]; isStdLibObject {
|
||||||
|
basicType = object
|
||||||
|
}
|
||||||
|
if k, ok := basicTypes[basicType]; ok {
|
||||||
|
typeFormat := strings.Split(k, ":")
|
||||||
|
m.Type = typeFormat[0]
|
||||||
|
m.Format = typeFormat[1]
|
||||||
|
}
|
||||||
for _, pkg := range astPkgs {
|
for _, pkg := range astPkgs {
|
||||||
for _, fl := range pkg.Files {
|
for _, fl := range pkg.Files {
|
||||||
for _, obj := range fl.Scope.Objects {
|
for _, obj := range fl.Scope.Objects {
|
||||||
@ -942,44 +951,11 @@ func parseIdent(k string, m *swagger.Schema, astPkgs []*ast.Package) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
beeLogger.Log.Fatalf("Unknown type without ValueSpec: %v\n", vs)
|
beeLogger.Log.Fatalf("Unknown type without ValueSpec: %v\n", vs)
|
||||||
}
|
}
|
||||||
ti, ok := vs.Type.(*ast.Ident)
|
enum := fmt.Sprintf("%s = %v", vs.Names[0].Name, vs.Names[0].Obj.Data)
|
||||||
if !ok {
|
m.Enum = append(m.Enum, enum)
|
||||||
// TODO type inference, iota not support yet
|
// Automatically give an example.
|
||||||
continue
|
if m.Example == nil {
|
||||||
}
|
m.Example = vs.Names[0].Obj.Data
|
||||||
if ti.Name != k {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, val := range vs.Values {
|
|
||||||
v, ok := val.(*ast.BasicLit)
|
|
||||||
if !ok {
|
|
||||||
beeLogger.Log.Warnf("Unknown type without BasicLit: %v\n", v)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
rawV := strings.Trim(v.Value, `"`)
|
|
||||||
switch v.Kind {
|
|
||||||
case token.INT:
|
|
||||||
vv, err := strconv.Atoi(rawV)
|
|
||||||
if err != nil {
|
|
||||||
beeLogger.Log.Warnf("Unknown type with BasicLit to int: %v\n", rawV)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m.Enum = append(m.Enum, vv)
|
|
||||||
case token.FLOAT:
|
|
||||||
vv, err := strconv.ParseFloat(rawV, 64)
|
|
||||||
if err != nil {
|
|
||||||
beeLogger.Log.Warnf("Unknown type with BasicLit to int: %v\n", rawV)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m.Enum = append(m.Enum, vv)
|
|
||||||
//case token.IMAG:
|
|
||||||
// rawV := strings.Trim(v.Value, `i`)
|
|
||||||
// m.Enum = append(m.Enum, strconv.ParseFloat(rawV, 64))
|
|
||||||
case token.CHAR:
|
|
||||||
m.Enum = append(m.Enum, rawV)
|
|
||||||
case token.STRING:
|
|
||||||
m.Enum = append(m.Enum, rawV)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1005,6 +981,7 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
|
|||||||
}
|
}
|
||||||
*realTypes = append(*realTypes, realType)
|
*realTypes = append(*realTypes, realType)
|
||||||
mp := swagger.Propertie{}
|
mp := swagger.Propertie{}
|
||||||
|
isObject := false
|
||||||
if isSlice {
|
if isSlice {
|
||||||
mp.Type = "array"
|
mp.Type = "array"
|
||||||
if isBasicType(strings.Replace(realType, "[]", "", -1)) {
|
if isBasicType(strings.Replace(realType, "[]", "", -1)) {
|
||||||
@ -1020,6 +997,7 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if sType == "object" {
|
if sType == "object" {
|
||||||
|
isObject = true
|
||||||
mp.Ref = "#/definitions/" + realType
|
mp.Ref = "#/definitions/" + realType
|
||||||
} else if isBasicType(realType) {
|
} else if isBasicType(realType) {
|
||||||
typeFormat := strings.Split(sType, ":")
|
typeFormat := strings.Split(sType, ":")
|
||||||
@ -1087,6 +1065,10 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
|
|||||||
mp.Description = desc
|
mp.Description = desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if example := stag.Get("example"); example != "" && !isObject && !isSlice {
|
||||||
|
mp.Example = str2RealType(example, realType)
|
||||||
|
}
|
||||||
|
|
||||||
m.Properties[name] = mp
|
m.Properties[name] = mp
|
||||||
}
|
}
|
||||||
if ignore := stag.Get("ignore"); ignore != "" {
|
if ignore := stag.Get("ignore"); ignore != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user