ref.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "net/http"
  18. "os"
  19. "path/filepath"
  20. "github.com/go-openapi/jsonreference"
  21. "k8s.io/kube-openapi/pkg/internal"
  22. )
  23. // Refable is a struct for things that accept a $ref property
  24. type Refable struct {
  25. Ref Ref
  26. }
  27. // MarshalJSON marshals the ref to json
  28. func (r Refable) MarshalJSON() ([]byte, error) {
  29. return r.Ref.MarshalJSON()
  30. }
  31. // UnmarshalJSON unmarshalss the ref from json
  32. func (r *Refable) UnmarshalJSON(d []byte) error {
  33. return json.Unmarshal(d, &r.Ref)
  34. }
  35. // Ref represents a json reference that is potentially resolved
  36. type Ref struct {
  37. jsonreference.Ref
  38. }
  39. // RemoteURI gets the remote uri part of the ref
  40. func (r *Ref) RemoteURI() string {
  41. if r.String() == "" {
  42. return r.String()
  43. }
  44. u := *r.GetURL()
  45. u.Fragment = ""
  46. return u.String()
  47. }
  48. // IsValidURI returns true when the url the ref points to can be found
  49. func (r *Ref) IsValidURI(basepaths ...string) bool {
  50. if r.String() == "" {
  51. return true
  52. }
  53. v := r.RemoteURI()
  54. if v == "" {
  55. return true
  56. }
  57. if r.HasFullURL {
  58. rr, err := http.Get(v)
  59. if err != nil {
  60. return false
  61. }
  62. return rr.StatusCode/100 == 2
  63. }
  64. if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) {
  65. return false
  66. }
  67. // check for local file
  68. pth := v
  69. if r.HasURLPathOnly {
  70. base := "."
  71. if len(basepaths) > 0 {
  72. base = filepath.Dir(filepath.Join(basepaths...))
  73. }
  74. p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth)))
  75. if e != nil {
  76. return false
  77. }
  78. pth = p
  79. }
  80. fi, err := os.Stat(filepath.ToSlash(pth))
  81. if err != nil {
  82. return false
  83. }
  84. return !fi.IsDir()
  85. }
  86. // Inherits creates a new reference from a parent and a child
  87. // If the child cannot inherit from the parent, an error is returned
  88. func (r *Ref) Inherits(child Ref) (*Ref, error) {
  89. ref, err := r.Ref.Inherits(child.Ref)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &Ref{Ref: *ref}, nil
  94. }
  95. // NewRef creates a new instance of a ref object
  96. // returns an error when the reference uri is an invalid uri
  97. func NewRef(refURI string) (Ref, error) {
  98. ref, err := jsonreference.New(refURI)
  99. if err != nil {
  100. return Ref{}, err
  101. }
  102. return Ref{Ref: ref}, nil
  103. }
  104. // MustCreateRef creates a ref object but panics when refURI is invalid.
  105. // Use the NewRef method for a version that returns an error.
  106. func MustCreateRef(refURI string) Ref {
  107. return Ref{Ref: jsonreference.MustCreateRef(refURI)}
  108. }
  109. // MarshalJSON marshals this ref into a JSON object
  110. func (r Ref) MarshalJSON() ([]byte, error) {
  111. str := r.String()
  112. if str == "" {
  113. if r.IsRoot() {
  114. return []byte(`{"$ref":""}`), nil
  115. }
  116. return []byte("{}"), nil
  117. }
  118. v := map[string]interface{}{"$ref": str}
  119. return json.Marshal(v)
  120. }
  121. // UnmarshalJSON unmarshals this ref from a JSON object
  122. func (r *Ref) UnmarshalJSON(d []byte) error {
  123. var v map[string]interface{}
  124. if err := json.Unmarshal(d, &v); err != nil {
  125. return err
  126. }
  127. return r.fromMap(v)
  128. }
  129. func (r *Ref) fromMap(v map[string]interface{}) error {
  130. return internal.JSONRefFromMap(&r.Ref, v)
  131. }