diff options
Diffstat (limited to 'vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go')
-rw-r--r-- | vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go b/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go index cfa5f6a3a..4f57ff789 100644 --- a/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go +++ b/vendor/github.com/xeipuuv/gojsonschema/jsonLoader.go @@ -43,8 +43,7 @@ import ( var osFS = osFileSystem(os.Open) -// JSON loader interface - +// JSONLoader defines the JSON loader interface type JSONLoader interface { JsonSource() interface{} LoadJSON() (interface{}, error) @@ -52,17 +51,22 @@ type JSONLoader interface { LoaderFactory() JSONLoaderFactory } +// JSONLoaderFactory defines the JSON loader factory interface type JSONLoaderFactory interface { + // New creates a new JSON loader for the given source New(source string) JSONLoader } +// DefaultJSONLoaderFactory is the default JSON loader factory type DefaultJSONLoaderFactory struct { } +// FileSystemJSONLoaderFactory is a JSON loader factory that uses http.FileSystem type FileSystemJSONLoaderFactory struct { fs http.FileSystem } +// New creates a new JSON loader for the given source func (d DefaultJSONLoaderFactory) New(source string) JSONLoader { return &jsonReferenceLoader{ fs: osFS, @@ -70,6 +74,7 @@ func (d DefaultJSONLoaderFactory) New(source string) JSONLoader { } } +// New creates a new JSON loader for the given source func (f FileSystemJSONLoaderFactory) New(source string) JSONLoader { return &jsonReferenceLoader{ fs: f.fs, @@ -80,6 +85,7 @@ func (f FileSystemJSONLoaderFactory) New(source string) JSONLoader { // osFileSystem is a functional wrapper for os.Open that implements http.FileSystem. type osFileSystem func(string) (*os.File, error) +// Opens a file with the given name func (o osFileSystem) Open(name string) (http.File, error) { return o(name) } @@ -131,14 +137,14 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) { return nil, err } - refToUrl := reference - refToUrl.GetUrl().Fragment = "" + refToURL := reference + refToURL.GetUrl().Fragment = "" var document interface{} if reference.HasFileScheme { - filename := strings.TrimPrefix(refToUrl.String(), "file://") + 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 @@ -153,7 +159,7 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) { } else { - document, err = l.loadFromHTTP(refToUrl.String()) + document, err = l.loadFromHTTP(refToURL.String()) if err != nil { return nil, err } @@ -169,7 +175,7 @@ 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)) + return decodeJSONUsingNumber(strings.NewReader(metaSchema)) } resp, err := http.Get(address) @@ -187,7 +193,7 @@ func (l *jsonReferenceLoader) loadFromHTTP(address string) (interface{}, error) return nil, err } - return decodeJsonUsingNumber(bytes.NewReader(bodyBuff)) + return decodeJSONUsingNumber(bytes.NewReader(bodyBuff)) } func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) { @@ -202,7 +208,7 @@ func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) { return nil, err } - return decodeJsonUsingNumber(bytes.NewReader(bodyBuff)) + return decodeJSONUsingNumber(bytes.NewReader(bodyBuff)) } @@ -224,13 +230,14 @@ func (l *jsonStringLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } +// NewStringLoader creates a new JSONLoader, taking a string as source func NewStringLoader(source string) JSONLoader { return &jsonStringLoader{source: source} } func (l *jsonStringLoader) LoadJSON() (interface{}, error) { - return decodeJsonUsingNumber(strings.NewReader(l.JsonSource().(string))) + return decodeJSONUsingNumber(strings.NewReader(l.JsonSource().(string))) } @@ -252,12 +259,13 @@ func (l *jsonBytesLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } +// NewBytesLoader creates a new JSONLoader, taking a `[]byte` as source func NewBytesLoader(source []byte) JSONLoader { return &jsonBytesLoader{source: source} } func (l *jsonBytesLoader) LoadJSON() (interface{}, error) { - return decodeJsonUsingNumber(bytes.NewReader(l.JsonSource().([]byte))) + return decodeJSONUsingNumber(bytes.NewReader(l.JsonSource().([]byte))) } // JSON Go (types) loader @@ -279,6 +287,7 @@ func (l *jsonGoLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } +// NewGoLoader creates a new JSONLoader from a given Go struct func NewGoLoader(source interface{}) JSONLoader { return &jsonGoLoader{source: source} } @@ -292,7 +301,7 @@ func (l *jsonGoLoader) LoadJSON() (interface{}, error) { return nil, err } - return decodeJsonUsingNumber(bytes.NewReader(jsonBytes)) + return decodeJSONUsingNumber(bytes.NewReader(jsonBytes)) } @@ -300,11 +309,13 @@ type jsonIOLoader struct { buf *bytes.Buffer } +// NewReaderLoader creates a new JSON loader using the provided io.Reader func NewReaderLoader(source io.Reader) (JSONLoader, io.Reader) { buf := &bytes.Buffer{} return &jsonIOLoader{buf: buf}, io.TeeReader(source, buf) } +// NewWriterLoader creates a new JSON loader using the provided io.Writer func NewWriterLoader(source io.Writer) (JSONLoader, io.Writer) { buf := &bytes.Buffer{} return &jsonIOLoader{buf: buf}, io.MultiWriter(source, buf) @@ -315,7 +326,7 @@ func (l *jsonIOLoader) JsonSource() interface{} { } func (l *jsonIOLoader) LoadJSON() (interface{}, error) { - return decodeJsonUsingNumber(l.buf) + return decodeJSONUsingNumber(l.buf) } func (l *jsonIOLoader) JsonReference() (gojsonreference.JsonReference, error) { @@ -334,7 +345,8 @@ type jsonRawLoader struct { source interface{} } -func NewRawLoader(source interface{}) *jsonRawLoader { +// NewRawLoader creates a new JSON raw loader for the given source +func NewRawLoader(source interface{}) JSONLoader { return &jsonRawLoader{source: source} } func (l *jsonRawLoader) JsonSource() interface{} { @@ -350,7 +362,7 @@ func (l *jsonRawLoader) LoaderFactory() JSONLoaderFactory { return &DefaultJSONLoaderFactory{} } -func decodeJsonUsingNumber(r io.Reader) (interface{}, error) { +func decodeJSONUsingNumber(r io.Reader) (interface{}, error) { var document interface{} |