From 417d87020354f57cfa8edad74418dbe62c53d495 Mon Sep 17 00:00:00 2001 From: sigu-399 Date: Tue, 22 Sep 2015 12:59:07 +0800 Subject: [PATCH] Wraps internalLog calls with internalLogEnabled --- internalLog.go | 6 +---- jsonLoader.go | 54 ++++++++++++++++-------------------------- schemaPool.go | 8 +++++-- schemaReferencePool.go | 13 +++++++--- validation.go | 50 ++++++++++++++++++++++++-------------- 5 files changed, 69 insertions(+), 62 deletions(-) diff --git a/internalLog.go b/internalLog.go index 2459fef..4ef7a8d 100644 --- a/internalLog.go +++ b/internalLog.go @@ -33,9 +33,5 @@ import ( const internalLogEnabled = false func internalLog(format string, v ...interface{}) { - - if internalLogEnabled { - log.Printf(format, v...) - } - + log.Printf(format, v...) } diff --git a/jsonLoader.go b/jsonLoader.go index 81ca1a3..8cd4e02 100644 --- a/jsonLoader.go +++ b/jsonLoader.go @@ -30,6 +30,7 @@ import ( "bytes" "encoding/json" "errors" + "io" "io/ioutil" "net/http" "path/filepath" @@ -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) { @@ -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 @@ -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))) } @@ -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)) } @@ -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 + +} diff --git a/schemaPool.go b/schemaPool.go index 150fc6b..79fbb60 100644 --- a/schemaPool.go +++ b/schemaPool.go @@ -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 @@ -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 } diff --git a/schemaReferencePool.go b/schemaReferencePool.go index 83ee7c9..294e36a 100644 --- a/schemaReferencePool.go +++ b/schemaReferencePool.go @@ -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 } @@ -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 } diff --git a/validation.go b/validation.go index 67a1a37..2880d83 100644 --- a/validation.go +++ b/validation.go @@ -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 { @@ -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 { @@ -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 { @@ -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) @@ -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 { @@ -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 @@ -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 @@ -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: @@ -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()