-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathLogProbeTestHelper.java
More file actions
53 lines (48 loc) · 1.7 KB
/
LogProbeTestHelper.java
File metadata and controls
53 lines (48 loc) · 1.7 KB
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
package com.datadog.debugger.util;
import com.datadog.debugger.el.ValueScript;
import com.datadog.debugger.probe.LogProbe;
import datadog.trace.util.Strings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LogProbeTestHelper {
public static List<LogProbe.Segment> parseTemplate(String template) {
if (template == null) {
return Collections.emptyList();
}
List<LogProbe.Segment> result = new ArrayList<>();
int currentIdx = 0;
int startStrIdx = 0;
do {
int startIdx = template.indexOf('{', currentIdx);
if (startIdx == -1) {
addStrSegment(result, template.substring(startStrIdx));
return result;
}
if (startIdx + 1 < template.length() && template.charAt(startIdx + 1) == '{') {
currentIdx = startIdx + 2;
continue;
}
int endIdx = template.indexOf('}', startIdx);
if (endIdx == -1) {
addStrSegment(result, template.substring(startStrIdx));
currentIdx = startIdx + 1;
startStrIdx = currentIdx;
} else {
if (startStrIdx != startIdx) {
addStrSegment(result, template.substring(startStrIdx, startIdx));
}
String expr = template.substring(startIdx + 1, endIdx);
result.add(new LogProbe.Segment(new ValueScript(ValueScript.parseRefPath(expr), expr)));
currentIdx = endIdx + 1;
startStrIdx = currentIdx;
}
} while (currentIdx < template.length());
return result;
}
private static void addStrSegment(List<LogProbe.Segment> segments, String str) {
str = Strings.replace(str, "{{", "{");
str = Strings.replace(str, "}}", "}");
segments.add(new LogProbe.Segment(str));
}
}