Skip to content

Commit 86eb58f

Browse files
authored
chore: Remove map! macro (#12716)
* Remove map! macro As discussed in #12695 one of the blockers to adjusting the `Value` type systemtically is the difficult compiler output around our testing macros. This commit removes one of those macros -- map! -- in favor of `BTreeMap::from` for the most part. Signed-off-by: Brian L. Troutwine <brian@troutwine.us> * resolve clippy dings Signed-off-by: Brian L. Troutwine <brian@troutwine.us>
1 parent 8228507 commit 86eb58f

File tree

10 files changed

+260
-224
lines changed

10 files changed

+260
-224
lines changed

lib/vrl/compiler/src/test_util.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,6 @@ macro_rules! __prep_bench_or_test {
169169
}};
170170
}
171171

172-
#[macro_export]
173-
macro_rules! map {
174-
() => (
175-
::std::collections::BTreeMap::new()
176-
);
177-
($($k:tt: $v:expr),+ $(,)?) => {
178-
vec![$(($k.into(), $v.into())),+]
179-
.into_iter()
180-
.collect::<::std::collections::BTreeMap<_, _>>()
181-
};
182-
}
183-
184172
#[macro_export]
185173
macro_rules! type_def {
186174
(unknown) => {

lib/vrl/stdlib/src/compact.rs

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,17 @@ mod test {
336336
Default::default(),
337337
),
338338
(
339-
vec![1.into(), Value::Object(map!["field2": 2]), 2.into()],
339+
vec![
340+
Value::from(1),
341+
Value::Object(BTreeMap::from([(String::from("field2"), Value::from(2))])),
342+
Value::from(2),
343+
],
340344
vec![
341345
1.into(),
342-
Value::Object(map!["field1": Value::Null,
343-
"field2": 2]),
346+
Value::Object(BTreeMap::from([
347+
(String::from("field1"), Value::Null),
348+
(String::from("field2"), Value::from(2)),
349+
])),
344350
2.into(),
345351
],
346352
Default::default(),
@@ -383,43 +389,63 @@ mod test {
383389
Default::default(),
384390
),
385391
(
386-
map!["key1": Value::from(1),
387-
"key2": Value::Object(map!["key2": Value::from(3)]),
388-
"key3": Value::from(2),
389-
],
390-
map![
391-
"key1": Value::from(1),
392-
"key2": Value::Object(map!["key1": Value::Null,
393-
"key2": Value::from(3),
394-
"key3": Value::Null]),
395-
"key3": Value::from(2),
396-
],
392+
BTreeMap::from([
393+
(String::from("key1"), Value::from(1)),
394+
(
395+
String::from("key2"),
396+
Value::Object(BTreeMap::from([(String::from("key2"), Value::from(3))])),
397+
),
398+
(String::from("key3"), Value::from(2)),
399+
]),
400+
BTreeMap::from([
401+
(String::from("key1"), Value::from(1)),
402+
(
403+
String::from("key2"),
404+
Value::Object(BTreeMap::from([
405+
(String::from("key1"), Value::Null),
406+
(String::from("key2"), Value::from(3)),
407+
(String::from("key3"), Value::Null),
408+
])),
409+
),
410+
(String::from("key3"), Value::from(2)),
411+
]),
397412
Default::default(),
398413
),
399414
(
400-
map!["key1": Value::from(1),
401-
"key2": Value::Object(map!["key1": Value::Null,]),
402-
"key3": Value::from(2),
403-
],
404-
map![
405-
"key1": Value::from(1),
406-
"key2": Value::Object(map!["key1": Value::Null,]),
407-
"key3": Value::from(2),
408-
],
415+
BTreeMap::from([
416+
(String::from("key1"), Value::from(1)),
417+
(
418+
String::from("key2"),
419+
Value::Object(BTreeMap::from([(String::from("key1"), Value::Null)])),
420+
),
421+
(String::from("key3"), Value::from(2)),
422+
]),
423+
BTreeMap::from([
424+
(String::from("key1"), Value::from(1)),
425+
(
426+
String::from("key2"),
427+
Value::Object(BTreeMap::from([(String::from("key1"), Value::Null)])),
428+
),
429+
(String::from("key3"), Value::from(2)),
430+
]),
409431
CompactOptions {
410432
recursive: false,
411433
..Default::default()
412434
},
413435
),
414436
(
415-
map!["key1": Value::from(1),
416-
"key3": Value::from(2),
417-
],
418-
map![
419-
"key1": Value::from(1),
420-
"key2": Value::Object(map!["key1": Value::Null,]),
421-
"key3": Value::from(2),
422-
],
437+
BTreeMap::from([
438+
(String::from("key1"), Value::from(1)),
439+
(String::from("key3"), Value::from(2)),
440+
]),
441+
BTreeMap::from([
442+
(String::from("key1"), Value::from(1)),
443+
(
444+
String::from("key2"),
445+
Value::Object(BTreeMap::from([(String::from("key1"), Value::Null)])),
446+
),
447+
(String::from("key3"), Value::from(2)),
448+
]),
423449
Default::default(),
424450
),
425451
(
@@ -446,11 +472,8 @@ mod test {
446472
compact => Compact;
447473

448474
with_map {
449-
args: func_args![value: map!["key1": Value::Null,
450-
"key2": 1,
451-
"key3": "",
452-
]],
453-
want: Ok(Value::Object(map!["key2": 1])),
475+
args: func_args![value: Value::from(BTreeMap::from([(String::from("key1"), Value::Null), (String::from("key2"), Value::from(1)), (String::from("key3"), Value::from(""))]))],
476+
want: Ok(Value::Object(BTreeMap::from([(String::from("key2"), Value::from(1))]))),
454477
tdef: TypeDef::object(Collection::any()),
455478
}
456479

@@ -469,7 +492,7 @@ mod test {
469492
},
470493
nullish: true
471494
],
472-
want: Ok(Value::Object(map!["key2": 1])),
495+
want: Ok(Value::Object(BTreeMap::from([(String::from("key2"), Value::from(1))]))),
473496
tdef: TypeDef::object(Collection::any()),
474497
}
475498
];

lib/vrl/stdlib/src/encode_json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod tests {
101101
}
102102

103103
map {
104-
args: func_args![value: map!["field": "value"]],
104+
args: func_args![value: Value::from(BTreeMap::from([(String::from("field"), Value::from("value"))]))],
105105
want: Ok(r#"{"field":"value"}"#),
106106
tdef: TypeDef::bytes().infallible(),
107107
}

lib/vrl/stdlib/src/parse_apache_log.rs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -166,55 +166,46 @@ impl Expression for ParseApacheLogFn {
166166
}
167167

168168
fn kind_common() -> BTreeMap<Field, Kind> {
169-
map! {
170-
"host": Kind::bytes() | Kind::null(),
171-
"identity": Kind::bytes() | Kind::null(),
172-
"user": Kind::bytes() | Kind::null(),
173-
"timestamp": Kind::timestamp() | Kind::null(),
174-
"message": Kind::bytes() | Kind::null(),
175-
"method": Kind::bytes() | Kind::null(),
176-
"path": Kind::bytes() | Kind::null(),
177-
"protocol": Kind::bytes() | Kind::null(),
178-
"status": Kind::integer() | Kind::null(),
179-
"size": Kind::integer() | Kind::null(),
180-
}
181-
.into_iter()
182-
.map(|(key, kind): (&str, _)| (key.into(), kind))
183-
.collect()
169+
BTreeMap::from([
170+
(Field::from("host"), Kind::bytes() | Kind::null()),
171+
(Field::from("identity"), Kind::bytes() | Kind::null()),
172+
(Field::from("user"), Kind::bytes() | Kind::null()),
173+
(Field::from("timestamp"), Kind::timestamp() | Kind::null()),
174+
(Field::from("message"), Kind::bytes() | Kind::null()),
175+
(Field::from("method"), Kind::bytes() | Kind::null()),
176+
(Field::from("path"), Kind::bytes() | Kind::null()),
177+
(Field::from("protocol"), Kind::bytes() | Kind::null()),
178+
(Field::from("status"), Kind::integer() | Kind::null()),
179+
(Field::from("size"), Kind::integer() | Kind::null()),
180+
])
184181
}
185182

186183
fn kind_combined() -> BTreeMap<Field, Kind> {
187-
map! {
188-
"host": Kind::bytes() | Kind::null(),
189-
"identity": Kind::bytes() | Kind::null(),
190-
"user": Kind::bytes() | Kind::null(),
191-
"timestamp": Kind::timestamp() | Kind::null(),
192-
"message": Kind::bytes() | Kind::null(),
193-
"method": Kind::bytes() | Kind::null(),
194-
"path": Kind::bytes() | Kind::null(),
195-
"protocol": Kind::bytes() | Kind::null(),
196-
"status": Kind::integer() | Kind::null(),
197-
"size": Kind::integer() | Kind::null(),
198-
"referrer": Kind::bytes() | Kind::null(),
199-
"agent": Kind::bytes() | Kind::null(),
200-
}
201-
.into_iter()
202-
.map(|(key, kind): (&str, _)| (key.into(), kind))
203-
.collect()
184+
BTreeMap::from([
185+
(Field::from("host"), Kind::bytes() | Kind::null()),
186+
(Field::from("identity"), Kind::bytes() | Kind::null()),
187+
(Field::from("user"), Kind::bytes() | Kind::null()),
188+
(Field::from("timestamp"), Kind::timestamp() | Kind::null()),
189+
(Field::from("message"), Kind::bytes() | Kind::null()),
190+
(Field::from("method"), Kind::bytes() | Kind::null()),
191+
(Field::from("path"), Kind::bytes() | Kind::null()),
192+
(Field::from("protocol"), Kind::bytes() | Kind::null()),
193+
(Field::from("status"), Kind::integer() | Kind::null()),
194+
(Field::from("size"), Kind::integer() | Kind::null()),
195+
(Field::from("referrer"), Kind::bytes() | Kind::null()),
196+
(Field::from("agent"), Kind::bytes() | Kind::null()),
197+
])
204198
}
205199

206200
fn kind_error() -> BTreeMap<Field, Kind> {
207-
map! {
208-
"timestamp": Kind::timestamp() | Kind::null(),
209-
"module": Kind::bytes() | Kind::null(),
210-
"severity": Kind::bytes() | Kind::null(),
211-
"thread": Kind::bytes() | Kind::null(),
212-
"port": Kind::bytes() | Kind::null(),
213-
"message": Kind::bytes() | Kind::null(),
214-
}
215-
.into_iter()
216-
.map(|(key, kind): (&str, _)| (key.into(), kind))
217-
.collect()
201+
BTreeMap::from([
202+
(Field::from("timestamp"), Kind::timestamp() | Kind::null()),
203+
(Field::from("module"), Kind::bytes() | Kind::null()),
204+
(Field::from("severity"), Kind::bytes() | Kind::null()),
205+
(Field::from("thread"), Kind::bytes() | Kind::null()),
206+
(Field::from("port"), Kind::bytes() | Kind::null()),
207+
(Field::from("message"), Kind::bytes() | Kind::null()),
208+
])
218209
}
219210

220211
#[cfg(test)]

lib/vrl/stdlib/src/parse_aws_alb_log.rs

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,42 +82,57 @@ impl Expression for ParseAwsAlbLogFn {
8282
}
8383

8484
fn inner_kind() -> BTreeMap<Field, Kind> {
85-
map! {
86-
"actions_executed": Kind::bytes() | Kind::null(),
87-
"chosen_cert_arn": Kind::bytes() | Kind::null(),
88-
"classification_reason": Kind::bytes() | Kind::null(),
89-
"classification": Kind::bytes() | Kind::null(),
90-
"client_host": Kind::bytes(),
91-
"domain_name": Kind::bytes() | Kind::null(),
92-
"elb_status_code": Kind::bytes(),
93-
"elb": Kind::bytes(),
94-
"error_reason": Kind::bytes() | Kind::null(),
95-
"matched_rule_priority": Kind::bytes() | Kind::null(),
96-
"received_bytes": Kind::integer(),
97-
"redirect_url": Kind::bytes() | Kind::null(),
98-
"request_creation_time": Kind::bytes(),
99-
"request_method": Kind::bytes(),
100-
"request_processing_time": Kind::float(),
101-
"request_protocol": Kind::bytes(),
102-
"request_url": Kind::bytes(),
103-
"response_processing_time": Kind::float(),
104-
"sent_bytes": Kind::integer(),
105-
"ssl_cipher": Kind::bytes() | Kind::null(),
106-
"ssl_protocol": Kind::bytes() | Kind::null(),
107-
"target_group_arn": Kind::bytes(),
108-
"target_host": Kind::bytes() | Kind::null(),
109-
"target_port_list": Kind::bytes() | Kind::null(),
110-
"target_processing_time": Kind::float(),
111-
"target_status_code_list": Kind::bytes() | Kind::null(),
112-
"target_status_code": Kind::bytes() | Kind::null(),
113-
"timestamp": Kind::bytes(),
114-
"trace_id": Kind::bytes(),
115-
"type": Kind::bytes(),
116-
"user_agent": Kind::bytes(),
117-
}
118-
.into_iter()
119-
.map(|(key, kind): (&str, _)| (key.into(), kind))
120-
.collect()
85+
BTreeMap::from([
86+
(
87+
Field::from("actions_executed"),
88+
Kind::bytes() | Kind::null(),
89+
),
90+
(Field::from("chosen_cert_arn"), Kind::bytes() | Kind::null()),
91+
(
92+
Field::from("classification_reason"),
93+
Kind::bytes() | Kind::null(),
94+
),
95+
(Field::from("classification"), Kind::bytes() | Kind::null()),
96+
(Field::from("client_host"), Kind::bytes()),
97+
(Field::from("domain_name"), Kind::bytes() | Kind::null()),
98+
(Field::from("elb_status_code"), Kind::bytes()),
99+
(Field::from("elb"), Kind::bytes()),
100+
(Field::from("error_reason"), Kind::bytes() | Kind::null()),
101+
(
102+
Field::from("matched_rule_priority"),
103+
Kind::bytes() | Kind::null(),
104+
),
105+
(Field::from("received_bytes"), Kind::integer()),
106+
(Field::from("redirect_url"), Kind::bytes() | Kind::null()),
107+
(Field::from("request_creation_time"), Kind::bytes()),
108+
(Field::from("request_method"), Kind::bytes()),
109+
(Field::from("request_processing_time"), Kind::float()),
110+
(Field::from("request_protocol"), Kind::bytes()),
111+
(Field::from("request_url"), Kind::bytes()),
112+
(Field::from("response_processing_time"), Kind::float()),
113+
(Field::from("sent_bytes"), Kind::integer()),
114+
(Field::from("ssl_cipher"), Kind::bytes() | Kind::null()),
115+
(Field::from("ssl_protocol"), Kind::bytes() | Kind::null()),
116+
(Field::from("target_group_arn"), Kind::bytes()),
117+
(Field::from("target_host"), Kind::bytes() | Kind::null()),
118+
(
119+
Field::from("target_port_list"),
120+
Kind::bytes() | Kind::null(),
121+
),
122+
(Field::from("target_processing_time"), Kind::float()),
123+
(
124+
Field::from("target_status_code_list"),
125+
Kind::bytes() | Kind::null(),
126+
),
127+
(
128+
Field::from("target_status_code"),
129+
Kind::bytes() | Kind::null(),
130+
),
131+
(Field::from("timestamp"), Kind::bytes()),
132+
(Field::from("trace_id"), Kind::bytes()),
133+
(Field::from("type"), Kind::bytes()),
134+
(Field::from("user_agent"), Kind::bytes()),
135+
])
121136
}
122137

123138
fn parse_log(mut input: &str) -> Result<Value> {

0 commit comments

Comments
 (0)