@@ -28,7 +28,7 @@ func Test_replaceAllPatterns(t *testing.T) {
28
28
FCtx : ottl.FunctionContext {
29
29
Set : componenttest .NewNopTelemetrySettings (),
30
30
},
31
- Fact : StandardConverters [pcommon.Map ]()[ "SHA256" ] ,
31
+ Fact : optionalFnTestFactory [pcommon.Map ](),
32
32
}
33
33
optionalArg := ottl.NewTestingOptional [ottl.FunctionGetter [pcommon.Map ]](ottlValue )
34
34
@@ -59,8 +59,68 @@ func Test_replaceAllPatterns(t *testing.T) {
59
59
},
60
60
function : optionalArg ,
61
61
want : func (expectedMap pcommon.Map ) {
62
- expectedMap .PutStr ("test" , "4804d6b7f03268e33f78c484977f3d81771220df07cc6aac4ad4868102141fad world" )
63
- expectedMap .PutStr ("test2" , "4804d6b7f03268e33f78c484977f3d81771220df07cc6aac4ad4868102141fad" )
62
+ expectedMap .PutStr ("test" , "hash(hello {universe}) world" )
63
+ expectedMap .PutStr ("test2" , "hash(hello {universe})" )
64
+ expectedMap .PutStr ("test3" , "goodbye world1 and world2" )
65
+ expectedMap .PutInt ("test4" , 1234 )
66
+ expectedMap .PutDouble ("test5" , 1234 )
67
+ expectedMap .PutBool ("test6" , true )
68
+ },
69
+ },
70
+ {
71
+ name : "replace only matches (with capture group and hash function)" ,
72
+ target : target ,
73
+ mode : modeValue ,
74
+ pattern : "(hello)" ,
75
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
76
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
77
+ return "$1" , nil
78
+ },
79
+ },
80
+ function : optionalArg ,
81
+ want : func (expectedMap pcommon.Map ) {
82
+ expectedMap .PutStr ("test" , "hash(hello) world" )
83
+ expectedMap .PutStr ("test2" , "hash(hello)" )
84
+ expectedMap .PutStr ("test3" , "goodbye world1 and world2" )
85
+ expectedMap .PutInt ("test4" , 1234 )
86
+ expectedMap .PutDouble ("test5" , 1234 )
87
+ expectedMap .PutBool ("test6" , true )
88
+ },
89
+ },
90
+ {
91
+ name : "replace only matches (no capture group and with hash function)" ,
92
+ target : target ,
93
+ mode : modeValue ,
94
+ pattern : "hello" ,
95
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
96
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
97
+ return "$1" , nil
98
+ },
99
+ },
100
+ function : optionalArg ,
101
+ want : func (expectedMap pcommon.Map ) {
102
+ expectedMap .PutStr ("test" , "hash() world" )
103
+ expectedMap .PutStr ("test2" , "hash()" )
104
+ expectedMap .PutStr ("test3" , "goodbye world1 and world2" )
105
+ expectedMap .PutInt ("test4" , 1234 )
106
+ expectedMap .PutDouble ("test5" , 1234 )
107
+ expectedMap .PutBool ("test6" , true )
108
+ },
109
+ },
110
+ {
111
+ name : "replace only matches (no capture group or hash function)" ,
112
+ target : target ,
113
+ mode : modeValue ,
114
+ pattern : "hello" ,
115
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
116
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
117
+ return "$1" , nil
118
+ },
119
+ },
120
+ function : ottl.Optional [ottl.FunctionGetter [pcommon.Map ]]{},
121
+ want : func (expectedMap pcommon.Map ) {
122
+ expectedMap .PutStr ("test" , " world" )
123
+ expectedMap .PutStr ("test2" , "" )
64
124
expectedMap .PutStr ("test3" , "goodbye world1 and world2" )
65
125
expectedMap .PutInt ("test4" , 1234 )
66
126
expectedMap .PutDouble ("test5" , 1234 )
@@ -127,6 +187,106 @@ func Test_replaceAllPatterns(t *testing.T) {
127
187
expectedMap .PutBool ("test6" , true )
128
188
},
129
189
},
190
+ {
191
+ name : "regex match (with multiple capture groups)" ,
192
+ target : target ,
193
+ mode : modeValue ,
194
+ pattern : `(world1) and (world2)` ,
195
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
196
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
197
+ return "blue-$1 and blue-$2" , nil
198
+ },
199
+ },
200
+ function : ottl.Optional [ottl.FunctionGetter [pcommon.Map ]]{},
201
+ want : func (expectedMap pcommon.Map ) {
202
+ expectedMap .PutStr ("test" , "hello world" )
203
+ expectedMap .PutStr ("test2" , "hello" )
204
+ expectedMap .PutStr ("test3" , "goodbye blue-world1 and blue-world2" )
205
+ expectedMap .PutInt ("test4" , 1234 )
206
+ expectedMap .PutDouble ("test5" , 1234 )
207
+ expectedMap .PutBool ("test6" , true )
208
+ },
209
+ },
210
+ {
211
+ name : "regex match (with multiple matches from one capture group)" ,
212
+ target : target ,
213
+ mode : modeValue ,
214
+ pattern : `(world\d)` ,
215
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
216
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
217
+ return "blue-$1" , nil
218
+ },
219
+ },
220
+ function : ottl.Optional [ottl.FunctionGetter [pcommon.Map ]]{},
221
+ want : func (expectedMap pcommon.Map ) {
222
+ expectedMap .PutStr ("test" , "hello world" )
223
+ expectedMap .PutStr ("test2" , "hello" )
224
+ expectedMap .PutStr ("test3" , "goodbye blue-world1 and blue-world2" )
225
+ expectedMap .PutInt ("test4" , 1234 )
226
+ expectedMap .PutDouble ("test5" , 1234 )
227
+ expectedMap .PutBool ("test6" , true )
228
+ },
229
+ },
230
+ {
231
+ name : "regex match (with multiple capture groups and hash function)" ,
232
+ target : target ,
233
+ mode : modeValue ,
234
+ pattern : `(world1) and (world2)` ,
235
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
236
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
237
+ return "$1" , nil
238
+ },
239
+ },
240
+ function : optionalArg ,
241
+ want : func (expectedMap pcommon.Map ) {
242
+ expectedMap .PutStr ("test" , "hello world" )
243
+ expectedMap .PutStr ("test2" , "hello" )
244
+ expectedMap .PutStr ("test3" , "goodbye hash(world1)" )
245
+ expectedMap .PutInt ("test4" , 1234 )
246
+ expectedMap .PutDouble ("test5" , 1234 )
247
+ expectedMap .PutBool ("test6" , true )
248
+ },
249
+ },
250
+ {
251
+ name : "regex match (with multiple capture groups and hash function)" ,
252
+ target : target ,
253
+ mode : modeValue ,
254
+ pattern : `(world1) and (world2)` ,
255
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
256
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
257
+ return "$2" , nil
258
+ },
259
+ },
260
+ function : optionalArg ,
261
+ want : func (expectedMap pcommon.Map ) {
262
+ expectedMap .PutStr ("test" , "hello world" )
263
+ expectedMap .PutStr ("test2" , "hello" )
264
+ expectedMap .PutStr ("test3" , "goodbye hash(world2)" )
265
+ expectedMap .PutInt ("test4" , 1234 )
266
+ expectedMap .PutDouble ("test5" , 1234 )
267
+ expectedMap .PutBool ("test6" , true )
268
+ },
269
+ },
270
+ {
271
+ name : "regex match (with multiple matches from one capture group and hash function)" ,
272
+ target : target ,
273
+ mode : modeValue ,
274
+ pattern : `(world\d)` ,
275
+ replacement : ottl.StandardStringGetter [pcommon.Map ]{
276
+ Getter : func (context.Context , pcommon.Map ) (any , error ) {
277
+ return "$1" , nil
278
+ },
279
+ },
280
+ function : optionalArg ,
281
+ want : func (expectedMap pcommon.Map ) {
282
+ expectedMap .PutStr ("test" , "hello world" )
283
+ expectedMap .PutStr ("test2" , "hello" )
284
+ expectedMap .PutStr ("test3" , "goodbye hash(world1) and hash(world2)" )
285
+ expectedMap .PutInt ("test4" , 1234 )
286
+ expectedMap .PutDouble ("test5" , 1234 )
287
+ expectedMap .PutBool ("test6" , true )
288
+ },
289
+ },
130
290
{
131
291
name : "replace only matches" ,
132
292
target : target ,
0 commit comments