d1353e1f7c
* update code.gitea.io/sdk/gitea v0.13.1 -> v0.13.2 * update github.com/go-swagger/go-swagger v0.25.0 -> v0.26.0 * update github.com/google/uuid v1.1.2 -> v1.2.0 * update github.com/klauspost/compress v1.11.3 -> v1.11.7 * update github.com/lib/pq 083382b7e6fc -> v1.9.0 * update github.com/markbates/goth v1.65.0 -> v1.66.1 * update github.com/mattn/go-sqlite3 v1.14.4 -> v1.14.6 * update github.com/mgechev/revive 246eac737dc7 -> v1.0.3 * update github.com/minio/minio-go/v7 v7.0.6 -> v7.0.7 * update github.com/niklasfasching/go-org v1.3.2 -> v1.4.0 * update github.com/olivere/elastic/v7 v7.0.21 -> v7.0.22 * update github.com/pquerna/otp v1.2.0 -> v1.3.0 * update github.com/xanzy/go-gitlab v0.39.0 -> v0.42.0 * update github.com/yuin/goldmark v1.2.1 -> v1.3.1
138 lines
3.1 KiB
Go
Vendored
138 lines
3.1 KiB
Go
Vendored
package loads
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
|
|
"github.com/go-openapi/spec"
|
|
"github.com/go-openapi/swag"
|
|
)
|
|
|
|
var (
|
|
// Default chain of loaders, defined at the package level.
|
|
//
|
|
// By default this matches json and yaml documents.
|
|
//
|
|
// May be altered with AddLoader().
|
|
loaders *loader
|
|
|
|
// Default loader. This expects json documents over local or http/https (no matching).
|
|
defaultLoader *loader
|
|
|
|
defaultMatcher DocMatcher
|
|
)
|
|
|
|
func init() {
|
|
defaultMatcher = func(_ string) bool { return true }
|
|
|
|
defaultLoader = &loader{
|
|
DocLoaderWithMatch: DocLoaderWithMatch{
|
|
Match: defaultMatcher,
|
|
Fn: JSONDoc,
|
|
},
|
|
}
|
|
loaders = defaultLoader
|
|
|
|
AddLoader(swag.YAMLMatcher, swag.YAMLDoc)
|
|
|
|
// sets the global default loader for go-openapi/spec
|
|
spec.PathLoader = loaders.Load
|
|
}
|
|
|
|
// DocLoader represents a doc loader type
|
|
type DocLoader func(string) (json.RawMessage, error)
|
|
|
|
// DocMatcher represents a predicate to check if a loader matches
|
|
type DocMatcher func(string) bool
|
|
|
|
// DocLoaderWithMatch describes a loading function for a given extension match.
|
|
type DocLoaderWithMatch struct {
|
|
Fn DocLoader
|
|
Match DocMatcher
|
|
}
|
|
|
|
// NewDocLoaderWithMatch builds a DocLoaderWithMatch to be used in load options
|
|
func NewDocLoaderWithMatch(fn DocLoader, matcher DocMatcher) DocLoaderWithMatch {
|
|
return DocLoaderWithMatch{
|
|
Fn: fn,
|
|
Match: matcher,
|
|
}
|
|
}
|
|
|
|
type loader struct {
|
|
DocLoaderWithMatch
|
|
Next *loader
|
|
}
|
|
|
|
// WithHead adds a loader at the head of the current stack
|
|
func (l *loader) WithHead(head *loader) *loader {
|
|
if head == nil {
|
|
return l
|
|
}
|
|
head.Next = l
|
|
return head
|
|
}
|
|
|
|
// WithNext adds a loader at the trail of the current stack
|
|
func (l *loader) WithNext(next *loader) *loader {
|
|
l.Next = next
|
|
return next
|
|
}
|
|
|
|
// Load the raw document from path
|
|
func (l *loader) Load(path string) (json.RawMessage, error) {
|
|
specURL, erp := url.Parse(path)
|
|
if erp != nil {
|
|
return nil, erp
|
|
}
|
|
|
|
var lastErr error = errors.New("no loader matched") // default error if no match was found
|
|
for ldr := l; ldr != nil; ldr = ldr.Next {
|
|
if ldr.Match != nil && !ldr.Match(specURL.Path) {
|
|
continue
|
|
}
|
|
|
|
// try then move to next one if there is an error
|
|
b, err := ldr.Fn(path)
|
|
if err == nil {
|
|
return b, nil
|
|
}
|
|
|
|
lastErr = err
|
|
}
|
|
|
|
return nil, lastErr
|
|
}
|
|
|
|
// JSONDoc loads a json document from either a file or a remote url
|
|
func JSONDoc(path string) (json.RawMessage, error) {
|
|
data, err := swag.LoadFromFileOrHTTP(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.RawMessage(data), nil
|
|
}
|
|
|
|
// AddLoader for a document, executed before other previously set loaders.
|
|
//
|
|
// This sets the configuration at the package level.
|
|
//
|
|
// The default initial loader at the package level assumes a JSON document.
|
|
//
|
|
// NOTE:
|
|
// * this updates the default loader used by github.com/go-openapi/spec
|
|
// * since this sets package level globals, you shouln't call this concurrently
|
|
//
|
|
func AddLoader(predicate DocMatcher, load DocLoader) {
|
|
loaders = loaders.WithHead(&loader{
|
|
DocLoaderWithMatch: DocLoaderWithMatch{
|
|
Match: predicate,
|
|
Fn: load,
|
|
},
|
|
})
|
|
|
|
// sets the global default loader for go-openapi/spec
|
|
spec.PathLoader = loaders.Load
|
|
}
|