本文整理汇总了Golang中github.com/vikstrous/go-swagger/spec.Schema.Properties方法的典型用法代码示例。如果您正苦于以下问题:Golang Schema.Properties方法的具体用法?Golang Schema.Properties怎么用?Golang Schema.Properties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/vikstrous/go-swagger/spec.Schema
的用法示例。
在下文中一共展示了Schema.Properties方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTypeResolver_AnonymousStructs
func TestTypeResolver_AnonymousStructs(t *testing.T) {
_, resolver, err := basicTaskListResolver(t)
if assert.NoError(t, err) {
// anonymous structs should be accounted for
parent := new(spec.Schema)
parent.Typed("object", "")
parent.Properties = make(map[string]spec.Schema)
parent.Properties["name"] = *spec.StringProperty()
parent.Properties["age"] = *spec.Int32Property()
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.False(t, rt.IsNullable)
assert.True(t, rt.IsAnonymous)
assert.True(t, rt.IsComplexObject)
}
parent.Extensions = make(spec.Extensions)
parent.Extensions["x-isnullable"] = true
rt, err = resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsNullable)
assert.True(t, rt.IsAnonymous)
assert.True(t, rt.IsComplexObject)
}
}
}
示例2: buildItems
func (sg *schemaGenContext) buildItems() error {
presentsAsSingle := sg.Schema.Items != nil && sg.Schema.Items.Schema != nil
if presentsAsSingle && sg.Schema.AdditionalItems != nil { // unsure if htis a valid of invalid schema
return fmt.Errorf("single schema (%s) can't have additional items", sg.Name)
}
if presentsAsSingle {
return sg.buildArray()
}
if sg.Schema.Items == nil {
return nil
}
// This is a tuple, build a new model that represents this
if sg.Named {
sg.GenSchema.Name = sg.Name
sg.GenSchema.GoType = swag.ToGoName(sg.Name)
if sg.TypeResolver.ModelsPackage != "" {
sg.GenSchema.GoType = sg.TypeResolver.ModelsPackage + "." + sg.GenSchema.GoType
}
for i, s := range sg.Schema.Items.Schemas {
elProp := sg.NewTupleElement(&s, i)
if err := elProp.makeGenSchema(); err != nil {
return err
}
sg.MergeResult(elProp)
elProp.GenSchema.Name = "p" + strconv.Itoa(i)
sg.GenSchema.Properties = append(sg.GenSchema.Properties, elProp.GenSchema)
}
return nil
}
// for an anonoymous object, first build the new object
// and then replace the current one with a $ref to the
// new tuple object
var sch spec.Schema
sch.Typed("object", "")
sch.Properties = make(map[string]spec.Schema)
for i, v := range sg.Schema.Items.Schemas {
sch.Required = append(sch.Required, "P"+strconv.Itoa(i))
sch.Properties["P"+strconv.Itoa(i)] = v
}
sch.AdditionalItems = sg.Schema.AdditionalItems
tup := sg.makeNewStruct(sg.GenSchema.Name+"Tuple"+strconv.Itoa(sg.Index), sch)
if err := tup.makeGenSchema(); err != nil {
return err
}
tup.GenSchema.IsTuple = true
tup.GenSchema.IsComplexObject = false
tup.GenSchema.Title = tup.GenSchema.Name + " a representation of an anonymous Tuple type"
tup.GenSchema.Description = ""
sg.ExtraSchemas[tup.Name] = tup.GenSchema
sg.Schema = *spec.RefProperty("#/definitions/" + tup.Name)
if err := sg.makeGenSchema(); err != nil {
return err
}
sg.MergeResult(tup)
return nil
}
示例3: 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 {
if len(fld.Names) > 0 && fld.Names[0] != nil && fld.Names[0].IsExported() {
var nm, gnm string
nm = fld.Names[0].Name
gnm = nm
if fld.Tag != nil && len(strings.TrimSpace(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}); 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}),
}
// check if this is a primitive, if so parse the validations from the
// doc comments of the slice declaration.
if ftpe, ok := fld.Type.(*ast.ArrayType); ok {
if iftpe, ok := ftpe.Elt.(*ast.Ident); ok && iftpe.Obj == nil {
if ps.Items != nil && ps.Items.Schema != nil {
itemsTaggers := []tagParser{
newSingleLineTagParser("itemsMaximum", &setMaximum{schemaValidations{ps.Items.Schema}, rxf(rxMaximumFmt, rxItemsPrefix)}),
newSingleLineTagParser("itemsMinimum", &setMinimum{schemaValidations{ps.Items.Schema}, rxf(rxMinimumFmt, rxItemsPrefix)}),
newSingleLineTagParser("itemsMultipleOf", &setMultipleOf{schemaValidations{ps.Items.Schema}, rxf(rxMultipleOfFmt, rxItemsPrefix)}),
newSingleLineTagParser("itemsMinLength", &setMinLength{schemaValidations{ps.Items.Schema}, rxf(rxMinLengthFmt, rxItemsPrefix)}),
newSingleLineTagParser("itemsMaxLength", &setMaxLength{schemaValidations{ps.Items.Schema}, rxf(rxMaxLengthFmt, rxItemsPrefix)}),
newSingleLineTagParser("itemsPattern", &setPattern{schemaValidations{ps.Items.Schema}, rxf(rxPatternFmt, rxItemsPrefix)}),
//.........这里部分代码省略.........
示例4: 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)
}
}
}
示例5: TestTypeResolver_AdditionalProperties
func TestTypeResolver_AdditionalProperties(t *testing.T) {
_, resolver, err := basicTaskListResolver(t)
if assert.NoError(t, err) {
// primitives as additional properties
for _, val := range schTypeVals {
sch := new(spec.Schema)
sch.Typed(val.Type, val.Format)
parent := new(spec.Schema)
parent.AdditionalProperties = new(spec.SchemaOrBool)
parent.AdditionalProperties.Schema = sch
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsMap)
assert.False(t, rt.IsComplexObject)
assert.Equal(t, "map[string]"+val.Expected, rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
// array of primitives as additional properties
for _, val := range schTypeVals {
sch := new(spec.Schema)
sch.Typed(val.Type, val.Format)
parent := new(spec.Schema)
parent.AdditionalProperties = new(spec.SchemaOrBool)
parent.AdditionalProperties.Schema = new(spec.Schema).CollectionOf(*sch)
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsMap)
assert.False(t, rt.IsComplexObject)
assert.Equal(t, "map[string][]"+val.Expected, rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
// refs as additional properties
for _, val := range schRefVals {
sch := new(spec.Schema)
sch.Ref, _ = spec.NewRef("#/definitions/" + val.Type)
parent := new(spec.Schema)
parent.AdditionalProperties = new(spec.SchemaOrBool)
parent.AdditionalProperties.Schema = sch
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsMap)
assert.False(t, rt.IsComplexObject)
assert.Equal(t, "map[string]"+val.Expected, rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
// when additional properties and properties present, it's a complex object
// primitives as additional properties
for _, val := range schTypeVals {
sch := new(spec.Schema)
sch.Typed(val.Type, val.Format)
parent := new(spec.Schema)
parent.Properties = make(map[string]spec.Schema)
parent.Properties["id"] = *spec.Int32Property()
parent.AdditionalProperties = new(spec.SchemaOrBool)
parent.AdditionalProperties.Schema = sch
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsComplexObject)
assert.False(t, rt.IsMap)
assert.Equal(t, "map[string]"+val.Expected, rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
// array of primitives as additional properties
for _, val := range schTypeVals {
sch := new(spec.Schema)
sch.Typed(val.Type, val.Format)
parent := new(spec.Schema)
parent.Properties = make(map[string]spec.Schema)
parent.Properties["id"] = *spec.Int32Property()
parent.AdditionalProperties = new(spec.SchemaOrBool)
parent.AdditionalProperties.Schema = new(spec.Schema).CollectionOf(*sch)
rt, err := resolver.ResolveSchema(parent, true)
if assert.NoError(t, err) {
assert.True(t, rt.IsComplexObject)
assert.False(t, rt.IsMap)
assert.Equal(t, "map[string][]"+val.Expected, rt.GoType)
assert.Equal(t, "object", rt.SwaggerType)
}
}
// refs as additional properties
//.........这里部分代码省略.........