Skip to content

Commit

Permalink
Wraps internalLog calls with internalLogEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sigu-399 committed Sep 22, 2015
1 parent ce63822 commit 417d870
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 62 deletions.
6 changes: 1 addition & 5 deletions internalLog.go
Expand Up @@ -33,9 +33,5 @@ import (
const internalLogEnabled = false

func internalLog(format string, v ...interface{}) {

if internalLogEnabled {
log.Printf(format, v...)
}

log.Printf(format, v...)
}
54 changes: 20 additions & 34 deletions jsonLoader.go
Expand Up @@ -30,6 +30,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -150,15 +151,8 @@ func (l *jsonReferenceLoader) loadFromHTTP(address string) (interface{}, error)
return nil, err
}

var document interface{}
decoder := json.NewDecoder(bytes.NewReader(bodyBuff))
decoder.UseNumber()
err = decoder.Decode(&document)
if err != nil {
return nil, err
}
return decodeJsonUsingNumber(bytes.NewReader(bodyBuff))

return document, nil
}

func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) {
Expand All @@ -168,15 +162,8 @@ func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) {
return nil, err
}

var document interface{}
decoder := json.NewDecoder(bytes.NewReader(bodyBuff))
decoder.UseNumber()
err = decoder.Decode(&document)
if err != nil {
return nil, err
}
return decodeJsonUsingNumber(bytes.NewReader(bodyBuff))

return document, nil
}

// JSON string loader
Expand All @@ -195,15 +182,7 @@ func NewStringLoader(source string) *jsonStringLoader {

func (l *jsonStringLoader) loadJSON() (interface{}, error) {

var document interface{}
decoder := json.NewDecoder(strings.NewReader(l.jsonSource().(string)))
decoder.UseNumber()
err := decoder.Decode(&document)
if err != nil {
return nil, err
}

return document, nil
return decodeJsonUsingNumber(strings.NewReader(l.jsonSource().(string)))

}

Expand Down Expand Up @@ -258,15 +237,7 @@ func (l *jsonGoLoader) loadJSON() (interface{}, error) {
return nil, err
}

var document interface{}
decoder := json.NewDecoder(bytes.NewReader(jsonBytes))
decoder.UseNumber()
err = decoder.Decode(&document)
if err != nil {
return nil, err
}

return document, nil
return decodeJsonUsingNumber(bytes.NewReader(jsonBytes))

}

Expand Down Expand Up @@ -296,3 +267,18 @@ func (l *jsonGoLoader) loadSchema() (*Schema, error) {
return &d, nil

}

func decodeJsonUsingNumber(r io.Reader) (interface{}, error) {

var document interface{}

decoder := json.NewDecoder(r)
decoder.UseNumber()
err := decoder.Decode(&document)
if err != nil {
return nil, err
}

return document, nil

}
8 changes: 6 additions & 2 deletions schemaPool.go
Expand Up @@ -60,7 +60,9 @@ func (p *schemaPool) GetStandaloneDocument() (document interface{}) {

func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*schemaPoolDocument, error) {

internalLog("Get Document ( %s )", reference.String())
if internalLogEnabled {
internalLog("Get Document ( %s )", reference.String())
}

var err error

Expand All @@ -85,7 +87,9 @@ func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*sche
}

if spd != nil {
internalLog(" From pool")
if internalLogEnabled {
internalLog(" From pool")
}
return spd, nil
}

Expand Down
13 changes: 10 additions & 3 deletions schemaReferencePool.go
Expand Up @@ -43,10 +43,14 @@ func newSchemaReferencePool() *schemaReferencePool {

func (p *schemaReferencePool) Get(ref string) (r *subSchema, o bool) {

internalLog(fmt.Sprintf("Schema Reference ( %s )", ref))
if internalLogEnabled {
internalLog(fmt.Sprintf("Schema Reference ( %s )", ref))
}

if sch, ok := p.documents[ref]; ok {
internalLog(fmt.Sprintf(" From pool"))
if internalLogEnabled {
internalLog(fmt.Sprintf(" From pool"))
}
return sch, true
}

Expand All @@ -55,6 +59,9 @@ func (p *schemaReferencePool) Get(ref string) (r *subSchema, o bool) {

func (p *schemaReferencePool) Add(ref string, sch *subSchema) {

internalLog(fmt.Sprintf("Add Schema Reference %s to pool", ref))
if internalLogEnabled {
internalLog(fmt.Sprintf("Add Schema Reference %s to pool", ref))
}

p.documents[ref] = sch
}
50 changes: 32 additions & 18 deletions validation.go
Expand Up @@ -79,8 +79,10 @@ func (v *subSchema) subValidateWithContext(document interface{}, context *jsonCo
// Walker function to validate the json recursively against the subSchema
func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {

internalLog("validateRecursive %s", context.String())
internalLog(" %v", currentNode)
if internalLogEnabled {
internalLog("validateRecursive %s", context.String())
internalLog(" %v", currentNode)
}

// Handle referenced schemas, returns directly when a $ref is found
if currentSubSchema.refSchema != nil {
Expand Down Expand Up @@ -257,8 +259,10 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i
// Different kinds of validation there, subSchema / common / array / object / string...
func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {

internalLog("validateSchema %s", context.String())
internalLog(" %v", currentNode)
if internalLogEnabled {
internalLog("validateSchema %s", context.String())
internalLog(" %v", currentNode)
}

if len(currentSubSchema.anyOf) > 0 {

Expand Down Expand Up @@ -369,8 +373,10 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte

func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {

internalLog("validateCommon %s", context.String())
internalLog(" %v", value)
if internalLogEnabled {
internalLog("validateCommon %s", context.String())
internalLog(" %v", value)
}

// enum:
if len(currentSubSchema.enum) > 0 {
Expand All @@ -395,8 +401,10 @@ func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{

func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *jsonContext) {

internalLog("validateArray %s", context.String())
internalLog(" %v", value)
if internalLogEnabled {
internalLog("validateArray %s", context.String())
internalLog(" %v", value)
}

nbItems := len(value)

Expand Down Expand Up @@ -484,8 +492,10 @@ func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface

func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *jsonContext) {

internalLog("validateObject %s", context.String())
internalLog(" %v", value)
if internalLogEnabled {
internalLog("validateObject %s", context.String())
internalLog(" %v", value)
}

// minProperties & maxProperties:
if currentSubSchema.minProperties != nil {
Expand Down Expand Up @@ -628,8 +638,10 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string

func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *jsonContext) (has bool, matched bool) {

internalLog("validatePatternProperty %s", context.String())
internalLog(" %s %v", key, value)
if internalLogEnabled {
internalLog("validatePatternProperty %s", context.String())
internalLog(" %s %v", key, value)
}

has = false

Expand Down Expand Up @@ -658,9 +670,6 @@ func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key str

func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {

internalLog("validateString %s", context.String())
internalLog(" %v", value)

// Ignore JSON numbers
if isJsonNumber(value) {
return
Expand All @@ -671,6 +680,9 @@ func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{
return
}

internalLog("validateString %s", context.String())
internalLog(" %v", value)

stringValue := value.(string)

// minLength & maxLength:
Expand Down Expand Up @@ -725,14 +737,16 @@ func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{

func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {

internalLog("validateNumber %s", context.String())
internalLog(" %v", value)

// Ignore non numbers
if !isJsonNumber(value) {
return
}

if internalLogEnabled {
internalLog("validateNumber %s", context.String())
internalLog(" %v", value)
}

number := value.(json.Number)
float64Value, _ := number.Float64()

Expand Down

0 comments on commit 417d870

Please sign in to comment.