mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 08:33:12 +09:00
Revert keyToLabels label parsing
This commit is contained in:
1 parent
3f8ef85987
commit
81fcea8398
1 file changed
+11
-55
@@ -271,64 +271,20 @@ function labelsToKey(labels: LabelValues, metricLabels: string[]) {
|
|||||||
|
|
||||||
function keyToLabels(key: string) {
|
function keyToLabels(key: string) {
|
||||||
let labels = new Map<string, string>();
|
let labels = new Map<string, string>();
|
||||||
let index = 0;
|
|
||||||
|
|
||||||
while (index < key.length) {
|
if (key !== "") {
|
||||||
let equal = key.indexOf("=", index);
|
for (let pair of key.split(",")) {
|
||||||
if (equal === -1) {
|
let [name, value] = pair.split("=", 2);
|
||||||
throw new Error(`Malformed label key '${key}'`);
|
if (!name || value === undefined) {
|
||||||
}
|
continue;
|
||||||
|
|
||||||
let name = key.slice(index, equal);
|
|
||||||
let cursor = equal + 1;
|
|
||||||
if (key[cursor] !== "\"") {
|
|
||||||
throw new Error(`Malformed label key '${key}'`);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor += 1;
|
|
||||||
let valueParts: string[] = [];
|
|
||||||
while (cursor < key.length) {
|
|
||||||
let char = key[cursor];
|
|
||||||
if (char === "\"") {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
labels.set(name, value
|
||||||
if (char === "\\") {
|
.slice(1, -1)
|
||||||
cursor += 1;
|
.replace(/\\"/g, "\"")
|
||||||
if (cursor >= key.length) {
|
.replace(/\\n/g, "\n")
|
||||||
throw new Error(`Malformed escape in label value for '${name}'`);
|
.replace(/\\\\/g, "\\")
|
||||||
}
|
);
|
||||||
char = key[cursor];
|
|
||||||
if (char === "n") {
|
|
||||||
valueParts.push("\n");
|
|
||||||
} else if (char === "\\") {
|
|
||||||
valueParts.push("\\");
|
|
||||||
} else if (char === "\"") {
|
|
||||||
valueParts.push("\"");
|
|
||||||
} else {
|
|
||||||
valueParts.push(char);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
valueParts.push(char);
|
|
||||||
}
|
|
||||||
cursor += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cursor >= key.length || key[cursor] !== "\"") {
|
|
||||||
throw new Error(`Missing closing quote for label '${name}'`);
|
|
||||||
}
|
|
||||||
|
|
||||||
labels.set(name, valueParts.join(""));
|
|
||||||
cursor += 1;
|
|
||||||
|
|
||||||
if (cursor >= key.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key[cursor] !== ",") {
|
|
||||||
throw new Error(`Malformed label key '${key}'`);
|
|
||||||
}
|
|
||||||
index = cursor + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return labels;
|
return labels;
|
||||||
|
|||||||
Reference in new issue
Block a user