Skip to content

Commit

Permalink
expose ParseQuery to parse query struct (#17)
Browse files Browse the repository at this point in the history
* add ParseQuery to allow web frameworks to parse the initial json contents

* add extra recover func

* Update rql.go

Co-Authored-By: Ariel Mashraki <[email protected]>

* Update rql.go

Co-Authored-By: Ariel Mashraki <[email protected]>

* Update rql.go

Co-Authored-By: Ariel Mashraki <[email protected]>

* fix ) -> }

* remove extra recover
  • Loading branch information
ashtonian authored and a8m committed Aug 1, 2019
1 parent a0e6353 commit f662c28
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rql.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func MustNewParser(c Config) *Parser {
// Parse parses the given buffer into a Param object. It returns an error
// if the JSON is invalid, or its values don't follow the schema of rql.
func (p *Parser) Parse(b []byte) (pr *Params, err error) {
q := new(Query)
if err := q.UnmarshalJSON(b); err != nil {
return nil, &ParseError{"decoding buffer to *Query: " + err.Error()}
}
return p.ParseQuery(*q)
}

// ParseQuery parses the given struct into a Param object. It returns an error
// if one of the query values don't follow the schema of rql.
func (p *Parser) ParseQuery(q Query) (pr *Params, err error) {
defer func() {
if e := recover(); e != nil {
perr, ok := e.(ParseError)
Expand All @@ -152,8 +162,6 @@ func (p *Parser) Parse(b []byte) (pr *Params, err error) {
pr = nil
}
}()
q := new(Query)
must(q.UnmarshalJSON(b), "decoding buffer to Query")
pr = &Params{
Limit: p.DefaultLimit,
}
Expand Down

0 comments on commit f662c28

Please sign in to comment.