当前位置: 首页>>代码示例>>Golang>>正文


Golang Schema.AllOf方法代码示例

本文整理汇总了Golang中github.com/go-swagger/go-swagger/spec.Schema.AllOf方法的典型用法代码示例。如果您正苦于以下问题:Golang Schema.AllOf方法的具体用法?Golang Schema.AllOf怎么用?Golang Schema.AllOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/go-swagger/go-swagger/spec.Schema的用法示例。


在下文中一共展示了Schema.AllOf方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestTypeResolver_ObjectType

func TestTypeResolver_ObjectType(t *testing.T) {
	_, resolver, err := basicTaskListResolver(t)
	resolver.ModelName = "TheModel"
	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)
			if assert.NoError(t, err) {
				assert.True(t, rt.IsMap)
				assert.False(t, rt.IsComplexObject)
				assert.Equal(t, "map[string]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)
			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)
			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)
		if assert.NoError(t, err) {
			assert.True(t, rt.IsMap)
			assert.False(t, rt.IsComplexObject)
			assert.Equal(t, "map[string]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)
		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)
		}
	}
}
开发者ID:rgbkrk,项目名称:go-swagger,代码行数:62,代码来源:typeresolver_test.go

示例2: 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
		}

//.........这里部分代码省略.........
开发者ID:jak-atx,项目名称:vic,代码行数:101,代码来源:schema.go

示例3: 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 {
//.........这里部分代码省略.........
开发者ID:jak-atx,项目名称:vic,代码行数:101,代码来源:schema.go

示例4: parseStructType

func (scp *schemaParser) parseStructType(gofile *ast.File, bschema *spec.Schema, tpe *ast.StructType, seenPreviously map[string]struct{}) error {
	if tpe.Fields != nil {
		var schema *spec.Schema
		seenProperties := seenPreviously

		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) {
					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
					}
					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.parseEmbeddedStruct(gofile, schema, fld.Type, seenProperties); err != nil {
					return err
				}
			}
		}
		if schema != nil && len(bschema.AllOf) > 0 {
			bschema.AllOf = append(bschema.AllOf, *schema)
		}
		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
				}

				sp := new(sectionedParser)
				sp.setDescription = func(lines []string) { ps.Description = joinDropLast(lines) }
				if ps.Ref.GetURL() == nil {
					sp.taggers = []tagParser{
						newSingleLineTagParser("maximum", &setMaximum{schemaValidations{&ps}, rxf(rxMaximumFmt, "")}),
						newSingleLineTagParser("minimum", &setMinimum{schemaValidations{&ps}, rxf(rxMinimumFmt, "")}),
						newSingleLineTagParser("multipleOf", &setMultipleOf{schemaValidations{&ps}, rxf(rxMultipleOfFmt, "")}),
						newSingleLineTagParser("minLength", &setMinLength{schemaValidations{&ps}, rxf(rxMinLengthFmt, "")}),
						newSingleLineTagParser("maxLength", &setMaxLength{schemaValidations{&ps}, rxf(rxMaxLengthFmt, "")}),
						newSingleLineTagParser("pattern", &setPattern{schemaValidations{&ps}, rxf(rxPatternFmt, "")}),
						newSingleLineTagParser("minItems", &setMinItems{schemaValidations{&ps}, rxf(rxMinItemsFmt, "")}),
						newSingleLineTagParser("maxItems", &setMaxItems{schemaValidations{&ps}, rxf(rxMaxItemsFmt, "")}),
						newSingleLineTagParser("unique", &setUnique{schemaValidations{&ps}, rxf(rxUniqueFmt, "")}),
						newSingleLineTagParser("required", &setRequiredSchema{schema, nm}),
						newSingleLineTagParser("readOnly", &setReadOnlySchema{&ps}),
					}

					itemsTaggers := func(items *spec.Schema, level int) []tagParser {
						// the expression is 1-index based not 0-index
						itemsPrefix := fmt.Sprintf(rxItemsPrefixFmt, level+1)
						return []tagParser{
							newSingleLineTagParser(fmt.Sprintf("items%dMaximum", level), &setMaximum{schemaValidations{items}, rxf(rxMaximumFmt, itemsPrefix)}),
//.........这里部分代码省略.........
开发者ID:ChrisPRobinson,项目名称:go-swagger,代码行数:101,代码来源:schema.go


注:本文中的github.com/go-swagger/go-swagger/spec.Schema.AllOf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。