Skip to content

Commit

Permalink
compiler: add ResourceExistsError
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed May 23, 2024
1 parent 610eb82 commit a9aea14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
7 changes: 6 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ func (c *Compiler) AddResource(url string, doc any) error {
if err != nil {
return err
}
c.roots.loader.add(uf.url, doc)
if isMeta(string(uf.url)) {
return &ResourceExistsError{string(uf.url)}
}
if !c.roots.loader.add(uf.url, doc) {
return &ResourceExistsError{string(uf.url)}
}
return nil
}

Expand Down
46 changes: 41 additions & 5 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func (l SchemeURLLoader) Load(url string) (any, error) {
//go:embed metaschemas
var metaFS embed.FS

func loadMeta(url string) (any, error) {
func openMeta(url string) (fs.File, error) {
u, meta := strings.CutPrefix(url, "http://json-schema.org/")
if !meta {
u, meta = strings.CutPrefix(url, "https://json-schema.org/")
}
if meta {
if u == "schema" {
return loadMeta(draftLatest.url)
return openMeta(draftLatest.url)
}
f, err := metaFS.Open("metaschemas/" + u)
if err != nil {
Expand All @@ -94,9 +94,34 @@ func loadMeta(url string) (any, error) {
}
return nil, err
}
return UnmarshalJSON(f)
return f, err
}
return nil, nil

}

func isMeta(url string) bool {
f, err := openMeta(url)
if err != nil {
return true
}
if f != nil {
f.Close()
return true
}
return false
}

func loadMeta(url string) (any, error) {
f, err := openMeta(url)
if err != nil {
return nil, err
}
if f == nil {
return nil, nil
}
defer f.Close()
return UnmarshalJSON(f)
}

// --
Expand All @@ -106,11 +131,12 @@ type defaultLoader struct {
loader URLLoader
}

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

func (l *defaultLoader) load(url url) (any, error) {
Expand Down Expand Up @@ -214,6 +240,16 @@ func (e *UnsupportedURLSchemeError) Error() string {

// --

type ResourceExistsError struct {
url string
}

func (e *ResourceExistsError) Error() string {
return fmt.Sprintf("resource for %q already exists", e.url)
}

// --

// UnmarshalJSON unmarshals into [any] without losing
// number precision using [json.Number].
func UnmarshalJSON(r io.Reader) (any, error) {
Expand Down

0 comments on commit a9aea14

Please sign in to comment.