-
Notifications
You must be signed in to change notification settings - Fork 14
/
stage.go
55 lines (47 loc) · 1.22 KB
/
stage.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
package expressions
import (
"fmt"
"strconv"
"strings"
)
const (
ArraySeparator rune = '\x00'
ArraySeparatorString string = string(ArraySeparator)
)
// KeyBuilderFunction defines a helper function at runtime
type KeyBuilderFunction func([]KeyBuilderStage) KeyBuilderStage
// KeyBuilderStage is a stage within the compiled builder
type KeyBuilderStage func(KeyBuilderContext) string
func stageLiteral(s string) KeyBuilderStage {
return KeyBuilderStage(func(context KeyBuilderContext) string {
return s
})
}
func stageSimpleVariable(s string) KeyBuilderStage {
index, err := strconv.Atoi(s)
if err != nil {
return KeyBuilderStage(func(context KeyBuilderContext) string {
return context.GetKey(s)
})
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
return context.GetMatch(index)
})
}
func stageError(msg string) KeyBuilderStage {
errMessage := fmt.Sprintf("<%s>", msg)
return KeyBuilderStage(func(context KeyBuilderContext) string {
return errMessage
})
}
// make a delim-separated array
func MakeArray(args ...string) string {
var sb strings.Builder
for i := 0; i < len(args); i++ {
if i > 0 {
sb.WriteRune(ArraySeparator)
}
sb.WriteString(args[i])
}
return sb.String()
}