podman
152 строки · 4.2 Кб
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
15package spec
16
17import (
18"encoding/json"
19
20"github.com/go-openapi/jsonpointer"
21"github.com/go-openapi/swag"
22)
23
24// ResponseProps properties specific to a response
25type ResponseProps struct {
26Description string `json:"description"`
27Schema *Schema `json:"schema,omitempty"`
28Headers map[string]Header `json:"headers,omitempty"`
29Examples map[string]interface{} `json:"examples,omitempty"`
30}
31
32// Response describes a single response from an API Operation.
33//
34// For more information: http://goo.gl/8us55a#responseObject
35type Response struct {
36Refable
37ResponseProps
38VendorExtensible
39}
40
41// JSONLookup look up a value by the json property name
42func (r Response) JSONLookup(token string) (interface{}, error) {
43if ex, ok := r.Extensions[token]; ok {
44return &ex, nil
45}
46if token == "$ref" {
47return &r.Ref, nil
48}
49ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
50return ptr, err
51}
52
53// UnmarshalJSON hydrates this items instance with the data from JSON
54func (r *Response) UnmarshalJSON(data []byte) error {
55if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
56return err
57}
58if err := json.Unmarshal(data, &r.Refable); err != nil {
59return err
60}
61return json.Unmarshal(data, &r.VendorExtensible)
62}
63
64// MarshalJSON converts this items object to JSON
65func (r Response) MarshalJSON() ([]byte, error) {
66var (
67b1 []byte
68err error
69)
70
71if r.Ref.String() == "" {
72// when there is no $ref, empty description is rendered as an empty string
73b1, err = json.Marshal(r.ResponseProps)
74} else {
75// when there is $ref inside the schema, description should be omitempty-ied
76b1, err = json.Marshal(struct {
77Description string `json:"description,omitempty"`
78Schema *Schema `json:"schema,omitempty"`
79Headers map[string]Header `json:"headers,omitempty"`
80Examples map[string]interface{} `json:"examples,omitempty"`
81}{
82Description: r.ResponseProps.Description,
83Schema: r.ResponseProps.Schema,
84Examples: r.ResponseProps.Examples,
85})
86}
87if err != nil {
88return nil, err
89}
90
91b2, err := json.Marshal(r.Refable)
92if err != nil {
93return nil, err
94}
95b3, err := json.Marshal(r.VendorExtensible)
96if err != nil {
97return nil, err
98}
99return swag.ConcatJSON(b1, b2, b3), nil
100}
101
102// NewResponse creates a new response instance
103func NewResponse() *Response {
104return new(Response)
105}
106
107// ResponseRef creates a response as a json reference
108func ResponseRef(url string) *Response {
109resp := NewResponse()
110resp.Ref = MustCreateRef(url)
111return resp
112}
113
114// WithDescription sets the description on this response, allows for chaining
115func (r *Response) WithDescription(description string) *Response {
116r.Description = description
117return r
118}
119
120// WithSchema sets the schema on this response, allows for chaining.
121// Passing a nil argument removes the schema from this response
122func (r *Response) WithSchema(schema *Schema) *Response {
123r.Schema = schema
124return r
125}
126
127// AddHeader adds a header to this response
128func (r *Response) AddHeader(name string, header *Header) *Response {
129if header == nil {
130return r.RemoveHeader(name)
131}
132if r.Headers == nil {
133r.Headers = make(map[string]Header)
134}
135r.Headers[name] = *header
136return r
137}
138
139// RemoveHeader removes a header from this response
140func (r *Response) RemoveHeader(name string) *Response {
141delete(r.Headers, name)
142return r
143}
144
145// AddExample adds an example to this response
146func (r *Response) AddExample(mediaType string, example interface{}) *Response {
147if r.Examples == nil {
148r.Examples = make(map[string]interface{})
149}
150r.Examples[mediaType] = example
151return r
152}
153