Exemplo n.º 1
0
func setSchemaType(s *spec.Schema, f *descriptor.Field) {
	if f.GetLabel() == godesc.FieldDescriptorProto_LABEL_REPEATED {
		s.Type = spec.StringOrArray([]string{"array"})

		items := new(spec.Schema)
		if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
			items.Ref = messageRef(f.Message)
		} else {
			ty, format := toSwaggerType(f.GetType())
			items.Type = spec.StringOrArray([]string{ty})
			items.Format = format
		}

		s.Items = &spec.SchemaOrArray{Schema: items}
		return
	}

	if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
		s.Ref = messageRef(f.Message)
		return
	}

	ty, format := toSwaggerType(f.GetType())
	s.Type = spec.StringOrArray([]string{ty})
	s.Format = format
}
Exemplo n.º 2
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.º 3
0
func setParameterType(s *spec.Parameter, f *descriptor.Field) {
	if f.GetLabel() == godesc.FieldDescriptorProto_LABEL_REPEATED {
		s.Type = "array"
		items := new(spec.Items)
		if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
			items.Ref = spec.MustCreateRef("#/definitions/" + f.GetTypeName())
		} else {
			ty, format := toSwaggerType(f.GetType())
			items.Type = ty
			items.Format = format
		}

		s.Items = items
		return
	}

	if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
		s.Ref = spec.MustCreateRef("#/definitions/" + f.GetTypeName())
		return
	}

	ty, format := toSwaggerType(f.GetType())
	s.Type = ty
	s.Format = format
}