Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ opentelemetry_sdk = {version = "0.32", features = ["rt-tokio"]}
opfs = "0.2.0"
percent-encoding = "2.3.2"
proptest = "1.11"
rand = "0.9.4"
rayon = "1.11.0"
regex-lite = "0.1.9"
reqwest = {version = "0.13"}
Expand Down Expand Up @@ -112,6 +113,7 @@ tracing-subscriber = "0.3.23"
url = "2.5.8"
utoipa = "5.5"
utoipa-swagger-ui = {version = "9", features = ["axum", "vendored"]}
uuid = "1.23.4"
wasm-bindgen = "0.2.100"
wasm-bindgen-futures = "0.4.55"
wasm-bindgen-test = "0.3.65"
Expand Down
8 changes: 8 additions & 0 deletions crates/mq-check/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ fn register_string(ctx: &mut InferenceContext) {
let a = ctx.fresh_var();
register_unary(ctx, "sha256", Type::Var(a), Type::String);
register_unary(ctx, "sha256", Type::None, Type::None);

// uuid/uuid_v4/uuid_v7: () -> string
register_nullary(ctx, "uuid", Type::String);
register_nullary(ctx, "uuid_v4", Type::String);
register_nullary(ctx, "uuid_v7", Type::String);
}

/// Array functions: flatten, reverse, sort, uniq, compact, len, slice, insert, range, repeat
Expand Down Expand Up @@ -1583,6 +1588,9 @@ mod tests {
#[case::base64d("base64d(\"aGVsbG8=\")", true)]
#[case::url_encode("url_encode(\"hello world\")", true)]
#[case::url_decode("url_decode(\"hello%20world\")", true)]
#[case::uuid("uuid()", true)]
#[case::uuid_v4("uuid_v4()", true)]
#[case::uuid_v7("uuid_v7()", true)]
fn test_string_encoding_functions(#[case] code: &str, #[case] should_succeed: bool) {
let result = check_types(code);
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions crates/mq-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ toon-format = { version = "0.5", default-features = false }
quick-xml = "0.40.1"
similar = "3.0.0"
ureq = { workspace = true, optional = true }
uuid = {workspace = true, features = ["v4", "v7"]}

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["console"] }
uuid = {workspace = true, features = ["js"]}

[features]
ast-json = ["smallvec/serde", "smol_str/serde"]
Expand Down
90 changes: 90 additions & 0 deletions crates/mq-lang/src/eval/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,21 @@ fn sha512_impl(ident: &Ident, _: &RuntimeValue, mut args: Args, _: &SharedEnv) -
}
}

#[mq_macros::mq_fn(name = "uuid", params = None)]
fn uuid_impl(_: &Ident, _: &RuntimeValue, _: Args, _: &SharedEnv) -> Result<RuntimeValue, Error> {
Ok(RuntimeValue::String(uuid::Uuid::new_v4().to_string()))
}

#[mq_macros::mq_fn(name = "uuid_v7", params = None)]
fn uuid_v7_impl(_: &Ident, _: &RuntimeValue, _: Args, _: &SharedEnv) -> Result<RuntimeValue, Error> {
Ok(RuntimeValue::String(uuid::Uuid::now_v7().to_string()))
}

#[mq_macros::mq_fn(name = "uuid_v4", params = None)]
fn uuid_v4_impl(_: &Ident, _: &RuntimeValue, _: Args, _: &SharedEnv) -> Result<RuntimeValue, Error> {
Ok(RuntimeValue::String(uuid::Uuid::new_v4().to_string()))
}

#[mq_macros::mq_fn(name = "from_hex", params = Fixed(1))]
fn from_hex_impl(ident: &Ident, _: &RuntimeValue, mut args: Args, _: &SharedEnv) -> Result<RuntimeValue, Error> {
match args.as_mut_slice() {
Expand Down Expand Up @@ -3913,6 +3928,9 @@ mq_macros::builtin_dispatch! {
MD5,
SHA256,
SHA512,
UUID,
UUID_V4,
UUID_V7,
MIN,
MAX,
FROM_HTML,
Expand Down Expand Up @@ -4901,6 +4919,27 @@ pub static BUILTIN_FUNCTION_DOC: LazyLock<FxHashMap<SmolStr, BuiltinFunctionDoc>
params: &["input"],
},
);
map.insert(
SmolStr::new("uuid"),
BuiltinFunctionDoc {
description: "Generates a random (version 4, RFC 4122) UUID string.",
params: &[],
},
);
map.insert(
SmolStr::new("uuid_v4"),
BuiltinFunctionDoc {
description: "Generates a random (version 4, RFC 4122) UUID string. Alias of `uuid`.",
params: &[],
},
);
map.insert(
SmolStr::new("uuid_v7"),
BuiltinFunctionDoc {
description: "Generates a time-ordered (version 7, RFC 9562) UUID string: a millisecond Unix timestamp followed by random bits, so values sort by creation time. The timestamp is plaintext, so prefer uuid/uuid_v4 for unguessable IDs.",
params: &[],
},
);
map.insert(
SmolStr::new("to_text"),
BuiltinFunctionDoc {
Expand Down Expand Up @@ -6292,6 +6331,57 @@ mod tests {
assert_eq!(result, RuntimeValue::Number(expected.into()));
}

fn call_uuid_fn(name: &str) -> String {
let env = Shared::new(SharedCell::new(Env::default()));
match eval_builtin(&RuntimeValue::None, &Ident::new(name), vec![], &env).unwrap() {
RuntimeValue::String(s) => s.to_string(),
other => panic!("{name} should return a string, got {other:?}"),
}
}

fn assert_uuid_shape(uuid: &str, expected_version: char) {
let parts: Vec<&str> = uuid.split('-').collect();
assert_eq!(parts.len(), 5, "uuid {uuid} should have 5 hyphen-separated groups");
assert_eq!(
[
parts[0].len(),
parts[1].len(),
parts[2].len(),
parts[3].len(),
parts[4].len()
],
[8, 4, 4, 4, 12]
);
assert!(uuid.chars().all(|c| c == '-' || c.is_ascii_hexdigit()));
assert_eq!(parts[2].chars().next().unwrap(), expected_version, "version nibble");
assert!(
matches!(parts[3].chars().next().unwrap(), '8' | '9' | 'a' | 'b'),
"variant nibble should be 10xxxxxx, got {}",
parts[3]
);
}

#[test]
fn test_uuid_is_version_4() {
assert_uuid_shape(&call_uuid_fn("uuid"), '4');
}

#[test]
fn test_uuid_v4_is_version_4() {
assert_uuid_shape(&call_uuid_fn("uuid_v4"), '4');
}

#[test]
fn test_uuid_v7_is_version_7() {
assert_uuid_shape(&call_uuid_fn("uuid_v7"), '7');
}

#[test]
fn test_uuid_calls_are_unique() {
let values: std::collections::HashSet<String> = (0..200).map(|_| call_uuid_fn("uuid")).collect();
assert_eq!(values.len(), 200, "uuid() should not repeat across calls");
}

#[rstest]
#[case(1704067200_i64, "%Y-%m-%d", "2024-01-01")]
#[case(0_i64, "%Y-%m-%dT%H:%M:%S", "1970-01-01T00:00:00")]
Expand Down
Loading