display.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2017 Google LLC. All Rights Reserved.
  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 jsonschema
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. //
  20. // DISPLAY
  21. // The following methods display Schemas.
  22. //
  23. // Description returns a string representation of a string or string array.
  24. func (s *StringOrStringArray) Description() string {
  25. if s.String != nil {
  26. return *s.String
  27. }
  28. if s.StringArray != nil {
  29. return strings.Join(*s.StringArray, ", ")
  30. }
  31. return ""
  32. }
  33. // Returns a string representation of a Schema.
  34. func (schema *Schema) String() string {
  35. return schema.describeSchema("")
  36. }
  37. // Helper: Returns a string representation of a Schema indented by a specified string.
  38. func (schema *Schema) describeSchema(indent string) string {
  39. result := ""
  40. if schema.Schema != nil {
  41. result += indent + "$schema: " + *(schema.Schema) + "\n"
  42. }
  43. if schema.ID != nil {
  44. result += indent + "id: " + *(schema.ID) + "\n"
  45. }
  46. if schema.MultipleOf != nil {
  47. result += indent + fmt.Sprintf("multipleOf: %+v\n", *(schema.MultipleOf))
  48. }
  49. if schema.Maximum != nil {
  50. result += indent + fmt.Sprintf("maximum: %+v\n", *(schema.Maximum))
  51. }
  52. if schema.ExclusiveMaximum != nil {
  53. result += indent + fmt.Sprintf("exclusiveMaximum: %+v\n", *(schema.ExclusiveMaximum))
  54. }
  55. if schema.Minimum != nil {
  56. result += indent + fmt.Sprintf("minimum: %+v\n", *(schema.Minimum))
  57. }
  58. if schema.ExclusiveMinimum != nil {
  59. result += indent + fmt.Sprintf("exclusiveMinimum: %+v\n", *(schema.ExclusiveMinimum))
  60. }
  61. if schema.MaxLength != nil {
  62. result += indent + fmt.Sprintf("maxLength: %+v\n", *(schema.MaxLength))
  63. }
  64. if schema.MinLength != nil {
  65. result += indent + fmt.Sprintf("minLength: %+v\n", *(schema.MinLength))
  66. }
  67. if schema.Pattern != nil {
  68. result += indent + fmt.Sprintf("pattern: %+v\n", *(schema.Pattern))
  69. }
  70. if schema.AdditionalItems != nil {
  71. s := schema.AdditionalItems.Schema
  72. if s != nil {
  73. result += indent + "additionalItems:\n"
  74. result += s.describeSchema(indent + " ")
  75. } else {
  76. b := *(schema.AdditionalItems.Boolean)
  77. result += indent + fmt.Sprintf("additionalItems: %+v\n", b)
  78. }
  79. }
  80. if schema.Items != nil {
  81. result += indent + "items:\n"
  82. items := schema.Items
  83. if items.SchemaArray != nil {
  84. for i, s := range *(items.SchemaArray) {
  85. result += indent + " " + fmt.Sprintf("%d", i) + ":\n"
  86. result += s.describeSchema(indent + " " + " ")
  87. }
  88. } else if items.Schema != nil {
  89. result += items.Schema.describeSchema(indent + " " + " ")
  90. }
  91. }
  92. if schema.MaxItems != nil {
  93. result += indent + fmt.Sprintf("maxItems: %+v\n", *(schema.MaxItems))
  94. }
  95. if schema.MinItems != nil {
  96. result += indent + fmt.Sprintf("minItems: %+v\n", *(schema.MinItems))
  97. }
  98. if schema.UniqueItems != nil {
  99. result += indent + fmt.Sprintf("uniqueItems: %+v\n", *(schema.UniqueItems))
  100. }
  101. if schema.MaxProperties != nil {
  102. result += indent + fmt.Sprintf("maxProperties: %+v\n", *(schema.MaxProperties))
  103. }
  104. if schema.MinProperties != nil {
  105. result += indent + fmt.Sprintf("minProperties: %+v\n", *(schema.MinProperties))
  106. }
  107. if schema.Required != nil {
  108. result += indent + fmt.Sprintf("required: %+v\n", *(schema.Required))
  109. }
  110. if schema.AdditionalProperties != nil {
  111. s := schema.AdditionalProperties.Schema
  112. if s != nil {
  113. result += indent + "additionalProperties:\n"
  114. result += s.describeSchema(indent + " ")
  115. } else {
  116. b := *(schema.AdditionalProperties.Boolean)
  117. result += indent + fmt.Sprintf("additionalProperties: %+v\n", b)
  118. }
  119. }
  120. if schema.Properties != nil {
  121. result += indent + "properties:\n"
  122. for _, pair := range *(schema.Properties) {
  123. name := pair.Name
  124. s := pair.Value
  125. result += indent + " " + name + ":\n"
  126. result += s.describeSchema(indent + " " + " ")
  127. }
  128. }
  129. if schema.PatternProperties != nil {
  130. result += indent + "patternProperties:\n"
  131. for _, pair := range *(schema.PatternProperties) {
  132. name := pair.Name
  133. s := pair.Value
  134. result += indent + " " + name + ":\n"
  135. result += s.describeSchema(indent + " " + " ")
  136. }
  137. }
  138. if schema.Dependencies != nil {
  139. result += indent + "dependencies:\n"
  140. for _, pair := range *(schema.Dependencies) {
  141. name := pair.Name
  142. schemaOrStringArray := pair.Value
  143. s := schemaOrStringArray.Schema
  144. if s != nil {
  145. result += indent + " " + name + ":\n"
  146. result += s.describeSchema(indent + " " + " ")
  147. } else {
  148. a := schemaOrStringArray.StringArray
  149. if a != nil {
  150. result += indent + " " + name + ":\n"
  151. for _, s2 := range *a {
  152. result += indent + " " + " " + s2 + "\n"
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if schema.Enumeration != nil {
  159. result += indent + "enumeration:\n"
  160. for _, value := range *(schema.Enumeration) {
  161. if value.String != nil {
  162. result += indent + " " + fmt.Sprintf("%+v\n", *value.String)
  163. } else {
  164. result += indent + " " + fmt.Sprintf("%+v\n", *value.Bool)
  165. }
  166. }
  167. }
  168. if schema.Type != nil {
  169. result += indent + fmt.Sprintf("type: %+v\n", schema.Type.Description())
  170. }
  171. if schema.AllOf != nil {
  172. result += indent + "allOf:\n"
  173. for _, s := range *(schema.AllOf) {
  174. result += s.describeSchema(indent + " ")
  175. result += indent + "-\n"
  176. }
  177. }
  178. if schema.AnyOf != nil {
  179. result += indent + "anyOf:\n"
  180. for _, s := range *(schema.AnyOf) {
  181. result += s.describeSchema(indent + " ")
  182. result += indent + "-\n"
  183. }
  184. }
  185. if schema.OneOf != nil {
  186. result += indent + "oneOf:\n"
  187. for _, s := range *(schema.OneOf) {
  188. result += s.describeSchema(indent + " ")
  189. result += indent + "-\n"
  190. }
  191. }
  192. if schema.Not != nil {
  193. result += indent + "not:\n"
  194. result += schema.Not.describeSchema(indent + " ")
  195. }
  196. if schema.Definitions != nil {
  197. result += indent + "definitions:\n"
  198. for _, pair := range *(schema.Definitions) {
  199. name := pair.Name
  200. s := pair.Value
  201. result += indent + " " + name + ":\n"
  202. result += s.describeSchema(indent + " " + " ")
  203. }
  204. }
  205. if schema.Title != nil {
  206. result += indent + "title: " + *(schema.Title) + "\n"
  207. }
  208. if schema.Description != nil {
  209. result += indent + "description: " + *(schema.Description) + "\n"
  210. }
  211. if schema.Default != nil {
  212. result += indent + "default:\n"
  213. result += indent + fmt.Sprintf(" %+v\n", *(schema.Default))
  214. }
  215. if schema.Format != nil {
  216. result += indent + "format: " + *(schema.Format) + "\n"
  217. }
  218. if schema.Ref != nil {
  219. result += indent + "$ref: " + *(schema.Ref) + "\n"
  220. }
  221. return result
  222. }