-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.go
53 lines (44 loc) · 1.25 KB
/
query.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package fauna
import (
"errors"
"fmt"
)
type queryFragment struct {
literal bool
value any
}
// Query represents a query to be sent to Fauna.
type Query struct {
fragments []*queryFragment
}
// FQL creates a [fauna.Query] from an FQL string and set of arguments.
//
// args are optional. If provided their keys must match with `${name}` sigils
// in the query. FQL `${value} + 1` must have an argument called "value" in the
// args map.
//
// The values of args can be any type, including [fauna.Query] to allow for
// query composition.
func FQL(query string, args map[string]any) (*Query, error) {
parts, err := parseTemplate(query)
if err != nil {
return nil, err
}
fragments := make([]*queryFragment, 0, len(parts))
for _, part := range parts {
switch category := part.Category; category {
case templateLiteral:
fragments = append(fragments, &queryFragment{true, part.Text})
case templateVariable:
if args == nil {
return nil, errors.New("found template variable, but args is nil")
}
if arg, ok := args[part.Text]; ok {
fragments = append(fragments, &queryFragment{false, arg})
} else {
return nil, fmt.Errorf("template variable %s not found in args", part.Text)
}
}
}
return &Query{fragments: fragments}, nil
}