-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_builtin.go
111 lines (105 loc) · 2.2 KB
/
func_builtin.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package sqlf
import (
"errors"
"fmt"
"strings"
"github.com/qjebbs/go-sqlf/v2/syntax"
)
var builtInFuncs = FuncMap{
"arg": funcArg,
"f": funcFragment,
"fragment": funcFragment,
"join": funcJoin,
}
func funcJoin(ctx *Context, tmpl, separator string, indexes ...int) (string, error) {
var err error
var from, to int
switch len(indexes) {
case 0:
case 1:
from = indexes[0]
case 2:
from = indexes[0]
to = indexes[1]
default:
return "", fmt.Errorf(
"too many args for #join: want 2-4 got %d",
len(indexes)+2,
)
}
if to > 0 && from > to {
return "", fmt.Errorf("invalid index range %d to %d", from, to)
}
c, err := syntax.Parse(tmpl)
if err != nil {
return "", fmt.Errorf("parse join template '%s': %w", tmpl, err)
}
b := new(strings.Builder)
var calls []*syntax.FuncCallExpr
for i, expr := range c.ExprList {
fn, ok := expr.(*syntax.FuncExpr)
if !ok {
continue
}
f, ok := ctx.fn(fn.Name)
if !ok {
return "", fmt.Errorf("unknown function #%s", fn.Name)
}
if f.JoinCompatibilityError() != nil {
return "", fmt.Errorf("function #%s is incompatible with #join: %w", fn.Name, f.joinError)
}
call := &syntax.FuncCallExpr{
Name: fn.Name,
}
c.ExprList[i] = call
calls = append(calls, call)
}
if len(calls) == 0 {
return "", fmt.Errorf("no function in join template '%s' (e.g.: #f, not #f1)", tmpl)
}
start := from
if start <= 0 {
start = 1
}
for i := start; ; i++ {
for _, call := range calls {
call.Args = []any{i}
}
s, err := buildClause(ctx, c)
if errors.Is(err, ErrInvalidIndex) {
if (from > 0 && i == from) ||
(to > 0 && i <= to) {
// report index error only if explicitly specified
return "", err
}
break
}
if err != nil {
return "", err
}
if s != "" {
if b.Len() > 0 {
b.WriteString(separator)
}
b.WriteString(s)
}
if to > 0 && i >= to {
break
}
}
return b.String(), nil
}
func funcArg(ctx *Context, i int) (string, error) {
c, err := ctx.mustFragment()
if err != nil {
return "", err
}
return c.Args.Build(ctx, i)
}
func funcFragment(ctx *Context, i int) (string, error) {
c, err := ctx.mustFragment()
if err != nil {
return "", err
}
return c.Fragments.Build(ctx, i)
}