1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "github.com/go-openapi/jsonpointer"
22 "github.com/go-openapi/swag"
25 // Extensions vendor specific extensions
26 type Extensions map[string]interface{}
28 // Add adds a value to these extensions
29 func (e Extensions) Add(key string, value interface{}) {
30 realKey := strings.ToLower(key)
34 // GetString gets a string value from the extensions
35 func (e Extensions) GetString(key string) (string, bool) {
36 if v, ok := e[strings.ToLower(key)]; ok {
43 // GetBool gets a string value from the extensions
44 func (e Extensions) GetBool(key string) (bool, bool) {
45 if v, ok := e[strings.ToLower(key)]; ok {
52 // GetStringSlice gets a string value from the extensions
53 func (e Extensions) GetStringSlice(key string) ([]string, bool) {
54 if v, ok := e[strings.ToLower(key)]; ok {
55 arr, isSlice := v.([]interface{})
60 for _, iface := range arr {
61 str, isString := iface.(string)
65 strs = append(strs, str)
72 // VendorExtensible composition block.
73 type VendorExtensible struct {
77 // AddExtension adds an extension to this extensible object
78 func (v *VendorExtensible) AddExtension(key string, value interface{}) {
82 if v.Extensions == nil {
83 v.Extensions = make(map[string]interface{})
85 v.Extensions.Add(key, value)
88 // MarshalJSON marshals the extensions to json
89 func (v VendorExtensible) MarshalJSON() ([]byte, error) {
90 toser := make(map[string]interface{})
91 for k, v := range v.Extensions {
92 lk := strings.ToLower(k)
93 if strings.HasPrefix(lk, "x-") {
97 return json.Marshal(toser)
100 // UnmarshalJSON for this extensible object
101 func (v *VendorExtensible) UnmarshalJSON(data []byte) error {
102 var d map[string]interface{}
103 if err := json.Unmarshal(data, &d); err != nil {
106 for k, vv := range d {
107 lk := strings.ToLower(k)
108 if strings.HasPrefix(lk, "x-") {
109 if v.Extensions == nil {
110 v.Extensions = map[string]interface{}{}
118 // InfoProps the properties for an info definition
119 type InfoProps struct {
120 Description string `json:"description,omitempty"`
121 Title string `json:"title,omitempty"`
122 TermsOfService string `json:"termsOfService,omitempty"`
123 Contact *ContactInfo `json:"contact,omitempty"`
124 License *License `json:"license,omitempty"`
125 Version string `json:"version,omitempty"`
128 // Info object provides metadata about the API.
129 // The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience.
131 // For more information: http://goo.gl/8us55a#infoObject
137 // JSONLookup look up a value by the json property name
138 func (i Info) JSONLookup(token string) (interface{}, error) {
139 if ex, ok := i.Extensions[token]; ok {
142 r, _, err := jsonpointer.GetForToken(i.InfoProps, token)
146 // MarshalJSON marshal this to JSON
147 func (i Info) MarshalJSON() ([]byte, error) {
148 b1, err := json.Marshal(i.InfoProps)
152 b2, err := json.Marshal(i.VendorExtensible)
156 return swag.ConcatJSON(b1, b2), nil
159 // UnmarshalJSON marshal this from JSON
160 func (i *Info) UnmarshalJSON(data []byte) error {
161 if err := json.Unmarshal(data, &i.InfoProps); err != nil {
164 return json.Unmarshal(data, &i.VendorExtensible)