podman
184 строки · 4.6 Кб
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"strconv"20"strings"21
22"github.com/go-openapi/jsonpointer"23"github.com/go-openapi/swag"24)
25
26// Extensions vendor specific extensions
27type Extensions map[string]interface{}28
29// Add adds a value to these extensions
30func (e Extensions) Add(key string, value interface{}) {31realKey := strings.ToLower(key)32e[realKey] = value33}
34
35// GetString gets a string value from the extensions
36func (e Extensions) GetString(key string) (string, bool) {37if v, ok := e[strings.ToLower(key)]; ok {38str, ok := v.(string)39return str, ok40}41return "", false42}
43
44// GetInt gets a int value from the extensions
45func (e Extensions) GetInt(key string) (int, bool) {46realKey := strings.ToLower(key)47
48if v, ok := e.GetString(realKey); ok {49if r, err := strconv.Atoi(v); err == nil {50return r, true51}52}53
54if v, ok := e[realKey]; ok {55if r, rOk := v.(float64); rOk {56return int(r), true57}58}59return -1, false60}
61
62// GetBool gets a string value from the extensions
63func (e Extensions) GetBool(key string) (bool, bool) {64if v, ok := e[strings.ToLower(key)]; ok {65str, ok := v.(bool)66return str, ok67}68return false, false69}
70
71// GetStringSlice gets a string value from the extensions
72func (e Extensions) GetStringSlice(key string) ([]string, bool) {73if v, ok := e[strings.ToLower(key)]; ok {74arr, isSlice := v.([]interface{})75if !isSlice {76return nil, false77}78var strs []string79for _, iface := range arr {80str, isString := iface.(string)81if !isString {82return nil, false83}84strs = append(strs, str)85}86return strs, ok87}88return nil, false89}
90
91// VendorExtensible composition block.
92type VendorExtensible struct {93Extensions Extensions
94}
95
96// AddExtension adds an extension to this extensible object
97func (v *VendorExtensible) AddExtension(key string, value interface{}) {98if value == nil {99return100}101if v.Extensions == nil {102v.Extensions = make(map[string]interface{})103}104v.Extensions.Add(key, value)105}
106
107// MarshalJSON marshals the extensions to json
108func (v VendorExtensible) MarshalJSON() ([]byte, error) {109toser := make(map[string]interface{})110for k, v := range v.Extensions {111lk := strings.ToLower(k)112if strings.HasPrefix(lk, "x-") {113toser[k] = v114}115}116return json.Marshal(toser)117}
118
119// UnmarshalJSON for this extensible object
120func (v *VendorExtensible) UnmarshalJSON(data []byte) error {121var d map[string]interface{}122if err := json.Unmarshal(data, &d); err != nil {123return err124}125for k, vv := range d {126lk := strings.ToLower(k)127if strings.HasPrefix(lk, "x-") {128if v.Extensions == nil {129v.Extensions = map[string]interface{}{}130}131v.Extensions[k] = vv132}133}134return nil135}
136
137// InfoProps the properties for an info definition
138type InfoProps struct {139Description string `json:"description,omitempty"`140Title string `json:"title,omitempty"`141TermsOfService string `json:"termsOfService,omitempty"`142Contact *ContactInfo `json:"contact,omitempty"`143License *License `json:"license,omitempty"`144Version string `json:"version,omitempty"`145}
146
147// Info object provides metadata about the API.
148// The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience.
149//
150// For more information: http://goo.gl/8us55a#infoObject
151type Info struct {152VendorExtensible
153InfoProps
154}
155
156// JSONLookup look up a value by the json property name
157func (i Info) JSONLookup(token string) (interface{}, error) {158if ex, ok := i.Extensions[token]; ok {159return &ex, nil160}161r, _, err := jsonpointer.GetForToken(i.InfoProps, token)162return r, err163}
164
165// MarshalJSON marshal this to JSON
166func (i Info) MarshalJSON() ([]byte, error) {167b1, err := json.Marshal(i.InfoProps)168if err != nil {169return nil, err170}171b2, err := json.Marshal(i.VendorExtensible)172if err != nil {173return nil, err174}175return swag.ConcatJSON(b1, b2), nil176}
177
178// UnmarshalJSON marshal this from JSON
179func (i *Info) UnmarshalJSON(data []byte) error {180if err := json.Unmarshal(data, &i.InfoProps); err != nil {181return err182}183return json.Unmarshal(data, &i.VendorExtensible)184}
185