-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
executable file
·329 lines (303 loc) · 8.63 KB
/
value.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2021 Telefonica Cybersecurity & Cloud Tech SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package golium
import (
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"time"
"unicode"
"github.com/google/uuid"
)
// ValueAsString invokes Value and converts the return value to string.
func ValueAsString(ctx context.Context, s string) string {
return fmt.Sprintf("%v", Value(ctx, s))
}
// ValueAsInt invokes Value and converts the return value to int.
func ValueAsInt(ctx context.Context, s string) (int, error) {
v := Value(ctx, s)
if n, ok := v.(float64); ok {
return int(n), nil
}
return strconv.Atoi(s)
}
// Value converts a value as a string to consider some golium patterns.
// Supported patterns:
// - Booleans: [TRUE] or [FALSE]
// - Null value: [NULL]
// - Empty value: [EMPTY]
// - Number: [NUMBER:1234] or [NUMBER:1234.67]
// - Configuration parameters: [CONF:test.parameter]
// - Context values: [CTXT:test.context]
// - SHA256: [SHA256:text.to.be.hashed]
// - BASE64: [BASE64:text.to.be.base64.encoded]
// - Time: [NOW:+24h:unix] with the format: [NOW:{duration}:{format}]
// The value {duration} can be empty (there is no change from now timestamp) or a format valid for
// time.ParseDuration function. Currently, it supports the following units:
// "ns", "us", "ms", "s", "m", "h".
// The format can be "unix" or a layout valid for time.Format function.
// It is possible to use [NOW]. In that case, it returns an int64 with the now timestamp
// in unix format.
//
// Most cases, the return value is a string except for the following cases:
// - [TRUE] and [FALSE] return a bool type.
// - [NUMBER:1234] returns a float64 if s only contains this tag and there is no surrounding text.
// - [NOW:{duration}:{format}] returns an int64 when {format} is "unix".
func Value(ctx context.Context, s string) interface{} {
composedTag := NewComposedTag(s)
return composedTag.Value(ctx)
}
var simpleTagFuncs = map[string]func() funcReturn{
"TRUE": func() funcReturn { return funcReturn{ret: true, err: nil} },
"FALSE": func() funcReturn { return funcReturn{ret: false, err: nil} },
"EMPTY": func() funcReturn { return funcReturn{ret: "", err: nil} },
"NOW": func() funcReturn { return funcReturn{ret: time.Now().Unix(), err: nil} },
"NULL": func() funcReturn { return funcReturn{ret: nil, err: nil} },
"UUID": func() funcReturn {
guid, err := uuid.NewRandom()
if err != nil {
return funcReturn{ret: "", err: err}
}
return funcReturn{ret: guid.String(), err: nil}
},
}
type funcInput struct {
ctx context.Context
s string
}
type funcReturn struct {
ret interface{}
err error
}
var valuedTagFuncs = map[string]func(input funcInput) funcReturn{
"CONF": func(input funcInput) funcReturn {
m := GetEnvironment()
return funcReturn{
ret: m.Get(input.s),
err: nil,
}
},
"CTXT": func(input funcInput) funcReturn {
return funcReturn{
ret: GetContext(input.ctx).Get(input.s),
err: nil,
}
},
"SHA256": func(input funcInput) funcReturn {
return funcReturn{
ret: fmt.Sprintf("%x", sha256.Sum256([]byte(input.s))),
err: nil,
}
},
"BASE64": func(input funcInput) funcReturn {
return funcReturn{
ret: base64.StdEncoding.EncodeToString([]byte(input.s)),
err: nil,
}
},
"NUMBER": func(input funcInput) funcReturn {
parse, err := strconv.ParseFloat(input.s, 64)
return funcReturn{
ret: parse,
err: err,
}
},
"NOW": func(input funcInput) funcReturn {
process, err := processNow(input.s)
r := funcReturn{
ret: process,
err: err,
}
return r
},
}
// processNow processes tag "NOW" with the format [NOW:{duration}:{format}].
// So, tagName has the format: {duration}:{format}
func processNow(s string) (interface{}, error) {
parts := strings.SplitN(s, ":", 2)
if len(parts) != 2 {
return nil, errors.New("invalid NOW tag")
}
duration := parts[0]
format := parts[1]
now := time.Now()
if duration != "" {
d, err := time.ParseDuration(duration)
if err != nil {
return nil, fmt.Errorf("invalid duration in NOW tag: %w", err)
}
now = now.Add(d)
}
switch format {
case "unix":
return now.Unix(), nil
default:
return now.Format(format), nil
}
}
// Tag interface to calculate the value of a tag.
// A golium tag is a text surrounded by brackets that can be evaluated into a value.
// For example: [CONF:property]
type Tag interface {
Value(ctx context.Context) interface{}
}
// StringTag represents a implicit tag composed of a text.
// This tag is used to compose a string with a tag to generate a new string.
type StringTag struct {
s string
}
// NewStringTag creates a Tag that evaluated to the string without any modification.
func NewStringTag(s string) Tag {
return &StringTag{s: s}
}
func (s StringTag) Value(ctx context.Context) interface{} {
return s.s
}
// NamedTag is a Tag that can be evaluated with a tag function depending on the name of the tag.
type NamedTag struct {
s string
}
// NewNamedTag creates a NamedTag.
func NewNamedTag(s string) Tag {
return &NamedTag{s: s}
}
func (t NamedTag) Value(ctx context.Context) interface{} {
value := t.valueWithError(ctx)
if value.err == nil {
return value.ret
}
return t.s
}
func (t NamedTag) valueWithError(ctx context.Context) funcReturn {
tag := t.s[1 : len(t.s)-1]
parts := strings.SplitN(tag, ":", 2)
tagName := parts[0]
if len(parts) == 2 {
tagValue := parts[1]
procValuedTag := t.processValuedTag(ctx, tagName, tagValue)
return funcReturn{ret: procValuedTag.ret, err: procValuedTag.err}
}
return t.processSimpleTag(tagName)
}
func (t NamedTag) processSimpleTag(tagName string) funcReturn {
if f, ok := simpleTagFuncs[tagName]; ok {
return f()
}
return funcReturn{ret: nil, err: fmt.Errorf("invalid tag '%s'", tagName)}
}
func (t NamedTag) processValuedTag(
ctx context.Context,
tagName, tagValue string,
) funcReturn {
if f, ok := valuedTagFuncs[tagName]; ok {
composedTag := NewComposedTag(tagValue)
composedTagValue := composedTag.Value(ctx)
composedTagValueString := fmt.Sprintf("%v", composedTagValue)
return f(funcInput{
ctx: ctx,
s: composedTagValueString,
})
}
return funcReturn{
ret: nil,
err: fmt.Errorf("invalid tag '%s'", tagName),
}
}
type separator struct {
opener bool
pos int
}
// ComposedTag is a composition of tags, including StringTags, NamedTags and other ComposedTags
// to provide an evaluation.
type ComposedTag struct {
s string
}
// NewComposedTag creates a ComposedTag.
func NewComposedTag(s string) Tag {
return &ComposedTag{s: s}
}
func (t ComposedTag) findSeparators() (separators []separator) {
for i, c := range t.s {
if c == '[' && unicode.IsUpper(rune(t.s[i+1])) {
sep := separator{opener: true, pos: i}
separators = append(separators, sep)
} else if c == ']' {
sep := separator{opener: false, pos: i}
separators = append(separators, sep)
}
}
return
}
func (t ComposedTag) buildTags(separators []separator) []Tag {
tags := []Tag{}
if len(separators) < 2 {
return tags
}
lastCloser := -1
for i := 0; i < len(separators)-1; i++ {
if !separators[i].opener {
// Discard it because we must start with an opener
continue
}
distance := 1
for j := i + 1; j < len(separators); j++ {
if separators[j].opener {
distance++
} else {
distance--
}
if distance != 0 {
continue
}
opener := separators[i].pos
closer := separators[j].pos
// Add a tag text if there is a text prefix
if lastCloser+1 < opener {
tag := NewStringTag(t.s[lastCloser+1 : opener])
tags = append(tags, tag)
}
// Found end of tag
tag := NewNamedTag(t.s[opener : closer+1])
tags = append(tags, tag)
i = j
lastCloser = closer
break
}
}
// Add a tag text if there is a text suffix
if lastCloser+1 < len(t.s) {
tag := NewStringTag(t.s[lastCloser+1:])
tags = append(tags, tag)
}
return tags
}
func (t ComposedTag) Value(ctx context.Context) interface{} {
tags := t.buildTags(t.findSeparators())
if len(tags) == 0 {
return t.s
}
if len(tags) == 1 {
return tags[0].Value(ctx)
}
// If multiple tags, it returns a string with the concatenation of each tag value
var v strings.Builder
for _, tag := range tags {
v.WriteString(fmt.Sprintf("%v", tag.Value(ctx)))
}
return v.String()
}