podman
97 строк · 2.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 spec
16
17import (
18"encoding/json"
19"fmt"
20"strings"
21
22"github.com/go-openapi/swag"
23)
24
25// Paths holds the relative paths to the individual endpoints.
26// The path is appended to the [`basePath`](http://goo.gl/8us55a#swaggerBasePath) in order
27// to construct the full URL.
28// The Paths may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
29//
30// For more information: http://goo.gl/8us55a#pathsObject
31type Paths struct {
32VendorExtensible
33Paths map[string]PathItem `json:"-"` // custom serializer to flatten this, each entry must start with "/"
34}
35
36// JSONLookup look up a value by the json property name
37func (p Paths) JSONLookup(token string) (interface{}, error) {
38if pi, ok := p.Paths[token]; ok {
39return &pi, nil
40}
41if ex, ok := p.Extensions[token]; ok {
42return &ex, nil
43}
44return nil, fmt.Errorf("object has no field %q", token)
45}
46
47// UnmarshalJSON hydrates this items instance with the data from JSON
48func (p *Paths) UnmarshalJSON(data []byte) error {
49var res map[string]json.RawMessage
50if err := json.Unmarshal(data, &res); err != nil {
51return err
52}
53for k, v := range res {
54if strings.HasPrefix(strings.ToLower(k), "x-") {
55if p.Extensions == nil {
56p.Extensions = make(map[string]interface{})
57}
58var d interface{}
59if err := json.Unmarshal(v, &d); err != nil {
60return err
61}
62p.Extensions[k] = d
63}
64if strings.HasPrefix(k, "/") {
65if p.Paths == nil {
66p.Paths = make(map[string]PathItem)
67}
68var pi PathItem
69if err := json.Unmarshal(v, &pi); err != nil {
70return err
71}
72p.Paths[k] = pi
73}
74}
75return nil
76}
77
78// MarshalJSON converts this items object to JSON
79func (p Paths) MarshalJSON() ([]byte, error) {
80b1, err := json.Marshal(p.VendorExtensible)
81if err != nil {
82return nil, err
83}
84
85pths := make(map[string]PathItem)
86for k, v := range p.Paths {
87if strings.HasPrefix(k, "/") {
88pths[k] = v
89}
90}
91b2, err := json.Marshal(pths)
92if err != nil {
93return nil, err
94}
95concated := swag.ConcatJSON(b1, b2)
96return concated, nil
97}
98