forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/info.go

317 lines
4.7 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"strconv"
"strings"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
// Extensions vendor specific extensions
2024-05-14 13:07:09 +00:00
type Extensions map[string]interface{}
// Add adds a value to these extensions
2024-05-14 13:07:09 +00:00
func (e Extensions) Add(key string, value interface{}) {
2024-05-14 13:07:09 +00:00
realKey := strings.ToLower(key)
2024-05-14 13:07:09 +00:00
e[realKey] = value
2024-05-14 13:07:09 +00:00
}
// GetString gets a string value from the extensions
2024-05-14 13:07:09 +00:00
func (e Extensions) GetString(key string) (string, bool) {
2024-05-14 13:07:09 +00:00
if v, ok := e[strings.ToLower(key)]; ok {
2024-05-14 13:07:09 +00:00
str, ok := v.(string)
2024-05-14 13:07:09 +00:00
return str, ok
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return "", false
2024-05-14 13:07:09 +00:00
}
// GetInt gets a int value from the extensions
2024-05-14 13:07:09 +00:00
func (e Extensions) GetInt(key string) (int, bool) {
2024-05-14 13:07:09 +00:00
realKey := strings.ToLower(key)
if v, ok := e.GetString(realKey); ok {
2024-05-14 13:07:09 +00:00
if r, err := strconv.Atoi(v); err == nil {
2024-05-14 13:07:09 +00:00
return r, true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
if v, ok := e[realKey]; ok {
2024-05-14 13:07:09 +00:00
if r, rOk := v.(float64); rOk {
2024-05-14 13:07:09 +00:00
return int(r), true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return -1, false
2024-05-14 13:07:09 +00:00
}
// GetBool gets a string value from the extensions
2024-05-14 13:07:09 +00:00
func (e Extensions) GetBool(key string) (bool, bool) {
2024-05-14 13:07:09 +00:00
if v, ok := e[strings.ToLower(key)]; ok {
2024-05-14 13:07:09 +00:00
str, ok := v.(bool)
2024-05-14 13:07:09 +00:00
return str, ok
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return false, false
2024-05-14 13:07:09 +00:00
}
// GetStringSlice gets a string value from the extensions
2024-05-14 13:07:09 +00:00
func (e Extensions) GetStringSlice(key string) ([]string, bool) {
2024-05-14 13:07:09 +00:00
if v, ok := e[strings.ToLower(key)]; ok {
2024-05-14 13:07:09 +00:00
arr, isSlice := v.([]interface{})
2024-05-14 13:07:09 +00:00
if !isSlice {
2024-05-14 13:07:09 +00:00
return nil, false
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var strs []string
2024-05-14 13:07:09 +00:00
for _, iface := range arr {
2024-05-14 13:07:09 +00:00
str, isString := iface.(string)
2024-05-14 13:07:09 +00:00
if !isString {
2024-05-14 13:07:09 +00:00
return nil, false
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
strs = append(strs, str)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return strs, ok
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil, false
2024-05-14 13:07:09 +00:00
}
// VendorExtensible composition block.
2024-05-14 13:07:09 +00:00
type VendorExtensible struct {
Extensions Extensions
}
// AddExtension adds an extension to this extensible object
2024-05-14 13:07:09 +00:00
func (v *VendorExtensible) AddExtension(key string, value interface{}) {
2024-05-14 13:07:09 +00:00
if value == nil {
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if v.Extensions == nil {
2024-05-14 13:07:09 +00:00
v.Extensions = make(map[string]interface{})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
v.Extensions.Add(key, value)
2024-05-14 13:07:09 +00:00
}
// MarshalJSON marshals the extensions to json
2024-05-14 13:07:09 +00:00
func (v VendorExtensible) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
toser := make(map[string]interface{})
2024-05-14 13:07:09 +00:00
for k, v := range v.Extensions {
2024-05-14 13:07:09 +00:00
lk := strings.ToLower(k)
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(lk, "x-") {
2024-05-14 13:07:09 +00:00
toser[k] = v
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Marshal(toser)
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON for this extensible object
2024-05-14 13:07:09 +00:00
func (v *VendorExtensible) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
var d map[string]interface{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &d); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for k, vv := range d {
2024-05-14 13:07:09 +00:00
lk := strings.ToLower(k)
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(lk, "x-") {
2024-05-14 13:07:09 +00:00
if v.Extensions == nil {
2024-05-14 13:07:09 +00:00
v.Extensions = map[string]interface{}{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
v.Extensions[k] = vv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// InfoProps the properties for an info definition
2024-05-14 13:07:09 +00:00
type InfoProps struct {
Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact *ContactInfo `json:"contact,omitempty"`
License *License `json:"license,omitempty"`
Version string `json:"version,omitempty"`
2024-05-14 13:07:09 +00:00
}
// Info object provides metadata about the API.
2024-05-14 13:07:09 +00:00
// The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#infoObject
2024-05-14 13:07:09 +00:00
type Info struct {
VendorExtensible
2024-05-14 13:07:09 +00:00
InfoProps
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (i Info) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if ex, ok := i.Extensions[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r, _, err := jsonpointer.GetForToken(i.InfoProps, token)
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}
// MarshalJSON marshal this to JSON
2024-05-14 13:07:09 +00:00
func (i Info) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(i.InfoProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b2, err := json.Marshal(i.VendorExtensible)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return swag.ConcatJSON(b1, b2), nil
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON marshal this from JSON
2024-05-14 13:07:09 +00:00
func (i *Info) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &i.InfoProps); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Unmarshal(data, &i.VendorExtensible)
2024-05-14 13:07:09 +00:00
}