本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

192 lines
4.1KB

  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. "bytes"
  17. "encoding/gob"
  18. "encoding/json"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "github.com/go-openapi/jsonreference"
  23. )
  24. // Refable is a struct for things that accept a $ref property
  25. type Refable struct {
  26. Ref Ref
  27. }
  28. // MarshalJSON marshals the ref to json
  29. func (r Refable) MarshalJSON() ([]byte, error) {
  30. return r.Ref.MarshalJSON()
  31. }
  32. // UnmarshalJSON unmarshalss the ref from json
  33. func (r *Refable) UnmarshalJSON(d []byte) error {
  34. return json.Unmarshal(d, &r.Ref)
  35. }
  36. // Ref represents a json reference that is potentially resolved
  37. type Ref struct {
  38. jsonreference.Ref
  39. }
  40. // RemoteURI gets the remote uri part of the ref
  41. func (r *Ref) RemoteURI() string {
  42. if r.String() == "" {
  43. return r.String()
  44. }
  45. u := *r.GetURL()
  46. u.Fragment = ""
  47. return u.String()
  48. }
  49. // IsValidURI returns true when the url the ref points to can be found
  50. func (r *Ref) IsValidURI(basepaths ...string) bool {
  51. if r.String() == "" {
  52. return true
  53. }
  54. v := r.RemoteURI()
  55. if v == "" {
  56. return true
  57. }
  58. if r.HasFullURL {
  59. rr, err := http.Get(v)
  60. if err != nil {
  61. return false
  62. }
  63. return rr.StatusCode/100 == 2
  64. }
  65. if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) {
  66. return false
  67. }
  68. // check for local file
  69. pth := v
  70. if r.HasURLPathOnly {
  71. base := "."
  72. if len(basepaths) > 0 {
  73. base = filepath.Dir(filepath.Join(basepaths...))
  74. }
  75. p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth)))
  76. if e != nil {
  77. return false
  78. }
  79. pth = p
  80. }
  81. fi, err := os.Stat(filepath.ToSlash(pth))
  82. if err != nil {
  83. return false
  84. }
  85. return !fi.IsDir()
  86. }
  87. // Inherits creates a new reference from a parent and a child
  88. // If the child cannot inherit from the parent, an error is returned
  89. func (r *Ref) Inherits(child Ref) (*Ref, error) {
  90. ref, err := r.Ref.Inherits(child.Ref)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return &Ref{Ref: *ref}, nil
  95. }
  96. // NewRef creates a new instance of a ref object
  97. // returns an error when the reference uri is an invalid uri
  98. func NewRef(refURI string) (Ref, error) {
  99. ref, err := jsonreference.New(refURI)
  100. if err != nil {
  101. return Ref{}, err
  102. }
  103. return Ref{Ref: ref}, nil
  104. }
  105. // MustCreateRef creates a ref object but panics when refURI is invalid.
  106. // Use the NewRef method for a version that returns an error.
  107. func MustCreateRef(refURI string) Ref {
  108. return Ref{Ref: jsonreference.MustCreateRef(refURI)}
  109. }
  110. // MarshalJSON marshals this ref into a JSON object
  111. func (r Ref) MarshalJSON() ([]byte, error) {
  112. str := r.String()
  113. if str == "" {
  114. if r.IsRoot() {
  115. return []byte(`{"$ref":""}`), nil
  116. }
  117. return []byte("{}"), nil
  118. }
  119. v := map[string]interface{}{"$ref": str}
  120. return json.Marshal(v)
  121. }
  122. // UnmarshalJSON unmarshals this ref from a JSON object
  123. func (r *Ref) UnmarshalJSON(d []byte) error {
  124. var v map[string]interface{}
  125. if err := json.Unmarshal(d, &v); err != nil {
  126. return err
  127. }
  128. return r.fromMap(v)
  129. }
  130. // GobEncode provides a safe gob encoder for Ref
  131. func (r Ref) GobEncode() ([]byte, error) {
  132. var b bytes.Buffer
  133. raw, err := r.MarshalJSON()
  134. if err != nil {
  135. return nil, err
  136. }
  137. err = gob.NewEncoder(&b).Encode(raw)
  138. return b.Bytes(), err
  139. }
  140. // GobDecode provides a safe gob decoder for Ref
  141. func (r *Ref) GobDecode(b []byte) error {
  142. var raw []byte
  143. buf := bytes.NewBuffer(b)
  144. err := gob.NewDecoder(buf).Decode(&raw)
  145. if err != nil {
  146. return err
  147. }
  148. return json.Unmarshal(raw, r)
  149. }
  150. func (r *Ref) fromMap(v map[string]interface{}) error {
  151. if v == nil {
  152. return nil
  153. }
  154. if vv, ok := v["$ref"]; ok {
  155. if str, ok := vv.(string); ok {
  156. ref, err := jsonreference.New(str)
  157. if err != nil {
  158. return err
  159. }
  160. *r = Ref{Ref: ref}
  161. }
  162. }
  163. return nil
  164. }
上海开阖软件有限公司 沪ICP备12045867号-1