本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 line
3.5KB

  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/jsonpointer"
  18. "github.com/go-openapi/swag"
  19. )
  20. // ResponseProps properties specific to a response
  21. type ResponseProps struct {
  22. Description string `json:"description,omitempty"`
  23. Schema *Schema `json:"schema,omitempty"`
  24. Headers map[string]Header `json:"headers,omitempty"`
  25. Examples map[string]interface{} `json:"examples,omitempty"`
  26. }
  27. // Response describes a single response from an API Operation.
  28. //
  29. // For more information: http://goo.gl/8us55a#responseObject
  30. type Response struct {
  31. Refable
  32. ResponseProps
  33. VendorExtensible
  34. }
  35. // JSONLookup look up a value by the json property name
  36. func (r Response) JSONLookup(token string) (interface{}, error) {
  37. if ex, ok := r.Extensions[token]; ok {
  38. return &ex, nil
  39. }
  40. if token == "$ref" {
  41. return &r.Ref, nil
  42. }
  43. ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
  44. return ptr, err
  45. }
  46. // UnmarshalJSON hydrates this items instance with the data from JSON
  47. func (r *Response) UnmarshalJSON(data []byte) error {
  48. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  49. return err
  50. }
  51. if err := json.Unmarshal(data, &r.Refable); err != nil {
  52. return err
  53. }
  54. return json.Unmarshal(data, &r.VendorExtensible)
  55. }
  56. // MarshalJSON converts this items object to JSON
  57. func (r Response) MarshalJSON() ([]byte, error) {
  58. b1, err := json.Marshal(r.ResponseProps)
  59. if err != nil {
  60. return nil, err
  61. }
  62. b2, err := json.Marshal(r.Refable)
  63. if err != nil {
  64. return nil, err
  65. }
  66. b3, err := json.Marshal(r.VendorExtensible)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return swag.ConcatJSON(b1, b2, b3), nil
  71. }
  72. // NewResponse creates a new response instance
  73. func NewResponse() *Response {
  74. return new(Response)
  75. }
  76. // ResponseRef creates a response as a json reference
  77. func ResponseRef(url string) *Response {
  78. resp := NewResponse()
  79. resp.Ref = MustCreateRef(url)
  80. return resp
  81. }
  82. // WithDescription sets the description on this response, allows for chaining
  83. func (r *Response) WithDescription(description string) *Response {
  84. r.Description = description
  85. return r
  86. }
  87. // WithSchema sets the schema on this response, allows for chaining.
  88. // Passing a nil argument removes the schema from this response
  89. func (r *Response) WithSchema(schema *Schema) *Response {
  90. r.Schema = schema
  91. return r
  92. }
  93. // AddHeader adds a header to this response
  94. func (r *Response) AddHeader(name string, header *Header) *Response {
  95. if header == nil {
  96. return r.RemoveHeader(name)
  97. }
  98. if r.Headers == nil {
  99. r.Headers = make(map[string]Header)
  100. }
  101. r.Headers[name] = *header
  102. return r
  103. }
  104. // RemoveHeader removes a header from this response
  105. func (r *Response) RemoveHeader(name string) *Response {
  106. delete(r.Headers, name)
  107. return r
  108. }
  109. // AddExample adds an example to this response
  110. func (r *Response) AddExample(mediaType string, example interface{}) *Response {
  111. if r.Examples == nil {
  112. r.Examples = make(map[string]interface{})
  113. }
  114. r.Examples[mediaType] = example
  115. return r
  116. }
上海开阖软件有限公司 沪ICP备12045867号-1