Exemplo n.º 1
0
func (g *Generator) generateProperty(f *descriptor.Field) *spec.Schema {
	sc := new(spec.Schema)
	sc.ExtraProps = make(map[string]interface{})
	if cmt := g.file.GetCommentText(f.CommentPath); cmt != nil {
		sc.Description = *cmt
		// glog.Fatalf("Comment: %s, %s", f.CommentPath, *cmt)
	} else {
		// glog.Fatalf("Comment: %s, %s", f.CommentPath, f.GetName())
	}

	// Handle $ref
	setSchemaType(sc, f)

	// default value if exists
	if f.DefaultValue != nil {
		sc.Default = f.GetDefaultValue()
	}

	// export protobuf id
	// sc.ExtraProps["protobufId"] = f.GetNumber()
	//
	// if f.Options != nil {
	// 	sc.ExtraProps["ext_options"] = f.GetOptions()
	// }
	//
	// sc.ExtraProps["ext_extendee"] = f.GetExtendee()
	// sc.ExtraProps["ext_oneOfIndex"] = f.GetOneofIndex()

	return sc
}
Exemplo 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
}
Exemplo n.º 3
0
func (scp *schemaParser) createParser(nm string, schema, ps *spec.Schema, fld *ast.Field) *sectionedParser {

	sp := new(sectionedParser)
	sp.setDescription = func(lines []string) { ps.Description = joinDropLast(lines) }
	if ps.Ref.String() == "" {
		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}),
			newSingleLineTagParser("discriminator", &setDiscriminator{schema, nm}),
		}

		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)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMinimum", level), &setMinimum{schemaValidations{items}, rxf(rxMinimumFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMultipleOf", level), &setMultipleOf{schemaValidations{items}, rxf(rxMultipleOfFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMinLength", level), &setMinLength{schemaValidations{items}, rxf(rxMinLengthFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMaxLength", level), &setMaxLength{schemaValidations{items}, rxf(rxMaxLengthFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dPattern", level), &setPattern{schemaValidations{items}, rxf(rxPatternFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMinItems", level), &setMinItems{schemaValidations{items}, rxf(rxMinItemsFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dMaxItems", level), &setMaxItems{schemaValidations{items}, rxf(rxMaxItemsFmt, itemsPrefix)}),
				newSingleLineTagParser(fmt.Sprintf("items%dUnique", level), &setUnique{schemaValidations{items}, rxf(rxUniqueFmt, itemsPrefix)}),
			}

		}
		// check if this is a primitive, if so parse the validations from the
		// doc comments of the slice declaration.
		if ftped, ok := fld.Type.(*ast.ArrayType); ok {
			ftpe := ftped
			items, level := ps.Items, 0
			for items != nil && items.Schema != nil {
				switch iftpe := ftpe.Elt.(type) {
				case *ast.ArrayType:
					eleTaggers := itemsTaggers(items.Schema, level)
					sp.taggers = append(eleTaggers, sp.taggers...)
					ftpe = iftpe
				case *ast.Ident:
					if iftpe.Obj == nil {
						sp.taggers = append(itemsTaggers(items.Schema, level), sp.taggers...)
					}
					break
					//default:
					//return fmt.Errorf("unknown field type (%T) ele for %q", iftpe, nm)
				}
				items = items.Schema.Items
				level = level + 1
			}
		}
	} else {
		sp.taggers = []tagParser{
			newSingleLineTagParser("required", &setRequiredSchema{schema, nm}),
		}
	}
	return sp
}