Skip to content

Commit

Permalink
fix : support list/dict subscript format in KCL string
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverRaR committed Dec 8, 2022
1 parent 11758de commit 586aaa8
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions kclvm/runtime/src/value/val_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,15 +911,15 @@ impl FormatString {
match name_part {
// Load attr
FieldNamePart::Attribute(attr) => {
argument = args.dict_get_value(attr.as_str()).unwrap().clone();
argument = argument.dict_get_value(attr.as_str()).unwrap().clone();
}
// List subscript
FieldNamePart::Index(index) => {
argument = args.list_get(index as isize).unwrap().clone();
argument = argument.list_get(index as isize).unwrap().clone();
}
// Dict subscript
FieldNamePart::StringIndex(value) => {
argument = args.dict_get_value(value.as_str()).unwrap().clone();
argument = argument.dict_get_value(value.as_str()).unwrap().clone();
}
}
}
Expand Down Expand Up @@ -1057,3 +1057,29 @@ impl ValueRef {
}
}
}

#[cfg(test)]
mod test_value_fmt {
use crate::*;

#[test]
fn test_string_format() {
let cases = [
(r#""{} {}""#, r#"["Hello","World"]"#, "\"Hello World\""),
(r#""{:.0f}""#, r#"[1.0]"#, "\"1\""),
(r#""{:.1f} {:.0f}""#, r#"[1.0,2.0]"#, "\"1.0 2\""),
(
r#""0{0[0]}, 1{0[1]}, Hello{1[Hello]}""#,
r#"[["0","1"],{ "Hello": "World" }]"#,
"\"00, 11, HelloWorld\"",
),
];
for (format_string, args, expected) in cases {
let format_string = FormatString::from_str(format_string).unwrap();
let args = ValueRef::from_json(args).unwrap();
let kwargs = ValueRef::dict(None);
let result = format_string.format(&args, &kwargs);
assert_eq!(&result, expected)
}
}
}

0 comments on commit 586aaa8

Please sign in to comment.