fix(wasm-go): transformer performs an add op when the replace key does not exist (#2706)

This commit is contained in:
WeixinX
2025-08-04 20:38:29 +08:00
committed by GitHub
parent 5f65b4f5b0
commit abc31169a2
6 changed files with 535 additions and 45 deletions

View File

@@ -915,16 +915,13 @@ func (h kvHandler) handle(host, path string, kvs map[string][]string, mapSourceD
}
}
case ReplaceK:
// replace: 若指定 key 不存在,则无操作;否则替换 value 为 newValue
// replace: 若指定 key 不存在,相当于添加操作;否则替换 value 为 newValue
for _, replace := range kvtOp.replaceKvtGroup {
key, newValue := replace.key, replace.newValue
if _, ok := kvs[key]; !ok {
continue
}
if replace.reg != nil {
newValue = replace.reg.matchAndReplace(newValue, host, path)
}
kvs[replace.key] = []string{newValue}
kvs[key] = []string{newValue}
}
case AddK:
// add: 若指定 key 存在则无操作;否则添加 key:value
@@ -1047,12 +1044,9 @@ func (h jsonHandler) handle(host, path string, oriData []byte, mapSourceData map
}
}
case ReplaceK:
// replace: 若指定 key 不存在,则操作;否则替换 value 为 newValue
// replace: 若指定 key 不存在,则相当于添加操作;否则替换 value 为 newValue
for _, replace := range kvtOp.replaceKvtGroup {
key, newValue, valueType := replace.key, replace.newValue, replace.typ
if !gjson.GetBytes(data, key).Exists() {
continue
}
if valueType == "string" && replace.reg != nil {
newValue = replace.reg.matchAndReplace(newValue, host, path)
}