Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: runtime union type parse and checking #791

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
14 changes: 12 additions & 2 deletions kclvm/runtime/src/value/val_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,15 @@ fn is_number_multiplier_literal_type(tpe: &str) -> bool {
}
}

#[inline]
fn ty_str_strip(ty_str: &str) -> &str {
// Empty and tab chars.
let chars = " \t";
ty_str.trim_matches(|c| chars.contains(c))
}

/// separate_kv split the union type and do not split '|' in dict and list
/// e.g., "int|str" -> vec!["int", "str"]
/// e.g., "int|str" -> vec!["int", "str"], "int | str" -> vec!["int", "str"]
pub fn split_type_union(tpe: &str) -> Vec<&str> {
let mut i = 0;
let mut s_index = 0;
Expand Down Expand Up @@ -645,7 +652,8 @@ pub fn split_type_union(tpe: &str) -> Vec<&str> {
i += 1;
}
types.push(&tpe[s_index..]);
types
// Remove empty and tab chars in the type string.
types.iter().map(|ty| ty_str_strip(ty)).collect()
}

/// separate_kv function separates key_type and value_type in the dictionary type strings,
Expand Down Expand Up @@ -954,7 +962,9 @@ mod test_value_type {
let cases = [
("", vec![""]),
("str|int", vec!["str", "int"]),
("str | int", vec!["str", "int"]),
("str|int|bool", vec!["str", "int", "bool"]),
("str | int | bool", vec!["str", "int", "bool"]),
("str|[str]", vec!["str", "[str]"]),
("str|{str:int}", vec!["str", "{str:int}"]),
("A|B|C", vec!["A", "B", "C"]),
Expand Down
Loading