-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.go
63 lines (54 loc) · 1.65 KB
/
properties.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
package sqlf
import "fmt"
// Properties is a list of properties.
type Properties []Property
// Build builds the propery at i.
// it returns ErrInvalidIndex when the i is out of range, which
// is the required behaviour for a custom #func to be compatible
// with #join.
//
// See examples for ContextWithFuncs() for how to use it.
func (p Properties) Build(ctx *Context, i int) (string, error) {
if i < 1 || i > len(p) {
return "", fmt.Errorf("%w: %d", ErrInvalidIndex, i)
}
return p[i-1].BuildFragment(ctx)
}
// checkUsage checks if all properties are used.
func (p Properties) checkUsage() error {
for i, prop := range p {
if !prop.Used() {
return fmt.Errorf("#%d unused", i+1)
}
}
return nil
}
// NewFragmentProperties creates new properties from FragmentBuilder.
// It's useful for creating global fragment properties shared between fragments,
// see examples for ContextWithFuncs() for how to use it.
func NewFragmentProperties(fragments ...FragmentBuilder) Properties {
r := make(Properties, 0)
for _, f := range fragments {
r = append(r, newDefaultProperty(f))
}
return r
}
// NewArgsProperties creates new properties from args.
// It's useful for creating global arg properties shared between fragments,
// see examples for ContextWithFuncs() for how to use it.
func NewArgsProperties(args ...any) Properties {
r := make(Properties, 0)
for _, a := range args {
r = append(r, newDefaultProperty(&arg{a}))
}
return r
}
var _ FragmentBuilder = (*arg)(nil)
type arg struct {
any
}
// BuildFragment implements FragmentBuilder
func (c *arg) BuildFragment(ctx *Context) (query string, err error) {
built := ctx.CommitArg(c.any)
return built, nil
}