Skip to content

Commit 9754415

Browse files
committed
filterx: implicit json cast for generator assignment
Signed-off-by: Attila Szakacs <[email protected]>
1 parent 17b1fd8 commit 9754415

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

lib/filterx/filterx-grammar.ym

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ generator_assignment
245245
NULL
246246
);
247247
}
248+
| variable KW_ASSIGN expr_generator
249+
{
250+
GError *error = NULL;
251+
FilterXExpr *json_func = filterx_function_lookup(configuration, "json", NULL, &error);
252+
CHECK_FUNCTION_ERROR(json_func, @1, "json", error);
253+
254+
filterx_generator_set_fillable($3, filterx_expr_ref($1));
255+
$$ = filterx_compound_expr_new_va(TRUE,
256+
filterx_assign_new($1, filterx_generator_create_container_new($3, json_func)),
257+
$3,
258+
NULL
259+
);
260+
}
248261
| generator_plus_assignment
249262
| generator_casted_assignment
250263
;

tests/light/functional_tests/filterx/test_filterx.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def test_json_change_recursive_literal(config, syslog_ng):
635635
def test_list_literal_becomes_syslogng_list_as_string(config, syslog_ng):
636636
(file_true, file_false) = create_config(
637637
config, """
638-
$MSG = json_array(["foo", "bar", "baz"]);
638+
$MSG = ["foo", "bar", "baz"];
639639
""",
640640
)
641641
syslog_ng.start(config)
@@ -648,7 +648,7 @@ def test_list_literal_becomes_syslogng_list_as_string(config, syslog_ng):
648648
def test_list_literal_becomes_json_list_as_a_part_of_json(config, syslog_ng):
649649
(file_true, file_false) = create_config(
650650
config, """
651-
$list = json_array(["foo", "bar", "baz"]);
651+
$list = ["foo", "bar", "baz"];
652652
$MSG = json({
653653
"key": "value",
654654
"list": $list,
@@ -665,7 +665,7 @@ def test_list_literal_becomes_json_list_as_a_part_of_json(config, syslog_ng):
665665
def test_list_is_cloned_upon_assignment(config, syslog_ng):
666666
(file_true, file_false) = create_config(
667667
config, """
668-
$list = json_array(["foo", "bar", "baz"]);
668+
$list = ["foo", "bar", "baz"];
669669
$MSG = $list;
670670
$list[0] = "changed foo";
671671
$MSG[2] = "changed baz";
@@ -682,7 +682,7 @@ def test_list_is_cloned_upon_assignment(config, syslog_ng):
682682
def test_list_subscript_without_index_appends_an_element(config, syslog_ng):
683683
(file_true, file_false) = create_config(
684684
config, """
685-
$list = json_array();
685+
$list = [];
686686
$list[] = "foo";
687687
$list[] = "bar";
688688
$list[] = "baz";
@@ -699,8 +699,8 @@ def test_list_subscript_without_index_appends_an_element(config, syslog_ng):
699699

700700
def test_literal_generator_assignment(config, syslog_ng):
701701
(file_true, file_false) = create_config(
702-
config, """
703-
$MSG = json();
702+
config, r"""
703+
$MSG = {}; # implicit json
704704
$MSG.foo = {"answer": 42, "leet": 1337};
705705
$MSG["bar"] = {"answer+1": 43, "leet+1": 1338};
706706
$MSG.list = ["will be replaced"];
@@ -736,7 +736,7 @@ def test_literal_generator_casted_assignment(config, syslog_ng):
736736
def test_function_call(config, syslog_ng):
737737
(file_true, file_false) = create_config(
738738
config, """
739-
$list = json_array();
739+
$list = [];
740740
$list[] = "foo";
741741
$list[] = "bar";
742742
$list[] = "baz";
@@ -1049,7 +1049,7 @@ def test_isset_inexisting_value_already_in_scope(config, syslog_ng):
10491049
def test_isset(config, syslog_ng):
10501050
(file_true, file_false) = create_config(
10511051
config, """
1052-
$MSG = json();
1052+
$MSG = {};
10531053
$MSG.inner_key = "foo";
10541054
10551055
isset(${values.int});
@@ -1095,7 +1095,7 @@ def test_unset_value_already_in_scope(config, syslog_ng):
10951095
def test_unset_existing_key(config, syslog_ng):
10961096
(file_true, file_false) = create_config(
10971097
config, """
1098-
$MSG = json();
1098+
$MSG = {};
10991099
$MSG["foo"] = "bar";
11001100
unset($MSG["foo"]);
11011101
not isset($MSG["foo"]);
@@ -1111,7 +1111,7 @@ def test_unset_existing_key(config, syslog_ng):
11111111
def test_unset_inexisting_key(config, syslog_ng):
11121112
(file_true, file_false) = create_config(
11131113
config, """
1114-
$MSG = json();
1114+
$MSG = {};
11151115
unset($MSG["foo"]);
11161116
not isset($MSG["foo"]);
11171117
""",
@@ -1126,7 +1126,7 @@ def test_unset_inexisting_key(config, syslog_ng):
11261126
def test_setting_an_unset_key_will_contain_the_right_value(config, syslog_ng):
11271127
(file_true, file_false) = create_config(
11281128
config, """
1129-
$MSG = json();
1129+
$MSG = {};
11301130
$MSG["foo"] = "first";
11311131
unset($MSG["foo"]);
11321132
not isset($MSG["foo"]);
@@ -1143,9 +1143,9 @@ def test_setting_an_unset_key_will_contain_the_right_value(config, syslog_ng):
11431143
def test_unset(config, syslog_ng):
11441144
(file_true, file_false) = create_config(
11451145
config, """
1146-
$MSG = json();
1146+
$MSG = {};
11471147
$MSG["inner_key"] = "foo";
1148-
$arr = json_array();
1148+
$arr = [];
11491149
$arr[] = "first";
11501150
$arr[] = "second";
11511151
@@ -1206,8 +1206,8 @@ def test_strptime_failure_result(config, syslog_ng):
12061206
def test_len(config, syslog_ng):
12071207
(file_true, file_false) = create_config(
12081208
config, r"""
1209-
$dict = json();
1210-
$list = json_array();
1209+
$dict = {};
1210+
$list = [];
12111211
len($dict) == 0;
12121212
len($list) == 0;
12131213
@@ -1248,7 +1248,7 @@ def test_regexp_match(config, syslog_ng):
12481248
def test_regexp_nomatch(config, syslog_ng):
12491249
(file_true, file_false) = create_config(
12501250
config, r"""
1251-
$MSG = json();
1251+
$MSG = {};
12521252
$MSG.match = ${values.str} !~ /string/;
12531253
$MSG.match_any = ${values.str} !~ /.*/;
12541254
$MSG.nomatch = ${values.str} !~ /wrong/;
@@ -1284,7 +1284,7 @@ def test_regexp_match_error_in_pattern(config, syslog_ng):
12841284
def test_regexp_search(config, syslog_ng):
12851285
(file_true, file_false) = create_config(
12861286
config, r"""
1287-
$MSG = json();
1287+
$MSG = {};
12881288
$MSG.unnamed = regexp_search("foobarbaz", /(foo)(bar)(baz)/);
12891289
$MSG.named = regexp_search("foobarbaz", /(?<first>foo)(?<second>bar)(?<third>baz)/);
12901290
$MSG.mixed = regexp_search("foobarbaz", /(?<first>foo)(bar)(?<third>baz)/);
@@ -1322,7 +1322,7 @@ def test_regexp_search(config, syslog_ng):
13221322
def test_regexp_search_error_in_pattern(config, syslog_ng):
13231323
_ = create_config(
13241324
config, r"""
1325-
$MSG = json(regexp_search("foo", /(/));
1325+
$MSG = regexp_search("foo", /(/);
13261326
""",
13271327
)
13281328
with pytest.raises(Exception):
@@ -1412,7 +1412,7 @@ def test_parse_csv_optional_arg_columns(config, syslog_ng):
14121412
(file_true, file_false) = create_config(
14131413
config, """
14141414
custom_message = "foo,bar,baz";
1415-
cols = json_array(["1st","2nd","3rd"]);
1415+
cols = ["1st","2nd","3rd"];
14161416
$MSG = parse_csv(custom_message, columns=cols);
14171417
""",
14181418
)
@@ -1441,7 +1441,7 @@ def test_parse_csv_optional_arg_non_greedy(config, syslog_ng):
14411441
(file_true, file_false) = create_config(
14421442
config, """
14431443
custom_message = "foo,bar,baz,tik,tak,toe";
1444-
cols = json_array(["1st","2nd","3rd"]);
1444+
cols = ["1st","2nd","3rd"];
14451445
$MSG = parse_csv(custom_message, columns=cols, greedy=false);
14461446
""",
14471447
)
@@ -1456,7 +1456,7 @@ def test_parse_csv_optional_arg_greedy(config, syslog_ng):
14561456
(file_true, file_false) = create_config(
14571457
config, """
14581458
custom_message = "foo,bar,baz,tik,tak,toe";
1459-
cols = json_array(["1st","2nd","3rd","rest"]);
1459+
cols = ["1st","2nd","3rd","rest"];
14601460
$MSG = parse_csv(custom_message, columns=cols, greedy=true);
14611461
""",
14621462
)
@@ -1519,19 +1519,19 @@ def test_vars(config, syslog_ng):
15191519
def test_unset_empties(config, syslog_ng):
15201520
(file_true, file_false) = create_config(
15211521
config, r"""
1522-
dict = json({"foo": "", "bar": "-", "baz": "N/A", "almafa": null, "kortefa": {"a":{"s":{"d":{}}}}, "szilvafa": [[[]]]});
1522+
dict = {"foo": "", "bar": "-", "baz": "N/A", "almafa": null, "kortefa": {"a":{"s":{"d":{}}}}, "szilvafa": [[[]]]};
15231523
defaults_dict = dict;
15241524
explicit_dict = dict;
15251525
unset_empties(defaults_dict);
15261526
unset_empties(explicit_dict, recursive=true);
15271527
1528-
list = json_array(["", "-", "N/A", null, {"a":{"s":{"d":{}}}}, [[[]]]]);
1528+
list = ["", "-", "N/A", null, {"a":{"s":{"d":{}}}}, [[[]]]];
15291529
defaults_list = list;
15301530
explicit_list = list;
15311531
unset_empties(defaults_list);
15321532
unset_empties(explicit_list, recursive=true);
15331533
1534-
$MSG = json_array([defaults_dict, explicit_dict, defaults_list, explicit_list]);
1534+
$MSG = [defaults_dict, explicit_dict, defaults_list, explicit_list];
15351535
""",
15361536
)
15371537
syslog_ng.start(config)
@@ -1573,10 +1573,10 @@ def test_null_coalesce_use_default_on_error_and_supress_error(config, syslog_ng)
15731573
def test_null_coalesce_get_happy_paths(config, syslog_ng):
15741574
(file_true, file_false) = create_config(
15751575
config, r"""
1576-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1576+
data = {"foo":"1", "bar":"2", "baz":"3"};
15771577
def = "default";
15781578
key = "bar";
1579-
$MSG = json();
1579+
$MSG = {};
15801580
15811581
$MSG.a = data[key] ?? def;
15821582
$MSG.b = key ?? def;
@@ -1592,7 +1592,7 @@ def = "default";
15921592
def test_null_coalesce_get_subscript_error(config, syslog_ng):
15931593
(file_true, file_false) = create_config(
15941594
config, r"""
1595-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1595+
data = {"foo":"1", "bar":"2", "baz":"3"};
15961596
def = "default";
15971597
key = "missing_key";
15981598
$MSG = data[key] ?? def;
@@ -1608,7 +1608,7 @@ def = "default";
16081608
def test_null_coalesce_use_nested_coalesce(config, syslog_ng):
16091609
(file_true, file_false) = create_config(
16101610
config, r"""
1611-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1611+
data = {"foo":"1", "bar":"2", "baz":"3"};
16121612
def = "default";
16131613
key1 = "missing_key1";
16141614
key2 = "missing_key2";
@@ -1625,7 +1625,7 @@ def = "default";
16251625
def test_null_coalesce_use_nested_coalesce_return_mid_match(config, syslog_ng):
16261626
(file_true, file_false) = create_config(
16271627
config, r"""
1628-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1628+
data = {"foo":"1", "bar":"2", "baz":"3"};
16291629
def = "default";
16301630
key1 = "missing_key1";
16311631
key2 = "baz";
@@ -1642,7 +1642,7 @@ def = "default";
16421642
def test_null_coalesce_do_not_supress_last_error(config, syslog_ng):
16431643
(file_true, file_false) = create_config(
16441644
config, r"""
1645-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1645+
data = {"foo":"1", "bar":"2", "baz":"3"};
16461646
def = "default";
16471647
key1 = "missing_key1";
16481648
key2 = "missing_key2";
@@ -1659,9 +1659,9 @@ def = "default";
16591659
def test_null_coalesce_precedence_versus_ternary(config, syslog_ng):
16601660
(file_true, file_false) = create_config(
16611661
config, r"""
1662-
data = json({"foo":"1", "bar":"2", "baz":"3"});
1662+
data = {"foo":"1", "bar":"2", "baz":"3"};
16631663
def = "default";
1664-
$MSG = json();
1664+
$MSG = {};
16651665
16661666
# according to c# and python null coalesce have higher precedence
16671667
@@ -1689,7 +1689,7 @@ def = "default";
16891689

16901690
def test_slash_string_features(config, syslog_ng):
16911691
cfg = r"""
1692-
$MSG = json();
1692+
$MSG = {};
16931693
$MSG.base = /foo bar/;
16941694
$MSG.line_break = /foo
16951695
bar/;
@@ -1749,7 +1749,7 @@ def test_slash_string_features(config, syslog_ng):
17491749
def test_regexp_subst(config, syslog_ng):
17501750
(file_true, file_false) = create_config(
17511751
config, r"""
1752-
$MSG = json();
1752+
$MSG = {};
17531753
$MSG.single = regexp_subst("foobarbaz","o","");
17541754
$MSG.empty_string = regexp_subst("","a","!");
17551755
$MSG.empty_pattern = regexp_subst("foobarbaz","","!");
@@ -1798,7 +1798,7 @@ def test_regexp_subst_all_args_are_mandatory(config, syslog_ng):
17981798
def test_add_operator_for_base_types(config, syslog_ng):
17991799
(file_true, file_false) = create_config(
18001800
config, r"""
1801-
$MSG = json();
1801+
$MSG = {};
18021802
$MSG.string = "foo" + "bar" + "baz";
18031803
$MSG.bytes = string(bytes("\xCA") + bytes("\xFE"));
18041804
$MSG.datetime_integer = string(strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z") + 3600000000);
@@ -1807,11 +1807,11 @@ def test_add_operator_for_base_types(config, syslog_ng):
18071807
$MSG.integer_double = 3 + 0.5;
18081808
$MSG.double_integer = 3.5 + 2;
18091809
$MSG.double_double = 3.14 + 0.86;
1810-
js1 = json_array(["foo","bar"]);
1811-
js2 = json_array(["baz","other"]);
1810+
js1 = ["foo","bar"];
1811+
js2 = ["baz","other"];
18121812
$MSG.list_list = js1 + js2;
1813-
dict1 = json({"foo":"bar"});
1814-
dict2 = json({"baz":"other"});
1813+
dict1 = {"foo":"bar"};
1814+
dict2 = {"baz":"other"};
18151815
$MSG.dict_dict = dict1 + dict2;
18161816
""",
18171817
)
@@ -1837,15 +1837,15 @@ def test_add_operator_for_base_types(config, syslog_ng):
18371837
def test_flatten(config, syslog_ng):
18381838
(file_true, file_false) = create_config(
18391839
config, r"""
1840-
dict = json({"top_level_field":42,"top_level_dict":{"inner_field":1337,"inner_dict":{"inner_inner_field":1}}});
1840+
dict = {"top_level_field":42,"top_level_dict":{"inner_field":1337,"inner_dict":{"inner_inner_field":1}}};
18411841
18421842
default_separator = dict;
18431843
custom_separator = dict;
18441844
18451845
flatten(default_separator);
18461846
flatten(custom_separator, separator="->");
18471847
1848-
$MSG = json_array([default_separator, custom_separator]);
1848+
$MSG = [default_separator, custom_separator];
18491849
""",
18501850
)
18511851
syslog_ng.start(config)
@@ -1861,14 +1861,14 @@ def test_flatten(config, syslog_ng):
18611861
def test_add_operator_for_generators(config, syslog_ng):
18621862
(file_true, file_false) = create_config(
18631863
config, r"""
1864-
$MSG = json();
1865-
js1 = json_array(["foo","bar"]);
1866-
js2 = json_array(["baz","other"]);
1864+
$MSG = {};
1865+
js1 = ["foo","bar"];
1866+
js2 = ["baz","other"];
18671867
$MSG.list_var_gen = js1 + ["baz1","other1"];
18681868
$MSG.list_gen_var = ["foo2", "bar2"] + js2;
18691869
$MSG.list_gen_gen = ["foo3", "bar3"] + ["baz3", "other3"];
1870-
dict1 = json({"foo":{"bar":"baz"}});
1871-
dict2 = json({"tik":{"tak":"toe"}});
1870+
dict1 = {"foo":{"bar":"baz"}};
1871+
dict2 = {"tik":{"tak":"toe"}};
18721872
$MSG.dict_var_gen = dict1 + {"tik1":{"tak1":"toe1"}};
18731873
$MSG.dict_gen_var = {"foo2":{"bar2":"baz2"}} + dict2;
18741874
$MSG.dict_gen_gen = {"foo3":{"bar3":"baz3"}} + {"tik3":{"tak3":"toe3"}};
@@ -1892,9 +1892,9 @@ def test_add_operator_for_generators(config, syslog_ng):
18921892
def test_plus_equal_grammar_rules(config, syslog_ng):
18931893
(file_true, file_false) = create_config(
18941894
config, r"""
1895-
$MSG = json();
1896-
js1 = json_array(["foo","bar"]);
1897-
js2 = json_array(["baz","other"]);
1895+
$MSG = {};
1896+
js1 = ["foo","bar"];
1897+
js2 = ["baz","other"];
18981898
18991899
a = 3;
19001900
a += 2;

0 commit comments

Comments
 (0)