-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer_incremental.go
77 lines (69 loc) · 1.89 KB
/
transformer_incremental.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
package sinoname
import (
"context"
"strconv"
)
// IncrementalPrefix adds an incrementing integer to the end of the string.
// The range of added numbers at the end of the string is [1, n].
//
// Foo1, Foo2, Foo3, Foo4 ... FooN
var IncrementalPrefix = func(n int, sep string) func(cfg *Config) (Transformer, bool) {
return func(cfg *Config) (Transformer, bool) {
return &incrementalTransformer{
cfg: cfg,
where: prefix,
n: n,
sep: sep,
}, false
}
}
// IncrementalSuffix adds an incrementing integer to the beginning of the string.
// The range of added numbers at the beginning of the string is [1, n].
//
// 1Foo, 2Foo, 3Foo, 4Foo ... NFoo
var IncrementalSuffix = func(n int, sep string) func(cfg *Config) (Transformer, bool) {
return func(cfg *Config) (Transformer, bool) {
return &incrementalTransformer{
cfg: cfg,
where: suffix,
n: n,
sep: sep,
}, false
}
}
// IncrementalCircumfix adds an incrementing circumfix integer.
// The range of added numbers at the end of the string is [1, n].
//
// 1Foo1, 2Foo2, 3Foo3, 4Foo4 ... NFooN
var IncrementalCircumfix = func(n int, sep string) func(cfg *Config) (Transformer, bool) {
return func(cfg *Config) (Transformer, bool) {
return &incrementalTransformer{
cfg: cfg,
where: circumfix,
n: n,
sep: sep,
}, false
}
}
type incrementalTransformer struct {
cfg *Config
where affix
n int
sep string
}
func (t *incrementalTransformer) Transform(ctx context.Context, in MessagePacket) (MessagePacket, error) {
for i := 1; i <= t.n; i++ {
add := strconv.Itoa(i)
out, ok := applyAffix(t.cfg, t.where, in.Message, t.sep, add)
if !ok {
// retrun early even if value too long.
// values only continue growing, no point in continuing.
return in, nil
}
if ok, err := t.cfg.Source.Valid(ctx, out); err != nil || ok {
in.setAndIncrement(out)
return in, err
}
}
return in, nil
}