unused.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /*
  16. import (
  17. "net/url"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "github.com/go-openapi/jsonpointer"
  22. )
  23. // Some currently unused functions and definitions that
  24. // used to be part of the expander.
  25. // Moved here for the record and possible future reuse
  26. var (
  27. idPtr, _ = jsonpointer.New("/id")
  28. refPtr, _ = jsonpointer.New("/$ref")
  29. )
  30. func idFromNode(node interface{}) (*Ref, error) {
  31. if idValue, _, err := idPtr.Get(node); err == nil {
  32. if refStr, ok := idValue.(string); ok && refStr != "" {
  33. idRef, err := NewRef(refStr)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &idRef, nil
  38. }
  39. }
  40. return nil, nil
  41. }
  42. func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointer) *Ref {
  43. if startingRef == nil {
  44. return nil
  45. }
  46. if ptr == nil {
  47. return startingRef
  48. }
  49. ret := startingRef
  50. var idRef *Ref
  51. node := startingNode
  52. for _, tok := range ptr.DecodedTokens() {
  53. node, _, _ = jsonpointer.GetForToken(node, tok)
  54. if node == nil {
  55. break
  56. }
  57. idRef, _ = idFromNode(node)
  58. if idRef != nil {
  59. nw, err := ret.Inherits(*idRef)
  60. if err != nil {
  61. break
  62. }
  63. ret = nw
  64. }
  65. refRef, _, _ := refPtr.Get(node)
  66. if refRef != nil {
  67. var rf Ref
  68. switch value := refRef.(type) {
  69. case string:
  70. rf, _ = NewRef(value)
  71. }
  72. nw, err := ret.Inherits(rf)
  73. if err != nil {
  74. break
  75. }
  76. nwURL := nw.GetURL()
  77. if nwURL.Scheme == "file" || (nwURL.Scheme == "" && nwURL.Host == "") {
  78. nwpt := filepath.ToSlash(nwURL.Path)
  79. if filepath.IsAbs(nwpt) {
  80. _, err := os.Stat(nwpt)
  81. if err != nil {
  82. nwURL.Path = filepath.Join(".", nwpt)
  83. }
  84. }
  85. }
  86. ret = nw
  87. }
  88. }
  89. return ret
  90. }
  91. // basePathFromSchemaID returns a new basePath based on an existing basePath and a schema ID
  92. func basePathFromSchemaID(oldBasePath, id string) string {
  93. u, err := url.Parse(oldBasePath)
  94. if err != nil {
  95. panic(err)
  96. }
  97. uid, err := url.Parse(id)
  98. if err != nil {
  99. panic(err)
  100. }
  101. if path.IsAbs(uid.Path) {
  102. return id
  103. }
  104. u.Path = path.Join(path.Dir(u.Path), uid.Path)
  105. return u.String()
  106. }
  107. */
  108. // type ExtraSchemaProps map[string]interface{}
  109. // // JSONSchema represents a structure that is a json schema draft 04
  110. // type JSONSchema struct {
  111. // SchemaProps
  112. // ExtraSchemaProps
  113. // }
  114. // // MarshalJSON marshal this to JSON
  115. // func (s JSONSchema) MarshalJSON() ([]byte, error) {
  116. // b1, err := json.Marshal(s.SchemaProps)
  117. // if err != nil {
  118. // return nil, err
  119. // }
  120. // b2, err := s.Ref.MarshalJSON()
  121. // if err != nil {
  122. // return nil, err
  123. // }
  124. // b3, err := s.Schema.MarshalJSON()
  125. // if err != nil {
  126. // return nil, err
  127. // }
  128. // b4, err := json.Marshal(s.ExtraSchemaProps)
  129. // if err != nil {
  130. // return nil, err
  131. // }
  132. // return swag.ConcatJSON(b1, b2, b3, b4), nil
  133. // }
  134. // // UnmarshalJSON marshal this from JSON
  135. // func (s *JSONSchema) UnmarshalJSON(data []byte) error {
  136. // var sch JSONSchema
  137. // if err := json.Unmarshal(data, &sch.SchemaProps); err != nil {
  138. // return err
  139. // }
  140. // if err := json.Unmarshal(data, &sch.Ref); err != nil {
  141. // return err
  142. // }
  143. // if err := json.Unmarshal(data, &sch.Schema); err != nil {
  144. // return err
  145. // }
  146. // if err := json.Unmarshal(data, &sch.ExtraSchemaProps); err != nil {
  147. // return err
  148. // }
  149. // *s = sch
  150. // return nil
  151. // }