Skip to content

Commit

Permalink
introduce defaultLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 24, 2024
1 parent a0f8863 commit b682da7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
4 changes: 2 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func (c *Compiler) AddResource(url string, doc any) error {
if err != nil {
return err
}
c.roots.userResources[uf.url] = doc
c.roots.loader.add(uf.url, doc)
return nil
}

// UseLoader overrides the default [URLLoader] used
// to load schema resources.
func (c *Compiler) UseLoader(loader URLLoader) {
c.roots.loader = loader
c.roots.loader.loader = loader
}

// UseRegexpEngine changes the regexp-engine used.
Expand Down
37 changes: 37 additions & 0 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ func loadMeta(url string) (any, error) {

// --

type defaultLoader struct {
docs map[url]any // docs loaded so far
loader URLLoader
}

func (l *defaultLoader) add(url url, doc any) {
if _, ok := l.docs[url]; ok {
return
}
l.docs[url] = doc
}

func (l *defaultLoader) load(url url) (any, error) {
if doc, ok := l.docs[url]; ok {
return doc, nil
}
doc, err := loadMeta(url.String())
if err != nil {
return nil, err
}
if doc != nil {
l.add(url, doc)
return doc, nil
}
if l.loader == nil {
return nil, &LoadURLError{url.String(), errors.New("no URLLoader set")}
}
doc, err = l.loader.Load(url.String())
if err != nil {
return nil, &LoadURLError{URL: url.String(), Err: err}
}
l.add(url, doc)
return doc, nil
}

// --

type LoadURLError struct {
URL string
Err error
Expand Down
47 changes: 15 additions & 32 deletions roots.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,31 @@ import (
)

type roots struct {
defaultDraft *Draft
roots map[url]*root
userResources map[url]any
loader URLLoader
regexpEngine RegexpEngine
vocabularies map[string]*Vocabulary
defaultDraft *Draft
roots map[url]*root
loader defaultLoader
regexpEngine RegexpEngine
vocabularies map[string]*Vocabulary
}

func newRoots() *roots {
return &roots{
defaultDraft: draftLatest,
roots: map[url]*root{},
userResources: map[url]any{},
loader: FileLoader{},
regexpEngine: goRegexpCompile,
vocabularies: map[string]*Vocabulary{},
defaultDraft: draftLatest,
roots: map[url]*root{},
loader: defaultLoader{
docs: map[url]any{},
loader: FileLoader{},
},
regexpEngine: goRegexpCompile,
vocabularies: map[string]*Vocabulary{},
}
}

func (rr *roots) loadURL(url url) (any, error) {
v, err := loadMeta(url.String())
if err != nil {
return nil, err
}
if v != nil {
return v, nil
}
if v, ok := rr.userResources[url]; ok {
return v, nil
}
v, err = rr.loader.Load(url.String())
if err != nil {
return nil, &LoadURLError{URL: url.String(), Err: err}
}
return v, nil
}

func (rr *roots) orLoad(u url) (*root, error) {
if r, ok := rr.roots[u]; ok {
return r, nil
}
doc, err := rr.loadURL(u)
doc, err := rr.loader.load(u)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,7 +66,7 @@ func (rr *roots) getMeta(up urlPtr, doc any, cycle map[url]struct{}) (meta, erro
return meta{}, &MetaSchemaCycleError{schUrl.String()}
}
cycle[schUrl] = struct{}{}
doc, err := rr.loadURL(schUrl)
doc, err := rr.loader.load(schUrl)
if err != nil {
return meta{}, err
}
Expand Down

0 comments on commit b682da7

Please sign in to comment.