Skip to content

Commit

Permalink
clean up and add docs
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Steensen <luke.steensen@gmail.com>
  • Loading branch information
lukesteensen committed May 27, 2021
1 parent 60b02e9 commit cec3b00
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
35 changes: 35 additions & 0 deletions docs/reference/remap/functions/match_any.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package metadata

remap: functions: match_any: {
category: "String"
description: """
Determines whether the `value` matches any the given `patterns`.
"""

arguments: [
{
name: "value"
description: "The value to match."
required: true
type: ["string"]
},
{
name: "patterns"
description: "The array of regular expression patterns to match against."
required: true
type: ["array"]
},
]
internal_failure_reasons: []
return: types: ["boolean"]

examples: [
{
title: "Regex match on a string"
source: """
match_any("I'm a little teapot", [r'frying pan', r'teapot'])
"""
return: true
},
]
}
2 changes: 2 additions & 0 deletions lib/vrl/stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ default = [
"length",
"log",
"match",
"match_any",
"md5",
"merge",
"now",
Expand Down Expand Up @@ -180,6 +181,7 @@ join = []
length = []
log = ["tracing"]
match = ["regex"]
match_any = ["regex"]
md5 = ["md-5", "hex"]
merge = []
now = ["chrono"]
Expand Down
10 changes: 10 additions & 0 deletions lib/vrl/stdlib/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ criterion_group!(
length,
log,
r#match,
match_any,
md5,
merge,
// TODO: value is dynamic so we cannot assert equality
Expand Down Expand Up @@ -589,6 +590,15 @@ bench_function! {
}
}

bench_function! {
match_any => vrl_stdlib::MatchAny;

simple {
args: func_args![value: "foo 2 bar", patterns: vec![Regex::new(r"foo \d bar").unwrap()]],
want: Ok(true),
}
}

bench_function! {
md5 => vrl_stdlib::Md5;

Expand Down
4 changes: 2 additions & 2 deletions lib/vrl/stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod log;
mod log_util;
#[cfg(feature = "match")]
mod r#match;
#[cfg(feature = "match")]
#[cfg(feature = "match_any")]
mod match_any;
#[cfg(feature = "md5")]
mod md5;
Expand Down Expand Up @@ -291,7 +291,7 @@ pub use join::Join;
pub use length::Length;
#[cfg(feature = "log")]
pub use log::Log;
#[cfg(feature = "match")]
#[cfg(feature = "match_any")]
pub use match_any::MatchAny;
#[cfg(feature = "merge")]
pub use merge::Merge;
Expand Down
5 changes: 3 additions & 2 deletions lib/vrl/stdlib/src/match_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl Function for MatchAny {
expr,
})?;

// TODO: Why does `?` not work here?
let re = value.try_regex().unwrap();
let re = value
.try_regex()
.map_err(|e| Box::new(e) as Box<dyn DiagnosticError>)?;
re_strings.push(re.to_string());
}

Expand Down

0 comments on commit cec3b00

Please sign in to comment.