-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpol.go
283 lines (242 loc) · 6.25 KB
/
interpol.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package interpol
import (
"bytes"
"fmt"
"strconv"
"strings"
)
// element is part of an interpolated string
type element struct {
handler Handler
modifier Modifier
}
func (ie *element) String() string {
str := ie.handler.String()
if ie.modifier != nil {
str = ie.modifier.Modify(str)
}
return str
}
func (ie *element) Reset() {
ie.handler.Reset()
}
func (ie *element) Next() bool {
return ie.handler.Next()
}
// InterpolatedString contains an interpolator object
type InterpolatedString struct {
elements []*element
buffer *bytes.Buffer
}
func newInterpolatedString(size int) *InterpolatedString {
ret := &InterpolatedString{}
bs := make([]byte, 1024)
ret.buffer = bytes.NewBuffer(bs)
ret.elements = make([]*element, size)
return ret
}
// convert InterpolatedString to a string
func (ips *InterpolatedString) String() string {
ips.buffer.Reset()
for _, e := range ips.elements {
ips.buffer.WriteString(e.String())
}
return ips.buffer.String()
}
// Parameters for an interpolator command in a more accesible form
type Parameters struct {
Type string
Properties map[string]string
}
// GetString returns an interpolation parameter as string
func (id *Parameters) GetString(name string, def string) string {
if s, okay := id.Properties[name]; okay {
return s
}
return def
}
// GetInteger returns an interpolation parameter as int
func (id *Parameters) GetInteger(name string, def int) int {
if s, okay := id.Properties[name]; okay {
n, err := strconv.Atoi(s)
if err == nil {
return n
}
}
return def
}
// GetBoolean returns an interpolation parameter as int
func (id *Parameters) GetBoolean(name string, def bool) bool {
if s, okay := id.Properties[name]; okay {
n, err := strconv.ParseBool(s)
if err == nil {
return n
}
}
return def
}
// Interpol context for an interpolation
type Interpol struct {
handlerFactories map[string]HandlerFactory
modifierFactories map[string]ModifierFactory
elements []*element
exported map[string]Handler
first bool
}
// New creates a new interpolator context
func New() *Interpol {
ret := &Interpol{}
ret.handlerFactories = make(map[string]HandlerFactory)
ret.modifierFactories = make(map[string]ModifierFactory)
ret.exported = make(map[string]Handler)
ret.elements = make([]*element, 0)
ret.Reset()
return ret
}
// Reset resets everything to its original state
func (ip *Interpol) Reset() {
for _, h := range ip.elements {
h.Reset()
}
ip.first = true
}
// Next calculates the next value
func (ip *Interpol) Next() bool {
// This thing allows us to do "for ip.Next() { }", which
// simplifies user code a bit. Note that it also means
// Interpol.Next() and Handler.Next() function differently
if ip.first && len(ip.elements) > 0 {
ip.first = false
return true
}
for _, e := range ip.elements {
if e.Next() {
return true
}
e.Reset()
}
return false
}
//
// modifier functions
//
// AddModifier registers a new modifier
func (ip *Interpol) AddModifier(typ string, modifier ModifierFactory) error {
if _, okay := ip.modifierFactories[typ]; okay {
return fmt.Errorf("Modifier '%s' already exists", typ)
}
ip.modifierFactories[typ] = modifier
return nil
}
func (ip *Interpol) findModifierFactory(name string) ModifierFactory {
name = strings.ToLower(name)
if def, okay := ip.modifierFactories[name]; okay {
return def
}
return findDefaultModifierFactory(name)
}
func (ip *Interpol) createModifier(id *Parameters) (Modifier, error) {
if id != nil {
if name := id.GetString("modifier", ""); name != "" {
mf := ip.findModifierFactory(name)
if mf == nil {
return nil, fmt.Errorf("Unknown modifier: %s", name)
}
return mf(ip, id)
}
}
return nil, nil
}
//
// handler functions
//
// AddHandler adds a handler for a specific type of interpolator
func (ip *Interpol) AddHandler(typ string, creator HandlerFactory) error {
if _, okay := ip.handlerFactories[typ]; okay {
return fmt.Errorf("Handler for '%s' already exists", typ)
}
ip.handlerFactories[typ] = creator
return nil
}
func (ip *Interpol) findHandlerFactory(name string) HandlerFactory {
name = strings.ToLower(name)
if def, okay := ip.handlerFactories[name]; okay {
return def
}
return findDefaultHandlerFactory(name)
}
// import/export functions for copy
func (ip *Interpol) tryImport(name string) Handler {
if h, found := ip.exported[name]; found {
return h
}
return nil
}
func (ip *Interpol) tryExport(data *Parameters, h Handler) error {
if data != nil {
if name, okay := data.Properties["name"]; okay {
if _, seenbefore := ip.exported[name]; seenbefore {
return fmt.Errorf("name '%s' already exists", name)
}
ip.exported[name] = h
}
}
return nil
}
// Add creates a new string to be interpolated
func (ip *Interpol) Add(text string) (*InterpolatedString, error) {
// parse the line for elements
els, err := parseLine(text)
if err != nil {
return nil, fmt.Errorf("Unable to parse '%s'", text)
}
// convert elements to handlers and build the return string
ret := newInterpolatedString(len(els))
for i, e := range els {
var factory HandlerFactory
var id *Parameters
if e.static {
factory = newTextHandler
id = nil
} else {
id, err = parseInterpolator(e.text)
if err != nil {
return nil, err
}
factory = ip.findHandlerFactory(id.Type)
if factory == nil {
return nil, fmt.Errorf("Cannot find a handler for '%s'", e.text)
}
}
handler, err := factory(ip, e.text, id)
if err != nil {
return nil, fmt.Errorf("Cannot initialize handler '%s': %v", text, err)
}
modifier, err := ip.createModifier(id)
if err != nil {
return nil, fmt.Errorf("Cannot create modifier '%s': %v", text, err)
}
err = ip.tryExport(id, handler)
if err != nil {
return nil, err
}
ret.elements[i] = &element{handler: handler, modifier: modifier}
}
// add the new handlers to the list of all handlers in this context
for _, h := range ret.elements {
ip.elements = append(ip.elements, h)
}
return ret, nil
}
// AddMultiple creates multiple strings to be interpolated
func (ip *Interpol) AddMultiple(texts ...string) ([]*InterpolatedString, error) {
ret := make([]*InterpolatedString, len(texts))
for i, text := range texts {
ips, err := ip.Add(text)
if err != nil {
return nil, err
}
ret[i] = ips
}
return ret, nil
}