本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

175 行
3.8KB

  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. // }
上海开阖软件有限公司 沪ICP备12045867号-1