Ejemplo n.º 1
0
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 = sg.TypeResolver.goTypeName(sg.Name) // 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, false)
			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)
	tup.IsTuple = true
	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, false)
	return nil
}
Ejemplo n.º 2
0
func (g *Generator) generateDefinition(msg *descriptor.Message) *spec.Schema {
	s := new(spec.Schema)
	s.Title = msg.GetModelName()
	s.Properties = make(map[string]spec.Schema)
	s.Required = make([]string, 0)
	s.ExtraProps = make(map[string]interface{})

	// handle comments.
	if cmt := g.file.GetCommentText(msg.CommentPath); cmt != nil {
		s.Description = *cmt
	}

	// iterate over fields.
	for _, field := range msg.Fields {
		prop := g.generateProperty(field)

		if field.GetLabel() == godesc.FieldDescriptorProto_LABEL_REQUIRED {
			s.Required = append(s.Required, field.GetName())
		}

		s.Properties[field.GetName()] = *prop
	}

	// if msg.EnumType != nil {
	// 	s.ExtraProps["ext_enumType"] = msg.GetEnumType()
	// }
	// if msg.Extension != nil {
	// 	s.ExtraProps["ext_extension"] = msg.GetExtension()
	// }
	// if msg.ExtensionRange != nil {
	// 	s.ExtraProps["ext_extensionRange"] = msg.GetExtensionRange()
	// }
	// if msg.NestedType != nil {
	// 	s.ExtraProps["ext_nestedType"] = msg.GetNestedType()
	// }
	// if msg.OneofDecl != nil {
	// 	s.ExtraProps["ext_oneofDecl"] = msg.GetOneofDecl()
	// }
	// if msg.Options != nil {
	// 	s.ExtraProps["ext_options"] = msg.GetOptions()
	// }

	return s
}