model_property_ext.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package swagger
  2. import (
  3. "reflect"
  4. "strings"
  5. )
  6. func (prop *ModelProperty) setDescription(field reflect.StructField) {
  7. if tag := field.Tag.Get("description"); tag != "" {
  8. prop.Description = tag
  9. }
  10. }
  11. func (prop *ModelProperty) setDefaultValue(field reflect.StructField) {
  12. if tag := field.Tag.Get("default"); tag != "" {
  13. prop.DefaultValue = Special(tag)
  14. }
  15. }
  16. func (prop *ModelProperty) setEnumValues(field reflect.StructField) {
  17. // We use | to separate the enum values. This value is chosen
  18. // since its unlikely to be useful in actual enumeration values.
  19. if tag := field.Tag.Get("enum"); tag != "" {
  20. prop.Enum = strings.Split(tag, "|")
  21. }
  22. }
  23. func (prop *ModelProperty) setMaximum(field reflect.StructField) {
  24. if tag := field.Tag.Get("maximum"); tag != "" {
  25. prop.Maximum = tag
  26. }
  27. }
  28. func (prop *ModelProperty) setType(field reflect.StructField) {
  29. if tag := field.Tag.Get("type"); tag != "" {
  30. prop.Type = &tag
  31. }
  32. }
  33. func (prop *ModelProperty) setMinimum(field reflect.StructField) {
  34. if tag := field.Tag.Get("minimum"); tag != "" {
  35. prop.Minimum = tag
  36. }
  37. }
  38. func (prop *ModelProperty) setUniqueItems(field reflect.StructField) {
  39. tag := field.Tag.Get("unique")
  40. switch tag {
  41. case "true":
  42. v := true
  43. prop.UniqueItems = &v
  44. case "false":
  45. v := false
  46. prop.UniqueItems = &v
  47. }
  48. }
  49. func (prop *ModelProperty) setPropertyMetadata(field reflect.StructField) {
  50. prop.setDescription(field)
  51. prop.setEnumValues(field)
  52. prop.setMinimum(field)
  53. prop.setMaximum(field)
  54. prop.setUniqueItems(field)
  55. prop.setDefaultValue(field)
  56. prop.setType(field)
  57. }