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 { 32 VendorExtensible 33 Paths 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) { 38 if pi, ok := p.Paths[token]; ok { 39 return &pi, nil 40 } 41 if ex, ok := p.Extensions[token]; ok { 42 return &ex, nil 43 } 44 return 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 { 49 var res map[string]json.RawMessage 50 if err := json.Unmarshal(data, &res); err != nil { 51 return err 52 } 53 for k, v := range res { 54 if strings.HasPrefix(strings.ToLower(k), "x-") { 55 if p.Extensions == nil { 56 p.Extensions = make(map[string]interface{}) 57 } 58 var d interface{} 59 if err := json.Unmarshal(v, &d); err != nil { 60 return err 61 } 62 p.Extensions[k] = d 63 } 64 if strings.HasPrefix(k, "/") { 65 if p.Paths == nil { 66 p.Paths = make(map[string]PathItem) 67 } 68 var pi PathItem 69 if err := json.Unmarshal(v, &pi); err != nil { 70 return err 71 } 72 p.Paths[k] = pi 73 } 74 } 75 return nil 76} 77 78// MarshalJSON converts this items object to JSON 79func (p Paths) MarshalJSON() ([]byte, error) { 80 b1, err := json.Marshal(p.VendorExtensible) 81 if err != nil { 82 return nil, err 83 } 84 85 pths := make(map[string]PathItem) 86 for k, v := range p.Paths { 87 if strings.HasPrefix(k, "/") { 88 pths[k] = v 89 } 90 } 91 b2, err := json.Marshal(pths) 92 if err != nil { 93 return nil, err 94 } 95 concated := swag.ConcatJSON(b1, b2) 96 return concated, nil 97} 98