...

Source file src/github.com/xeipuuv/gojsonschema/jsonContext.go

Documentation: github.com/xeipuuv/gojsonschema

     1  // Copyright 2013 MongoDB, Inc.
     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  // author           tolsen
    16  // author-github    https://github.com/tolsen
    17  //
    18  // repository-name  gojsonschema
    19  // repository-desc  An implementation of JSON Schema, based on IETF's draft v4 - Go language.
    20  //
    21  // description      Implements a persistent (immutable w/ shared structure) singly-linked list of strings for the purpose of storing a json context
    22  //
    23  // created          04-09-2013
    24  
    25  package gojsonschema
    26  
    27  import "bytes"
    28  
    29  // JsonContext implements a persistent linked-list of strings
    30  type JsonContext struct {
    31  	head string
    32  	tail *JsonContext
    33  }
    34  
    35  // NewJsonContext creates a new JsonContext
    36  func NewJsonContext(head string, tail *JsonContext) *JsonContext {
    37  	return &JsonContext{head, tail}
    38  }
    39  
    40  // String displays the context in reverse.
    41  // This plays well with the data structure's persistent nature with
    42  // Cons and a json document's tree structure.
    43  func (c *JsonContext) String(del ...string) string {
    44  	byteArr := make([]byte, 0, c.stringLen())
    45  	buf := bytes.NewBuffer(byteArr)
    46  	c.writeStringToBuffer(buf, del)
    47  
    48  	return buf.String()
    49  }
    50  
    51  func (c *JsonContext) stringLen() int {
    52  	length := 0
    53  	if c.tail != nil {
    54  		length = c.tail.stringLen() + 1 // add 1 for "."
    55  	}
    56  
    57  	length += len(c.head)
    58  	return length
    59  }
    60  
    61  func (c *JsonContext) writeStringToBuffer(buf *bytes.Buffer, del []string) {
    62  	if c.tail != nil {
    63  		c.tail.writeStringToBuffer(buf, del)
    64  
    65  		if len(del) > 0 {
    66  			buf.WriteString(del[0])
    67  		} else {
    68  			buf.WriteString(".")
    69  		}
    70  	}
    71  
    72  	buf.WriteString(c.head)
    73  }
    74  

View as plain text