欢迎访问我的博客:kelvin-qzy.top/
局部变量
除了属性外,rs也支持本地变量(local variables)在脚本中使用。
标识符
- Message JSON property names start with “$!”
- Local variables names start with “$.”
JSON消息属性以$!
开头,局部变量以$.
开头
set
设置local-variable或json属性的值,如果冲突会合并,忽略或重置。
set $.x!one = "val_1";
# results in $. = { "x": { "one": "val_1" } }
set $.y!two = "val_2";
# results in $. = { "x": { "one": "val_1" }, "y": { "two": "val_2" } }
set $.z!var = $.x;
# results in $. = { "x": { "one": "val_1" }, "y": { "two": "val_2" }, "z": { "var": { "one": "val_1" } } }
set $.z!var = $.y;
# results in $. = { "x": { "one": "val_1" }, "y": { "two": "val_2" }, "z": { "var": { "one": "val_1" } }, "two": "val_2" }
# note that the key *two* is at root level and not under *$.z!var*.
unset
移除键值
set $.x!val = "val_1";
unset $.x!val;
# results in $. = { "x": { } }
reset
强制设置新值,无论原来是啥
# to contrast with the set example above, here is how results would look with reset
set $.x!one = "val_1";
set $.y!two = "val_2";
set $.z!var = $.x;
# results in $. = { "x": { "one": "val_1" }, "y": { "two": "val_2" }, "z": { "var": { "one": "val_1" } } }
# 'set' or 'reset' can be used interchangeably above(3 lines), they both have the same behaviour, as variable doesn't have an existing value
reset $.z!var = $.y;
# results in $. = { "x": { "one": "val_1" }, "y": { "two": "val_2" }, "z": { "var": { "two": "val_2" } } }
# note how the value of $.z!var was replaced
reset $.x = "quux";
# results in $. = { "x": "quux", "y": { "two": "val_2" }, "z": { "var": { "two": "val_2" } } }
用法
设置了这些变量后可以在模板的property里直接引用!
例如:(第二行所示)
template(name="outfmt" type="list") {
property(name="$!x!somekey")
constant(value="\n" outname="IWantThisInMyDB")
}