如何替换 Jsonnet 字符串中搜索条件后面的部分字符串?
How to replace part of string BEHIND search criteria in Jsonnet string?
我正在寻找与 std.strReplace(str, from, to)
等效的方法来替换 Jsonnet 中的部分字符串。我需要 from
更像是一个“模式”,类似于 s/key="[^"]*"/key="myNewValue"/g
,所以实际上我正在寻找的是一个正则表达式搜索和替换。
编辑:
好的,这可能会帮助我解决我的具体问题:
local replaceKey(string) = (
local replaceNext = false;
std.join('"', [
if std.endsWith(x, "key=") then
replaceNext = true;
x
else if replaceNext then
replaceNext = false;
"myNewValue"
else
x
for x in std.split(string, '"')
])
);
但是“为先前定义的局部变量设置新值”(replaceNext = true;
) 将不起作用。
Not a binary operator: =
有什么想法吗?
Jsonnet 目前不支持正则表达式,一些参考资料:
- GH 问题请求支持:https://github.com/google/go-jsonnet/issues/409
tanka
中的正则表达式支持(jsonnet -> ksonnet 继承),针对 Kubernetes:https://tanka.dev/jsonnet/native#regexsubst
我现在有以下解决方案:
local modifyElement(element, newValue) =
std.join('"', std.mapWithIndex(
function(i, str)
if i == 1 then
newValue
else
str,
std.split(element, '"')
));
local splitSubstring(string, pattern) = (
local indexes = [0] + std.findSubstr(pattern, string);
local lenIdx = std.length(indexes);
local lenStr = std.length(string);
std.mapWithIndex(
function(i, strIndex)
std.substr(string, strIndex,
if lenIdx > i+1 then indexes[i+1]-strIndex
else lenStr-strIndex),
indexes
)
);
local replaceValue(string, searchKey, newValue) =
std.join("", std.mapWithIndex(
function(index, element)
if index == 0 then
element
else
modifyElement(element, newValue),
splitSubstring(string, searchKey)));
// TESTCASE
local oldExpression = 'rate(kube_pod_container_status_restarts_total{namespace=~"^(ns1|ns2)$",job="expose-kubernetes-metrics"}[10m]) * 60 * 5 > 0';
{'test': replaceValue(oldExpression, "namespace=~", "^myspaces-.*$")}
不过,如果这可能更容易实现,我会很感兴趣,因为对于这样一个微不足道的任务来说,这实在是太疯狂了。
我正在寻找与 std.strReplace(str, from, to)
等效的方法来替换 Jsonnet 中的部分字符串。我需要 from
更像是一个“模式”,类似于 s/key="[^"]*"/key="myNewValue"/g
,所以实际上我正在寻找的是一个正则表达式搜索和替换。
编辑: 好的,这可能会帮助我解决我的具体问题:
local replaceKey(string) = (
local replaceNext = false;
std.join('"', [
if std.endsWith(x, "key=") then
replaceNext = true;
x
else if replaceNext then
replaceNext = false;
"myNewValue"
else
x
for x in std.split(string, '"')
])
);
但是“为先前定义的局部变量设置新值”(replaceNext = true;
) 将不起作用。
Not a binary operator: =
有什么想法吗?
Jsonnet 目前不支持正则表达式,一些参考资料:
- GH 问题请求支持:https://github.com/google/go-jsonnet/issues/409
tanka
中的正则表达式支持(jsonnet -> ksonnet 继承),针对 Kubernetes:https://tanka.dev/jsonnet/native#regexsubst
我现在有以下解决方案:
local modifyElement(element, newValue) =
std.join('"', std.mapWithIndex(
function(i, str)
if i == 1 then
newValue
else
str,
std.split(element, '"')
));
local splitSubstring(string, pattern) = (
local indexes = [0] + std.findSubstr(pattern, string);
local lenIdx = std.length(indexes);
local lenStr = std.length(string);
std.mapWithIndex(
function(i, strIndex)
std.substr(string, strIndex,
if lenIdx > i+1 then indexes[i+1]-strIndex
else lenStr-strIndex),
indexes
)
);
local replaceValue(string, searchKey, newValue) =
std.join("", std.mapWithIndex(
function(index, element)
if index == 0 then
element
else
modifyElement(element, newValue),
splitSubstring(string, searchKey)));
// TESTCASE
local oldExpression = 'rate(kube_pod_container_status_restarts_total{namespace=~"^(ns1|ns2)$",job="expose-kubernetes-metrics"}[10m]) * 60 * 5 > 0';
{'test': replaceValue(oldExpression, "namespace=~", "^myspaces-.*$")}
不过,如果这可能更容易实现,我会很感兴趣,因为对于这样一个微不足道的任务来说,这实在是太疯狂了。