You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paths.go 2.6KB

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