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

173 lines
2.6 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"
"fmt"
"strings"
"github.com/go-openapi/swag"
)
// Paths holds the relative paths to the individual endpoints.
2024-05-14 13:07:09 +00:00
// The path is appended to the [`basePath`](http://goo.gl/8us55a#swaggerBasePath) in order
2024-05-14 13:07:09 +00:00
// to construct the full URL.
2024-05-14 13:07:09 +00:00
// The Paths may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#pathsObject
2024-05-14 13:07:09 +00:00
type Paths struct {
VendorExtensible
2024-05-14 13:07:09 +00:00
Paths map[string]PathItem `json:"-"` // custom serializer to flatten this, each entry must start with "/"
2024-05-14 13:07:09 +00:00
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (p Paths) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if pi, ok := p.Paths[token]; ok {
2024-05-14 13:07:09 +00:00
return &pi, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if ex, ok := p.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
return nil, fmt.Errorf("object has no field %q", token)
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON hydrates this items instance with the data from JSON
2024-05-14 13:07:09 +00:00
func (p *Paths) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
var res map[string]json.RawMessage
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &res); 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, v := range res {
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(strings.ToLower(k), "x-") {
2024-05-14 13:07:09 +00:00
if p.Extensions == nil {
2024-05-14 13:07:09 +00:00
p.Extensions = make(map[string]interface{})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var d interface{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(v, &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
p.Extensions[k] = d
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(k, "/") {
2024-05-14 13:07:09 +00:00
if p.Paths == nil {
2024-05-14 13:07:09 +00:00
p.Paths = make(map[string]PathItem)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var pi PathItem
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(v, &pi); 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
p.Paths[k] = pi
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
}
// MarshalJSON converts this items object to JSON
2024-05-14 13:07:09 +00:00
func (p Paths) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(p.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
}
pths := make(map[string]PathItem)
2024-05-14 13:07:09 +00:00
for k, v := range p.Paths {
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(k, "/") {
2024-05-14 13:07:09 +00:00
pths[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
b2, err := json.Marshal(pths)
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
concated := swag.ConcatJSON(b1, b2)
2024-05-14 13:07:09 +00:00
return concated, nil
2024-05-14 13:07:09 +00:00
}