Skip to content

Commit

Permalink
feat: support uuid function
Browse files Browse the repository at this point in the history
  • Loading branch information
pdiazvargas committed Oct 31, 2024
1 parent cb69614 commit 817d12e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_json = "1.0.117"
rand = "0.8.5"
regex = "1.11.1"
num-format = "0.4.4"
uuid = { version = "1.8.0", features = ["fast-rng", "v4", "v7"] }

[dev-dependencies]
test-case = "3.3.1"
Expand Down
13 changes: 13 additions & 0 deletions src/evaluator/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use regex::Regex;
use std::borrow::{Borrow, Cow};
use std::collections::HashSet;
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

use crate::datetime::{format_custom_date, parse_custom_format, parse_timezone_offset};
use crate::parser::expressions::check_balanced_brackets;
Expand Down Expand Up @@ -1079,6 +1080,18 @@ pub fn fn_millis<'a>(
))
}

pub fn fn_uuid<'a>(
context: FunctionContext<'a, '_>,
args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
max_args!(context, args, 0);

Ok(Value::string(
&context.arena,
Uuid::new_v4().to_string().as_str(),
))
}

pub fn to_millis<'a>(
context: FunctionContext<'a, '_>,
args: &[&'a Value<'a>],
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl<'a> JsonAta<'a> {
bind_native!("uppercase", 1, fn_uppercase);
bind_native!("zip", 1, fn_zip);
bind_native!("millis", 0, fn_millis);
bind_native!("uuid", 0, fn_uuid);

let chain_ast = Some(parser::parse(
"function($f, $g) { function($x){ $g($f($x)) } }",
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/groups/function-uuid/case000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$uuid()",
"dataset": null,
"bindings": {},
"result_re": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
}

0 comments on commit 817d12e

Please sign in to comment.