diff options
Diffstat (limited to 'vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go')
-rw-r--r-- | vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go b/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go index a77a81e40..cfa5f6a3a 100644 --- a/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go +++ b/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go @@ -38,7 +38,6 @@ import ( "runtime" "strings" - "github.com/xeipuuv/gojsonreference" ) @@ -108,7 +107,7 @@ func (l *jsonReferenceLoader) LoaderFactory() JSONLoaderFactory { } // NewReferenceLoader returns a JSON reference loader using the given source and the local OS file system. -func NewReferenceLoader(source string) *jsonReferenceLoader { +func NewReferenceLoader(source string) JSONLoader { return &jsonReferenceLoader{ fs: osFS, source: source, @@ -116,7 +115,7 @@ func NewReferenceLoader(source string) *jsonReferenceLoader { } // NewReferenceLoaderFileSystem returns a JSON reference loader using the given source and file system. -func NewReferenceLoaderFileSystem(source string, fs http.FileSystem) *jsonReferenceLoader { +func NewReferenceLoaderFileSystem(source string, fs http.FileSystem) JSONLoader { return &jsonReferenceLoader{ fs: fs, source: source, @@ -139,13 +138,11 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) { if reference.HasFileScheme { - filename := strings.Replace(refToUrl.GetUrl().Path, "file://", "", -1) + filename := strings.TrimPrefix(refToUrl.String(), "file://") if runtime.GOOS == "windows" { // on Windows, a file URL may have an extra leading slash, use slashes // instead of backslashes, and have spaces escaped - if strings.HasPrefix(filename, "/") { - filename = filename[1:] - } + filename = strings.TrimPrefix(filename, "/") filename = filepath.FromSlash(filename) } @@ -169,6 +166,12 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) { func (l *jsonReferenceLoader) loadFromHTTP(address string) (interface{}, error) { + // returned cached versions for metaschemas for drafts 4, 6 and 7 + // for performance and allow for easier offline use + if metaSchema := drafts.GetMetaSchema(address); metaSchema != "" { + return decodeJsonUsingNumber(strings.NewReader(metaSchema)) + } + resp, err := http.Get(address) if err != nil { return nil, err @@ -185,7 +188,6 @@ func (l *jsonReferenceLoader) loadFromHTTP(address string) (interface{}, error) } return decodeJsonUsingNumber(bytes.NewReader(bodyBuff)) - } func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) { @@ -222,7 +224,7 @@ func (l *jsonStringLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } -func NewStringLoader(source string) *jsonStringLoader { +func NewStringLoader(source string) JSONLoader { return &jsonStringLoader{source: source} } @@ -250,7 +252,7 @@ func (l *jsonBytesLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } -func NewBytesLoader(source []byte) *jsonBytesLoader { +func NewBytesLoader(source []byte) JSONLoader { return &jsonBytesLoader{source: source} } @@ -277,7 +279,7 @@ func (l *jsonGoLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } -func NewGoLoader(source interface{}) *jsonGoLoader { +func NewGoLoader(source interface{}) JSONLoader { return &jsonGoLoader{source: source} } @@ -298,12 +300,12 @@ type jsonIOLoader struct { buf *bytes.Buffer } -func NewReaderLoader(source io.Reader) (*jsonIOLoader, io.Reader) { +func NewReaderLoader(source io.Reader) (JSONLoader, io.Reader) { buf := &bytes.Buffer{} return &jsonIOLoader{buf: buf}, io.TeeReader(source, buf) } -func NewWriterLoader(source io.Writer) (*jsonIOLoader, io.Writer) { +func NewWriterLoader(source io.Writer) (JSONLoader, io.Writer) { buf := &bytes.Buffer{} return &jsonIOLoader{buf: buf}, io.MultiWriter(source, buf) } @@ -324,6 +326,30 @@ func (l *jsonIOLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } +// JSON raw loader +// In case the JSON is already marshalled to interface{} use this loader +// This is used for testing as otherwise there is no guarantee the JSON is marshalled +// "properly" by using https://golang.org/pkg/encoding/json/#Decoder.UseNumber +type jsonRawLoader struct { + source interface{} +} + +func NewRawLoader(source interface{}) *jsonRawLoader { + return &jsonRawLoader{source: source} +} +func (l *jsonRawLoader) JsonSource() interface{} { + return l.source +} +func (l *jsonRawLoader) LoadJSON() (interface{}, error) { + return l.source, nil +} +func (l *jsonRawLoader) JsonReference() (gojsonreference.JsonReference, error) { + return gojsonreference.NewJsonReference("#") +} +func (l *jsonRawLoader) LoaderFactory() JSONLoaderFactory { + return &DefaultJSONLoaderFactory{} +} + func decodeJsonUsingNumber(r io.Reader) (interface{}, error) { var document interface{} @@ -335,7 +361,7 @@ func decodeJsonUsingNumber(r io.Reader) (interface{}, error) { if err != nil { return nil, err } - + return document, nil } |