-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathParamTrigger.go
137 lines (117 loc) · 2.71 KB
/
ParamTrigger.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package docxplate
import (
"bytes"
"fmt"
"log"
"strings"
)
// On - trigger events when command to aply
const (
TriggerOnUnknown string = ":unknown"
TriggerOnEmpty string = ":empty"
TriggerOnValue string = ":="
)
// Command - what to do when triggered
const (
TriggerCommandRemove = ":remove"
TriggerCommandClear = ":clear"
)
// Scope - scope of affected elements by command
const (
TriggerScopePlaceholder = ":placeholder"
TriggerScopeCell = ":cell"
TriggerScopeRow = ":row"
TriggerScopeList = ":list"
TriggerScopeTable = ":table"
TriggerScopeSection = ":section" // table, list..
)
// ParamTrigger - param trigger command
// {{Key :On:Command:Scope}}
// {{MyParam :empty:remove:list}} -- Read as: "`remove` `list` on `empty` value"
type ParamTrigger struct {
raw string
On string
Command string
Scope string
}
// NewParamTrigger - take raw ":empty:remove:list" and make trigger and its fields from it
func NewParamTrigger(raw []byte) *ParamTrigger {
raw = bytes.TrimSpace(raw)
raw = bytes.ToLower(raw)
// init with defaults
tr := &ParamTrigger{
raw: string(raw),
On: TriggerOnUnknown,
Command: TriggerCommandRemove,
Scope: TriggerScopeRow,
}
// Always must start with ":"
if !strings.HasPrefix(tr.raw, ":") {
return nil
}
// Remove the first ":" so split parts counting is more readable
// Split into parts
parts := strings.Split(tr.raw[1:], ":")
var countCommandParts = 0
for _, part := range parts {
switch part {
case "unknown", "empty", "=":
countCommandParts++
tr.On = ":" + part
case "remove", "clear":
countCommandParts++
tr.Command = ":" + part
case "placeholder", "cell", "row", "list", "table", "section":
countCommandParts++
tr.Scope = ":" + part
}
}
if countCommandParts != 3 {
return nil
}
if !tr.isValid() {
return nil
}
return tr
}
// Validate trigger
func (tr *ParamTrigger) isValid() bool {
// On
if !inSlice(tr.On, []string{
TriggerOnUnknown,
TriggerOnEmpty,
TriggerOnValue,
}) {
log.Printf("ERROR: No such trigger on [%s]", tr.On)
return false
}
// Command
if !inSlice(tr.Command, []string{
TriggerCommandClear,
TriggerCommandRemove,
}) {
log.Printf("ERROR: No such trigger command [%s]", tr.Command)
return false
}
// Scope
if !inSlice(tr.Scope, []string{
TriggerScopePlaceholder,
TriggerScopeCell,
TriggerScopeRow,
TriggerScopeList,
TriggerScopeTable,
TriggerScopeSection,
}) {
log.Printf("ERROR: No such trigger scope [%s]", tr.Scope)
return false
}
return true
}
// String - return rebuilt trigger string
func (tr *ParamTrigger) String() string {
if tr == nil {
return ""
}
s := fmt.Sprintf("%s%s%s", tr.On, tr.Command, tr.Scope)
return s
}