本文整理汇总了Golang中github.com/astaxie/beego/swagger.ModelProperty类的典型用法代码示例。如果您正苦于以下问题:Golang ModelProperty类的具体用法?Golang ModelProperty怎么用?Golang ModelProperty使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelProperty类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: getModel
func getModel(str string) (pkgpath, objectname string, m swagger.Model, realTypes []string) {
strs := strings.Split(str, ".")
objectname = strs[len(strs)-1]
pkgpath = strings.Join(strs[:len(strs)-1], "/")
curpath, _ := os.Getwd()
pkgRealpath := path.Join(curpath, pkgpath)
fileSet := token.NewFileSet()
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments)
if err != nil {
ColorLog("[ERRO] the model %s parser.ParseDir error\n", str)
os.Exit(1)
}
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for k, d := range fl.Scope.Objects {
if d.Kind == ast.Typ {
if k != objectname {
continue
}
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
ColorLog("Unknown type without TypeSec: %v", d)
os.Exit(1)
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
m.Id = k
if st.Fields.List != nil {
m.Properties = make(map[string]swagger.ModelProperty)
for _, field := range st.Fields.List {
isSlice, realType := typeAnalyser(field)
realTypes = append(realTypes, realType)
mp := swagger.ModelProperty{}
// add type slice
if isSlice {
if isBasicType(realType) {
mp.Type = "[]" + realType
} else {
mp.Type = "array"
mp.Items = make(map[string]string)
mp.Items["$ref"] = realType
}
} else {
mp.Type = realType
}
// dont add property if anonymous field
if field.Names != nil {
// set property name as field name
var name = field.Names[0].Name
// if no tag skip tag processing
if field.Tag == nil {
m.Properties[name] = mp
continue
}
var tagValues []string
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
tag := stag.Get("json")
if tag != "" {
tagValues = strings.Split(tag, ",")
}
// dont add property if json tag first value is "-"
if len(tagValues) == 0 || tagValues[0] != "-" {
// set property name to the left most json tag value only if is not omitempty
if len(tagValues) > 0 && tagValues[0] != "omitempty" {
name = tagValues[0]
}
if thrifttag := stag.Get("thrift"); thrifttag != "" {
ts := strings.Split(thrifttag, ",")
if ts[0] != "" {
name = ts[0]
}
}
if required := stag.Get("required"); required != "" {
m.Required = append(m.Required, name)
}
if desc := stag.Get("description"); desc != "" {
mp.Description = desc
}
m.Properties[name] = mp
}
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
}
//.........这里部分代码省略.........
开发者ID:jojo13572001,项目名称:bee,代码行数:101,代码来源:g_docs.go
示例2: getModel
func getModel(str string) (pkgpath, objectname string, m swagger.Model, realTypes []string) {
strs := strings.Split(str, ".")
objectname = strs[len(strs)-1]
pkgpath = strings.Join(strs[:len(strs)-1], "/")
curpath, _ := os.Getwd()
pkgRealpath := path.Join(curpath, pkgpath)
fileSet := token.NewFileSet()
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments)
if err != nil {
ColorLog("[ERRO] the model %s parser.ParseDir error\n", str)
os.Exit(1)
}
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for k, d := range fl.Scope.Objects {
if d.Kind == ast.Typ {
if k != objectname {
continue
}
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
ColorLog("Unknown type without TypeSec: %v", d)
os.Exit(1)
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
m.Id = k
if st.Fields.List != nil {
m.Properties = make(map[string]swagger.ModelProperty)
for _, field := range st.Fields.List {
isSlice, realType := typeAnalyser(field)
realTypes = append(realTypes, realType)
mp := swagger.ModelProperty{}
// add type slice
if isSlice {
mp.Type = "[]" + realType
} else {
mp.Type = realType
}
// if the tag contains json tag, set the name to the left most json tag
var name = field.Names[0].Name
if field.Tag != nil {
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
if tag := stag.Get("json"); tag != "" {
name = tag
}
if thrifttag := stag.Get("thrift"); thrifttag != "" {
ts := strings.Split(thrifttag, ",")
if ts[0] != "" {
name = ts[0]
}
}
if required := stag.Get("required"); required != "" {
m.Required = append(m.Required, name)
}
if desc := stag.Get("description"); desc != "" {
mp.Description = desc
}
}
m.Properties[name] = mp
}
}
return
}
}
}
}
if m.Id == "" {
ColorLog("can't find the object: %v", str)
os.Exit(1)
}
return
}
开发者ID:NicholeGit,项目名称:go,代码行数:80,代码来源:g_docs.go
示例3: getInternalModel
func getInternalModel(str string, sourceFile *ast.File, sourceFilePkg string, m *swagger.Model, realTypes *[]*_RealType, isRoot bool, pkgRealpath string, level int) {
if level > _MAX_ANONYMOUS_LEVEL {
panic("exceed anonymous level, there may be some cycle reference")
}
strs := strings.Split(str, ".")
objectname := strs[len(strs)-1]
pkgpath := strings.Join(strs[:len(strs)-1], "/")
if pkgRealpath == "" {
pkgRealpath = path.Join(topPath, getModelPath(str, sourceFile, sourceFilePkg))
}
fileSet := token.NewFileSet()
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments)
Debugf(fmt.Sprintf("isRoot: %s, objectname: %s, pkgpath: %s, sourceFilePkg: %s, pkgRealpath: %s, topPath: %s",
strconv.FormatBool(isRoot), objectname, pkgpath, sourceFilePkg, pkgRealpath, topPath))
if err != nil {
println("----------", pkgRealpath)
ColorLog("[ERRO] the model %s parser.ParseDir error %s\n", str, err.Error())
os.Exit(1)
}
foundTypeButNotStruct := false
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for k, d := range fl.Scope.Objects {
// println(fmt.Sprintf("Scan %s, %s, %s", pkg.Name, fl.Name, k))
if d.Kind == ast.Typ {
if k != objectname {
continue
}
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
ColorLog("Unknown type without TypeSec: %v", d)
os.Exit(1)
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
foundTypeButNotStruct = true
continue
}
if isRoot {
m.ID = k
}
if st.Fields.List != nil {
if m.Properties == nil {
m.Properties = make(map[string]swagger.ModelProperty)
}
for _, field := range st.Fields.List {
mp := swagger.ModelProperty{}
isSlice, realType := typeAnalyser(field)
if isSlice {
if isBasicType(realType) {
mp.Type = "[]" + realType
} else {
mp.Type = "array"
mp.Items = make(map[string]string)
mp.Items["$ref"] = realType
}
} else {
mp.Type = getSwaggerTypeName(realType)
}
// not anonymous field
if field.Names != nil {
// set property name as field name
var name = field.Names[0].Name
// if no tag skip tag processing
if field.Tag == nil {
m.Properties[name] = mp
continue
}
var tagValues []string
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
tag := stag.Get("json")
if tag != "" {
tagValues = strings.Split(tag, ",")
}
// dont add property if json tag first value is "-"
if len(tagValues) == 0 || tagValues[0] != "-" {
if isRoot {
*realTypes = append(*realTypes, &_RealType{
RealTypeName: realType,
SourceFile: fl,
SourceFilePkg: pkgRealpath[len(topPath):],
})
}
// set property name to the left most json tag value only if is not omitempty
if len(tagValues) > 0 && tagValues[0] != "omitempty" {
name = tagValues[0]
}
//.........这里部分代码省略.........
开发者ID:dapeng-fu,项目名称:bee,代码行数:101,代码来源:g_docs.go
注:本文中的github.com/astaxie/beego/swagger.ModelProperty类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论