123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- // Copyright 2017 Google LLC. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package jsonschema
- import (
- "fmt"
- "gopkg.in/yaml.v3"
- )
- const indentation = " "
- func renderMappingNode(node *yaml.Node, indent string) (result string) {
- result = "{\n"
- innerIndent := indent + indentation
- for i := 0; i < len(node.Content); i += 2 {
- // first print the key
- key := node.Content[i].Value
- result += fmt.Sprintf("%s\"%+v\": ", innerIndent, key)
- // then the value
- value := node.Content[i+1]
- switch value.Kind {
- case yaml.ScalarNode:
- result += "\"" + value.Value + "\""
- case yaml.MappingNode:
- result += renderMappingNode(value, innerIndent)
- case yaml.SequenceNode:
- result += renderSequenceNode(value, innerIndent)
- default:
- result += fmt.Sprintf("???MapItem(Key:%+v, Value:%T)", value, value)
- }
- if i < len(node.Content)-2 {
- result += ","
- }
- result += "\n"
- }
- result += indent + "}"
- return result
- }
- func renderSequenceNode(node *yaml.Node, indent string) (result string) {
- result = "[\n"
- innerIndent := indent + indentation
- for i := 0; i < len(node.Content); i++ {
- item := node.Content[i]
- switch item.Kind {
- case yaml.ScalarNode:
- result += innerIndent + "\"" + item.Value + "\""
- case yaml.MappingNode:
- result += innerIndent + renderMappingNode(item, innerIndent) + ""
- default:
- result += innerIndent + fmt.Sprintf("???ArrayItem(%+v)", item)
- }
- if i < len(node.Content)-1 {
- result += ","
- }
- result += "\n"
- }
- result += indent + "]"
- return result
- }
- func renderStringArray(array []string, indent string) (result string) {
- result = "[\n"
- innerIndent := indent + indentation
- for i, item := range array {
- result += innerIndent + "\"" + item + "\""
- if i < len(array)-1 {
- result += ","
- }
- result += "\n"
- }
- result += indent + "]"
- return result
- }
- // Render renders a yaml.Node as JSON
- func Render(node *yaml.Node) string {
- if node.Kind == yaml.DocumentNode {
- if len(node.Content) == 1 {
- return Render(node.Content[0])
- }
- } else if node.Kind == yaml.MappingNode {
- return renderMappingNode(node, "") + "\n"
- } else if node.Kind == yaml.SequenceNode {
- return renderSequenceNode(node, "") + "\n"
- }
- return ""
- }
- func (object *SchemaNumber) nodeValue() *yaml.Node {
- if object.Integer != nil {
- return nodeForInt64(*object.Integer)
- } else if object.Float != nil {
- return nodeForFloat64(*object.Float)
- } else {
- return nil
- }
- }
- func (object *SchemaOrBoolean) nodeValue() *yaml.Node {
- if object.Schema != nil {
- return object.Schema.nodeValue()
- } else if object.Boolean != nil {
- return nodeForBoolean(*object.Boolean)
- } else {
- return nil
- }
- }
- func nodeForStringArray(array []string) *yaml.Node {
- content := make([]*yaml.Node, 0)
- for _, item := range array {
- content = append(content, nodeForString(item))
- }
- return nodeForSequence(content)
- }
- func nodeForSchemaArray(array []*Schema) *yaml.Node {
- content := make([]*yaml.Node, 0)
- for _, item := range array {
- content = append(content, item.nodeValue())
- }
- return nodeForSequence(content)
- }
- func (object *StringOrStringArray) nodeValue() *yaml.Node {
- if object.String != nil {
- return nodeForString(*object.String)
- } else if object.StringArray != nil {
- return nodeForStringArray(*(object.StringArray))
- } else {
- return nil
- }
- }
- func (object *SchemaOrStringArray) nodeValue() *yaml.Node {
- if object.Schema != nil {
- return object.Schema.nodeValue()
- } else if object.StringArray != nil {
- return nodeForStringArray(*(object.StringArray))
- } else {
- return nil
- }
- }
- func (object *SchemaOrSchemaArray) nodeValue() *yaml.Node {
- if object.Schema != nil {
- return object.Schema.nodeValue()
- } else if object.SchemaArray != nil {
- return nodeForSchemaArray(*(object.SchemaArray))
- } else {
- return nil
- }
- }
- func (object *SchemaEnumValue) nodeValue() *yaml.Node {
- if object.String != nil {
- return nodeForString(*object.String)
- } else if object.Bool != nil {
- return nodeForBoolean(*object.Bool)
- } else {
- return nil
- }
- }
- func nodeForNamedSchemaArray(array *[]*NamedSchema) *yaml.Node {
- content := make([]*yaml.Node, 0)
- for _, pair := range *(array) {
- content = appendPair(content, pair.Name, pair.Value.nodeValue())
- }
- return nodeForMapping(content)
- }
- func nodeForNamedSchemaOrStringArray(array *[]*NamedSchemaOrStringArray) *yaml.Node {
- content := make([]*yaml.Node, 0)
- for _, pair := range *(array) {
- content = appendPair(content, pair.Name, pair.Value.nodeValue())
- }
- return nodeForMapping(content)
- }
- func nodeForSchemaEnumArray(array *[]SchemaEnumValue) *yaml.Node {
- content := make([]*yaml.Node, 0)
- for _, item := range *array {
- content = append(content, item.nodeValue())
- }
- return nodeForSequence(content)
- }
- func nodeForMapping(content []*yaml.Node) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.MappingNode,
- Content: content,
- }
- }
- func nodeForSequence(content []*yaml.Node) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.SequenceNode,
- Content: content,
- }
- }
- func nodeForString(value string) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.ScalarNode,
- Tag: "!!str",
- Value: value,
- }
- }
- func nodeForBoolean(value bool) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.ScalarNode,
- Tag: "!!bool",
- Value: fmt.Sprintf("%t", value),
- }
- }
- func nodeForInt64(value int64) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.ScalarNode,
- Tag: "!!int",
- Value: fmt.Sprintf("%d", value),
- }
- }
- func nodeForFloat64(value float64) *yaml.Node {
- return &yaml.Node{
- Kind: yaml.ScalarNode,
- Tag: "!!float",
- Value: fmt.Sprintf("%f", value),
- }
- }
- func appendPair(nodes []*yaml.Node, name string, value *yaml.Node) []*yaml.Node {
- nodes = append(nodes, nodeForString(name))
- nodes = append(nodes, value)
- return nodes
- }
- func (schema *Schema) nodeValue() *yaml.Node {
- n := &yaml.Node{Kind: yaml.MappingNode}
- content := make([]*yaml.Node, 0)
- if schema.Title != nil {
- content = appendPair(content, "title", nodeForString(*schema.Title))
- }
- if schema.ID != nil {
- content = appendPair(content, "id", nodeForString(*schema.ID))
- }
- if schema.Schema != nil {
- content = appendPair(content, "$schema", nodeForString(*schema.Schema))
- }
- if schema.Type != nil {
- content = appendPair(content, "type", schema.Type.nodeValue())
- }
- if schema.Items != nil {
- content = appendPair(content, "items", schema.Items.nodeValue())
- }
- if schema.Description != nil {
- content = appendPair(content, "description", nodeForString(*schema.Description))
- }
- if schema.Required != nil {
- content = appendPair(content, "required", nodeForStringArray(*schema.Required))
- }
- if schema.AdditionalProperties != nil {
- content = appendPair(content, "additionalProperties", schema.AdditionalProperties.nodeValue())
- }
- if schema.PatternProperties != nil {
- content = appendPair(content, "patternProperties", nodeForNamedSchemaArray(schema.PatternProperties))
- }
- if schema.Properties != nil {
- content = appendPair(content, "properties", nodeForNamedSchemaArray(schema.Properties))
- }
- if schema.Dependencies != nil {
- content = appendPair(content, "dependencies", nodeForNamedSchemaOrStringArray(schema.Dependencies))
- }
- if schema.Ref != nil {
- content = appendPair(content, "$ref", nodeForString(*schema.Ref))
- }
- if schema.MultipleOf != nil {
- content = appendPair(content, "multipleOf", schema.MultipleOf.nodeValue())
- }
- if schema.Maximum != nil {
- content = appendPair(content, "maximum", schema.Maximum.nodeValue())
- }
- if schema.ExclusiveMaximum != nil {
- content = appendPair(content, "exclusiveMaximum", nodeForBoolean(*schema.ExclusiveMaximum))
- }
- if schema.Minimum != nil {
- content = appendPair(content, "minimum", schema.Minimum.nodeValue())
- }
- if schema.ExclusiveMinimum != nil {
- content = appendPair(content, "exclusiveMinimum", nodeForBoolean(*schema.ExclusiveMinimum))
- }
- if schema.MaxLength != nil {
- content = appendPair(content, "maxLength", nodeForInt64(*schema.MaxLength))
- }
- if schema.MinLength != nil {
- content = appendPair(content, "minLength", nodeForInt64(*schema.MinLength))
- }
- if schema.Pattern != nil {
- content = appendPair(content, "pattern", nodeForString(*schema.Pattern))
- }
- if schema.AdditionalItems != nil {
- content = appendPair(content, "additionalItems", schema.AdditionalItems.nodeValue())
- }
- if schema.MaxItems != nil {
- content = appendPair(content, "maxItems", nodeForInt64(*schema.MaxItems))
- }
- if schema.MinItems != nil {
- content = appendPair(content, "minItems", nodeForInt64(*schema.MinItems))
- }
- if schema.UniqueItems != nil {
- content = appendPair(content, "uniqueItems", nodeForBoolean(*schema.UniqueItems))
- }
- if schema.MaxProperties != nil {
- content = appendPair(content, "maxProperties", nodeForInt64(*schema.MaxProperties))
- }
- if schema.MinProperties != nil {
- content = appendPair(content, "minProperties", nodeForInt64(*schema.MinProperties))
- }
- if schema.Enumeration != nil {
- content = appendPair(content, "enum", nodeForSchemaEnumArray(schema.Enumeration))
- }
- if schema.AllOf != nil {
- content = appendPair(content, "allOf", nodeForSchemaArray(*schema.AllOf))
- }
- if schema.AnyOf != nil {
- content = appendPair(content, "anyOf", nodeForSchemaArray(*schema.AnyOf))
- }
- if schema.OneOf != nil {
- content = appendPair(content, "oneOf", nodeForSchemaArray(*schema.OneOf))
- }
- if schema.Not != nil {
- content = appendPair(content, "not", schema.Not.nodeValue())
- }
- if schema.Definitions != nil {
- content = appendPair(content, "definitions", nodeForNamedSchemaArray(schema.Definitions))
- }
- if schema.Default != nil {
- // m = append(m, yaml.MapItem{Key: "default", Value: *schema.Default})
- }
- if schema.Format != nil {
- content = appendPair(content, "format", nodeForString(*schema.Format))
- }
- n.Content = content
- return n
- }
- // JSONString returns a json representation of a schema.
- func (schema *Schema) JSONString() string {
- node := schema.nodeValue()
- return Render(node)
- }
|