-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.go
39 lines (31 loc) · 910 Bytes
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package parser
import (
"errors"
"github.com/b2wdigital/restQL-golang/v4/internal/domain"
"github.com/b2wdigital/restQL-golang/v4/internal/parser/ast"
)
// ErrInvalidQuery represents a given query that not comply with the restQL syntax
var ErrInvalidQuery = errors.New("invalid query")
// Parser is the interface implemented by types that
// can transform a query string into an internal representation.
type Parser interface {
Parse(queryStr string) (domain.Query, error)
}
type parser struct {
astGenerator ast.Generator
}
// New returns an instance of a Parser.
func New() (Parser, error) {
generator, err := ast.New()
if err != nil {
return parser{}, err
}
return parser{astGenerator: generator}, nil
}
func (p parser) Parse(queryStr string) (domain.Query, error) {
query, err := p.astGenerator.Parse(queryStr)
if err != nil {
return domain.Query{}, err
}
return Optimize(query)
}