本文整理匯總了Golang中github.com/go-openapi/spec.Schema.AllOf方法的典型用法代碼示例。如果您正苦於以下問題:Golang Schema.AllOf方法的具體用法?Golang Schema.AllOf怎麽用?Golang Schema.AllOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/go-openapi/spec.Schema
的用法示例。
在下文中一共展示了Schema.AllOf方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseInterfaceType
func (scp *schemaParser) parseInterfaceType(gofile *ast.File, bschema *spec.Schema, tpe *ast.InterfaceType, seenPreviously map[string]struct{}) error {
if tpe.Methods == nil {
return nil
}
// first check if this has embedded interfaces, if so make sure to refer to those by ref
// when they are decorated with an allOf annotation
// go over the method list again and this time collect the nullary methods and parse the comments
// as if they are properties on a struct
var schema *spec.Schema
seenProperties := seenPreviously
hasAllOf := false
for _, fld := range tpe.Methods.List {
if len(fld.Names) == 0 {
// if this created an allOf property then we have to rejig the schema var
// because all the fields collected that aren't from embedded structs should go in
// their own proper schema
// first process embedded structs in order of embedding
if allOfMember(fld.Doc) {
hasAllOf = true
if schema == nil {
schema = new(spec.Schema)
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseAllOfMember(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxAllOf.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
mv := matches[ml-1]
if mv != "" {
bschema.AddExtension("x-class", mv)
}
}
}
}
}
bschema.AllOf = append(bschema.AllOf, newSch)
continue
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseEmbeddedType(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
bschema.AllOf = append(bschema.AllOf, newSch)
hasAllOf = true
}
}
if schema == nil {
schema = bschema
}
// then add and possibly override values
if schema.Properties == nil {
schema.Properties = make(map[string]spec.Schema)
}
schema.Typed("object", "")
for _, fld := range tpe.Methods.List {
if mtpe, ok := fld.Type.(*ast.FuncType); ok && mtpe.Params.NumFields() == 0 && mtpe.Results.NumFields() == 1 {
gnm := fld.Names[0].Name
nm := gnm
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxName.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
nm = matches[ml-1]
}
}
}
}
ps := schema.Properties[nm]
if err := parseProperty(scp, gofile, mtpe.Results.List[0].Type, schemaTypable{&ps, 0}); err != nil {
return err
}
if err := scp.createParser(nm, schema, &ps, fld).Parse(fld.Doc); err != nil {
return err
}
if nm != gnm {
ps.AddExtension("x-go-name", gnm)
}
seenProperties[nm] = struct{}{}
schema.Properties[nm] = ps
}
//.........這裏部分代碼省略.........
示例2: parseStructType
func (scp *schemaParser) parseStructType(gofile *ast.File, bschema *spec.Schema, tpe *ast.StructType, seenPreviously map[string]struct{}) error {
if tpe.Fields == nil {
return nil
}
var schema *spec.Schema
seenProperties := seenPreviously
hasAllOf := false
for _, fld := range tpe.Fields.List {
if len(fld.Names) == 0 {
// if this created an allOf property then we have to rejig the schema var
// because all the fields collected that aren't from embedded structs should go in
// their own proper schema
// first process embedded structs in order of embedding
if allOfMember(fld.Doc) {
hasAllOf = true
if schema == nil {
schema = new(spec.Schema)
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseAllOfMember(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxAllOf.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
mv := matches[ml-1]
if mv != "" {
bschema.AddExtension("x-class", mv)
}
}
}
}
}
bschema.AllOf = append(bschema.AllOf, newSch)
continue
}
if schema == nil {
schema = bschema
}
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseEmbeddedType(gofile, schema, fld.Type, seenProperties); err != nil {
return err
}
}
}
if schema == nil {
schema = bschema
}
// then add and possibly override values
if schema.Properties == nil {
schema.Properties = make(map[string]spec.Schema)
}
schema.Typed("object", "")
for _, fld := range tpe.Fields.List {
var tag string
if fld.Tag != nil {
val, err := strconv.Unquote(fld.Tag.Value)
if err == nil {
tag = reflect.StructTag(val).Get("json")
}
}
if len(fld.Names) > 0 && fld.Names[0] != nil && fld.Names[0].IsExported() && (tag == "" || tag[0] != '-') {
var nm, gnm string
nm = fld.Names[0].Name
gnm = nm
if fld.Tag != nil && len(strings.TrimSpace(fld.Tag.Value)) > 0 /*&& fld.Tag.Value[0] != '-'*/ {
tv, err := strconv.Unquote(fld.Tag.Value)
if err != nil {
return err
}
if strings.TrimSpace(tv) != "" {
st := reflect.StructTag(tv)
if st.Get("json") != "" {
nm = strings.Split(st.Get("json"), ",")[0]
}
}
}
ps := schema.Properties[nm]
if err := parseProperty(scp, gofile, fld.Type, schemaTypable{&ps, 0}); err != nil {
return err
}
if err := scp.createParser(nm, schema, &ps, fld).Parse(fld.Doc); err != nil {
return err
}
if nm != gnm {
//.........這裏部分代碼省略.........
示例3: TestTypeResolver_ObjectType
func TestTypeResolver_ObjectType(t *testing.T) {
_, resolver, err := basicTaskListResolver(t)
resolver.ModelName = "TheModel"
resolver.KnownDefs["TheModel"] = struct{}{}
defer func() { resolver.ModelName = "" }()
if assert.NoError(t, err) {
//very poor schema definitions (as in none)
types := []string{"object", ""}
for _, tpe := range types {
sch := new(spec.Schema)
sch.Typed(tpe, "")
rt, err := resolver.ResolveSchema(sch, true, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsMap)
assert.False(t, rt.IsComplexObject)
assert.Equal(t, "interface{}", rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
sch.Properties = make(map[string]spec.Schema)
var ss spec.Schema
sch.Properties["tags"] = *(&ss).CollectionOf(*spec.StringProperty())
rt, err = resolver.ResolveSchema(sch, false, true)
assert.True(t, rt.IsComplexObject)
assert.False(t, rt.IsMap)
assert.Equal(t, "models.TheModel", rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
sch.Properties = nil
nsch := new(spec.Schema)
nsch.Typed(tpe, "")
nsch.AllOf = []spec.Schema{*sch}
rt, err = resolver.ResolveSchema(nsch, false, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsComplexObject)
assert.False(t, rt.IsMap)
assert.Equal(t, "models.TheModel", rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
sch := new(spec.Schema)
rt, err := resolver.ResolveSchema(sch, true, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsMap)
assert.False(t, rt.IsComplexObject)
assert.Equal(t, "interface{}", rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
sch = new(spec.Schema)
var sp spec.Schema
sp.Typed("object", "")
sch.AllOf = []spec.Schema{sp}
rt, err = resolver.ResolveSchema(sch, true, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsComplexObject)
assert.False(t, rt.IsMap)
assert.Equal(t, "models.TheModel", rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
}