model_property_ext.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Check if the first two characters of the type tag are
  31. // intended to emulate slice/array behaviour.
  32. //
  33. // If type is intended to be a slice/array then add the
  34. // overriden type to the array item instead of the main property
  35. if len(tag) > 2 && tag[0:2] == "[]" {
  36. pType := "array"
  37. prop.Type = &pType
  38. prop.Items = new(Item)
  39. iType := tag[2:]
  40. prop.Items.Type = &iType
  41. return
  42. }
  43. prop.Type = &tag
  44. }
  45. }
  46. func (prop *ModelProperty) setMinimum(field reflect.StructField) {
  47. if tag := field.Tag.Get("minimum"); tag != "" {
  48. prop.Minimum = tag
  49. }
  50. }
  51. func (prop *ModelProperty) setUniqueItems(field reflect.StructField) {
  52. tag := field.Tag.Get("unique")
  53. switch tag {
  54. case "true":
  55. v := true
  56. prop.UniqueItems = &v
  57. case "false":
  58. v := false
  59. prop.UniqueItems = &v
  60. }
  61. }
  62. func (prop *ModelProperty) setPropertyMetadata(field reflect.StructField) {
  63. prop.setDescription(field)
  64. prop.setEnumValues(field)
  65. prop.setMinimum(field)
  66. prop.setMaximum(field)
  67. prop.setUniqueItems(field)
  68. prop.setDefaultValue(field)
  69. prop.setType(field)
  70. }