1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 11:31:29 +00:00

swagger add embedded struct

This commit is contained in:
Sergey Lanzman 2016-08-25 01:42:23 +03:00
parent 2669a1bad6
commit 3855e6812a

View File

@ -607,6 +607,23 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if k != objectname { if k != objectname {
continue continue
} }
parseObject(d, k, &m, &realTypes, astPkgs)
}
}
}
}
if m.Title == "" {
ColorLog("can't find the object: %s", str)
os.Exit(1)
}
if len(rootapi.Definitions) == 0 {
rootapi.Definitions = make(map[string]swagger.Schema)
}
rootapi.Definitions[objectname] = m
return
}
func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package) {
ts, ok := d.Decl.(*ast.TypeSpec) ts, ok := d.Decl.(*ast.TypeSpec)
if !ok { if !ok {
ColorLog("Unknown type without TypeSec: %v", d) ColorLog("Unknown type without TypeSec: %v", d)
@ -614,16 +631,15 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
} }
st, ok := ts.Type.(*ast.StructType) st, ok := ts.Type.(*ast.StructType)
if !ok { if !ok {
continue return
} }
m.Title = k m.Title = k
if st.Fields.List != nil { if st.Fields.List != nil {
m.Properties = make(map[string]swagger.Propertie) m.Properties = make(map[string]swagger.Propertie)
for _, field := range st.Fields.List { for _, field := range st.Fields.List {
isSlice, realType, sType := typeAnalyser(field) isSlice, realType, sType := typeAnalyser(field)
realTypes = append(realTypes, realType) *realTypes = append(*realTypes, realType)
mp := swagger.Propertie{} mp := swagger.Propertie{}
// add type slice
if isSlice { if isSlice {
mp.Type = "array" mp.Type = "array"
if isBasicType(realType) { if isBasicType(realType) {
@ -647,8 +663,6 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
mp.Ref = "#/definitions/" + realType mp.Ref = "#/definitions/" + realType
} }
} }
// dont add property if anonymous field
if field.Names != nil { if field.Names != nil {
// set property name as field name // set property name as field name
@ -694,6 +708,12 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if ignore := stag.Get("ignore"); ignore != "" { if ignore := stag.Get("ignore"); ignore != "" {
continue continue
} }
} else {
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for nameOfObj, obj := range fl.Scope.Objects {
if obj.Name == fmt.Sprint(field.Type) {
parseObject(obj, nameOfObj, m, realTypes, astPkgs)
} }
} }
} }
@ -701,15 +721,6 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
} }
} }
} }
if m.Title == "" {
ColorLog("can't find the object: %s", str)
os.Exit(1)
}
if len(rootapi.Definitions) == 0 {
rootapi.Definitions = make(map[string]swagger.Schema)
}
rootapi.Definitions[objectname] = m
return
} }
func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {