Skip to content

Commit

Permalink
fix: runtime union type parse and checking
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Oct 18, 2023
1 parent 834c7b5 commit 272ab92
Showing 1 changed file with 12 additions and 2 deletions.
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

0 comments on commit 272ab92

Please sign in to comment.