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