Skip to content

Commit

Permalink
compiler: add resource from json value
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuk authored Oct 18, 2023
1 parent ce3bd6e commit a8fd7ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 10 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@ func NewCompiler() *Compiler {
//
// Note that url must not have fragment
func (c *Compiler) AddResource(url string, r io.Reader) error {
res, err := newResource(url, r)
doc, err := unmarshal(r)
if err != nil {
return fmt.Errorf("jsonschema: invalid json %s: %v", url, err)
}
return c.AddResourceJSON(url, doc)
}

// AddResourceJSON adds in-memory resource from given json value.
func (c *Compiler) AddResourceJSON(url string, doc interface{}) error {
res, err := newResource(url, doc)
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ func (r *resource) String() string {
return r.url + r.floc
}

func newResource(url string, r io.Reader) (*resource, error) {
func newResource(url string, doc interface{}) (*resource, error) {
var err error
if strings.IndexByte(url, '#') != -1 {
panic(fmt.Sprintf("BUG: newResource(%q)", url))
}
doc, err := unmarshal(r)
if err != nil {
return nil, fmt.Errorf("jsonschema: invalid json %s: %v", url, err)
}
url, err = toAbs(url)
if err != nil {
return nil, err
Expand Down

0 comments on commit a8fd7ed

Please sign in to comment.