Skip to content

Commit

Permalink
refactor: move collectResource to roots
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed May 6, 2024
1 parent 8f0bca5 commit b723bca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions draft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ func TestDraft_collectIds(t *testing.T) {
"/definitions/s4": "http://e.com/def", // id with fragments
}

rr := newRoots()
r := root{
url: url(u),
doc: doc,
resources: map[jsonPointer]*resource{},
subschemasProcessed: map[jsonPointer]struct{}{},
}
if err := r.collectResources(nil, false, nil, doc, u, jsonPointer(""), dialect{Draft4, nil}); err != nil {
if err := rr.collectResources(&r, doc, u, jsonPointer(""), dialect{Draft4, nil}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -116,13 +117,14 @@ func TestDraft_collectAnchors(t *testing.T) {
t.Fatal(err)
}

rr := newRoots()
r := root{
url: url(u),
doc: doc,
resources: map[jsonPointer]*resource{},
subschemasProcessed: map[jsonPointer]struct{}{},
}
if err := r.collectResources(nil, false, nil, doc, u, jsonPointer(""), dialect{Draft2020, nil}); err != nil {
if err := rr.collectResources(&r, doc, u, jsonPointer(""), dialect{Draft2020, nil}); err != nil {
t.Fatal(err)
}

Expand Down
20 changes: 10 additions & 10 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ func (r *root) resolve(uf urlFrag) (*urlPtr, error) {
return &up, err
}

func (r *root) collectResources(loader *defaultLoader, assertVocabs bool, vocabularies map[string]*Vocabulary, sch any, base url, schPtr jsonPointer, fallback dialect) error {
func (rr *roots) collectResources(r *root, sch any, base url, schPtr jsonPointer, fallback dialect) error {
if _, ok := r.subschemasProcessed[schPtr]; ok {
return nil
}
if err := r._collectResources(loader, assertVocabs, vocabularies, sch, base, schPtr, fallback); err != nil {
if err := rr._collectResources(r, sch, base, schPtr, fallback); err != nil {
return err
}
r.subschemasProcessed[schPtr] = struct{}{}
return nil
}

func (r *root) _collectResources(loader *defaultLoader, assertVocabs bool, vocabularies map[string]*Vocabulary, sch any, base url, schPtr jsonPointer, fallback dialect) error {
func (rr *roots) _collectResources(r *root, sch any, base url, schPtr jsonPointer, fallback dialect) error {
if _, ok := sch.(bool); ok {
if schPtr.isEmpty() {
// root resource
Expand All @@ -112,7 +112,7 @@ func (r *root) _collectResources(loader *defaultLoader, assertVocabs bool, vocab
}
}

draft, err := loader.getDraft(urlPtr{r.url, schPtr}, sch, fallback.draft, map[url]struct{}{})
draft, err := rr.loader.getDraft(urlPtr{r.url, schPtr}, sch, fallback.draft, map[url]struct{}{})
if err != nil {
return err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (r *root) _collectResources(loader *defaultLoader, assertVocabs bool, vocab
}
if !found {
if hasSchema {
vocabs, err := loader.getMetaVocabs(sch, draft, vocabularies)
vocabs, err := rr.loader.getMetaVocabs(sch, draft, rr.vocabularies)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,28 +181,28 @@ func (r *root) _collectResources(loader *defaultLoader, assertVocabs bool, vocab
// process subschemas
subschemas := map[jsonPointer]any{}
draft.subschemas.collect(obj, schPtr, subschemas)
for _, vocab := range baseRes.dialect.activeVocabs(true, vocabularies) { // TODO: take assertVcabs flag argument
if v := vocabularies[vocab]; v != nil {
for _, vocab := range baseRes.dialect.activeVocabs(true, rr.vocabularies) { // TODO: take assertVcabs flag argument
if v := rr.vocabularies[vocab]; v != nil {
v.Subschemas.collect(obj, schPtr, subschemas)
}
}
for ptr, v := range subschemas {
if err := r.collectResources(loader, assertVocabs, vocabularies, v, base, ptr, baseRes.dialect); err != nil {
if err := rr.collectResources(r, v, base, ptr, baseRes.dialect); err != nil {
return err
}
}

return nil
}

func (r *root) addSubschema(loader *defaultLoader, assertVocabs bool, vocabularies map[string]*Vocabulary, ptr jsonPointer) error {
func (rr *roots) addSubschema(r *root, ptr jsonPointer) error {
v, err := (&urlPtr{r.url, ptr}).lookup(r.doc)
if err != nil {
return err
}
base := r.resource(ptr)
baseURL := base.id
if err := r.collectResources(loader, assertVocabs, vocabularies, v, baseURL, ptr, base.dialect); err != nil {
if err := rr.collectResources(r, v, baseURL, ptr, base.dialect); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions roots.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (rr *roots) addRoot(u url, doc any) (*root, error) {
resources: map[jsonPointer]*resource{},
subschemasProcessed: map[jsonPointer]struct{}{},
}
if err := r.collectResources(&rr.loader, rr.assertVocabs, rr.vocabularies, doc, u, "", dialect{rr.defaultDraft, nil}); err != nil {
if err := rr.collectResources(r, doc, u, "", dialect{rr.defaultDraft, nil}); err != nil {
return nil, err
}
if !strings.HasPrefix(u.String(), "http://json-schema.org/") &&
Expand Down Expand Up @@ -80,7 +80,7 @@ func (rr *roots) ensureSubschema(up urlPtr) error {
return err
}
rClone := r.clone()
if err := rClone.addSubschema(&rr.loader, rr.assertVocabs, rr.vocabularies, up.ptr); err != nil {
if err := rr.addSubschema(rClone, up.ptr); err != nil {
return err
}
if err := rr.validate(rClone, v, up.ptr); err != nil {
Expand Down

0 comments on commit b723bca

Please sign in to comment.