podman
140 строк · 3.9 Кб
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 spec16
17import (18"encoding/json"19"fmt"20"reflect"21"strconv"22"strings"23
24"github.com/go-openapi/swag"25)
26
27// Responses is a container for the expected responses of an operation.
28// The container maps a HTTP response code to the expected response.
29// It is not expected from the documentation to necessarily cover all possible HTTP response codes,
30// since they may not be known in advance. However, it is expected from the documentation to cover
31// a successful operation response and any known errors.
32//
33// The `default` can be used a default response object for all HTTP codes that are not covered
34// individually by the specification.
35//
36// The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
37// for a successful operation call.
38//
39// For more information: http://goo.gl/8us55a#responsesObject
40type Responses struct {41VendorExtensible
42ResponsesProps
43}
44
45// JSONLookup implements an interface to customize json pointer lookup
46func (r Responses) JSONLookup(token string) (interface{}, error) {47if token == "default" {48return r.Default, nil49}50if ex, ok := r.Extensions[token]; ok {51return &ex, nil52}53if i, err := strconv.Atoi(token); err == nil {54if scr, ok := r.StatusCodeResponses[i]; ok {55return scr, nil56}57}58return nil, fmt.Errorf("object has no field %q", token)59}
60
61// UnmarshalJSON hydrates this items instance with the data from JSON
62func (r *Responses) UnmarshalJSON(data []byte) error {63if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {64return err65}66
67if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {68return err69}70if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {71r.ResponsesProps = ResponsesProps{}72}73return nil74}
75
76// MarshalJSON converts this items object to JSON
77func (r Responses) MarshalJSON() ([]byte, error) {78b1, err := json.Marshal(r.ResponsesProps)79if err != nil {80return nil, err81}82b2, err := json.Marshal(r.VendorExtensible)83if err != nil {84return nil, err85}86concated := swag.ConcatJSON(b1, b2)87return concated, nil88}
89
90// ResponsesProps describes all responses for an operation.
91// It tells what is the default response and maps all responses with a
92// HTTP status code.
93type ResponsesProps struct {94Default *Response95StatusCodeResponses map[int]Response96}
97
98// MarshalJSON marshals responses as JSON
99func (r ResponsesProps) MarshalJSON() ([]byte, error) {100toser := map[string]Response{}101if r.Default != nil {102toser["default"] = *r.Default103}104for k, v := range r.StatusCodeResponses {105toser[strconv.Itoa(k)] = v106}107return json.Marshal(toser)108}
109
110// UnmarshalJSON unmarshals responses from JSON
111func (r *ResponsesProps) UnmarshalJSON(data []byte) error {112var res map[string]json.RawMessage113if err := json.Unmarshal(data, &res); err != nil {114return err115}116
117if v, ok := res["default"]; ok {118var defaultRes Response119if err := json.Unmarshal(v, &defaultRes); err != nil {120return err121}122r.Default = &defaultRes123delete(res, "default")124}125for k, v := range res {126if !strings.HasPrefix(k, "x-") {127var statusCodeResp Response128if err := json.Unmarshal(v, &statusCodeResp); err != nil {129return err130}131if nk, err := strconv.Atoi(k); err == nil {132if r.StatusCodeResponses == nil {133r.StatusCodeResponses = map[int]Response{}134}135r.StatusCodeResponses[nk] = statusCodeResp136}137}138}139return nil140}
141