header.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "github.com/go-openapi/swag"
  18. )
  19. type HeaderProps struct {
  20. Description string `json:"description,omitempty"`
  21. }
  22. // Header describes a header for a response of the API
  23. //
  24. // For more information: http://goo.gl/8us55a#headerObject
  25. type Header struct {
  26. CommonValidations
  27. SimpleSchema
  28. HeaderProps
  29. }
  30. // ResponseHeader creates a new header instance for use in a response
  31. func ResponseHeader() *Header {
  32. return new(Header)
  33. }
  34. // WithDescription sets the description on this response, allows for chaining
  35. func (h *Header) WithDescription(description string) *Header {
  36. h.Description = description
  37. return h
  38. }
  39. // Typed a fluent builder method for the type of parameter
  40. func (h *Header) Typed(tpe, format string) *Header {
  41. h.Type = tpe
  42. h.Format = format
  43. return h
  44. }
  45. // CollectionOf a fluent builder method for an array item
  46. func (h *Header) CollectionOf(items *Items, format string) *Header {
  47. h.Type = "array"
  48. h.Items = items
  49. h.CollectionFormat = format
  50. return h
  51. }
  52. // WithDefault sets the default value on this item
  53. func (h *Header) WithDefault(defaultValue interface{}) *Header {
  54. h.Default = defaultValue
  55. return h
  56. }
  57. // WithMaxLength sets a max length value
  58. func (h *Header) WithMaxLength(max int64) *Header {
  59. h.MaxLength = &max
  60. return h
  61. }
  62. // WithMinLength sets a min length value
  63. func (h *Header) WithMinLength(min int64) *Header {
  64. h.MinLength = &min
  65. return h
  66. }
  67. // WithPattern sets a pattern value
  68. func (h *Header) WithPattern(pattern string) *Header {
  69. h.Pattern = pattern
  70. return h
  71. }
  72. // WithMultipleOf sets a multiple of value
  73. func (h *Header) WithMultipleOf(number float64) *Header {
  74. h.MultipleOf = &number
  75. return h
  76. }
  77. // WithMaximum sets a maximum number value
  78. func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
  79. h.Maximum = &max
  80. h.ExclusiveMaximum = exclusive
  81. return h
  82. }
  83. // WithMinimum sets a minimum number value
  84. func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
  85. h.Minimum = &min
  86. h.ExclusiveMinimum = exclusive
  87. return h
  88. }
  89. // WithEnum sets a the enum values (replace)
  90. func (h *Header) WithEnum(values ...interface{}) *Header {
  91. h.Enum = append([]interface{}{}, values...)
  92. return h
  93. }
  94. // WithMaxItems sets the max items
  95. func (h *Header) WithMaxItems(size int64) *Header {
  96. h.MaxItems = &size
  97. return h
  98. }
  99. // WithMinItems sets the min items
  100. func (h *Header) WithMinItems(size int64) *Header {
  101. h.MinItems = &size
  102. return h
  103. }
  104. // UniqueValues dictates that this array can only have unique items
  105. func (h *Header) UniqueValues() *Header {
  106. h.UniqueItems = true
  107. return h
  108. }
  109. // AllowDuplicates this array can have duplicates
  110. func (h *Header) AllowDuplicates() *Header {
  111. h.UniqueItems = false
  112. return h
  113. }
  114. // MarshalJSON marshal this to JSON
  115. func (h Header) MarshalJSON() ([]byte, error) {
  116. b1, err := json.Marshal(h.CommonValidations)
  117. if err != nil {
  118. return nil, err
  119. }
  120. b2, err := json.Marshal(h.SimpleSchema)
  121. if err != nil {
  122. return nil, err
  123. }
  124. b3, err := json.Marshal(h.HeaderProps)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return swag.ConcatJSON(b1, b2, b3), nil
  129. }
  130. // UnmarshalJSON marshal this from JSON
  131. func (h *Header) UnmarshalJSON(data []byte) error {
  132. if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
  133. return err
  134. }
  135. if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
  136. return err
  137. }
  138. if err := json.Unmarshal(data, &h.HeaderProps); err != nil {
  139. return err
  140. }
  141. return nil
  142. }