Bugfix: double map (#4169)
Previously, map was wrapping KCL values in a JSON object unnecessarily. The new `double_map` test would emit this error: ``` Syntax(KclErrorDetails { source_ranges: [SourceRange([31, 32])], message: "Invalid number: {\"type\":\"UserVal\",\"value\":1.0,\"__meta\":[{\"sourceRange\":[31,36]}]}" }) ``` In other words, the second `map` statement is being passed an array of JSON STRINGS, not an array of numbers. The strings contain JSON stringified representations of user values which are numbers. Bug is now fixed.
This commit is contained in:
@ -28,6 +28,18 @@ pub async fn map(exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
|
||||
memory: *f.memory,
|
||||
};
|
||||
let new_array = inner_map(array, map_fn, exec_state, &args).await?;
|
||||
let unwrapped = new_array
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|k| match k {
|
||||
KclValue::UserVal(user_val) => Ok(user_val.value),
|
||||
_ => Err(()),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>();
|
||||
if let Ok(unwrapped) = unwrapped {
|
||||
let uv = UserVal::new(vec![args.source_range.into()], unwrapped);
|
||||
return Ok(KclValue::UserVal(uv));
|
||||
}
|
||||
let uv = UserVal::new(vec![args.source_range.into()], new_array);
|
||||
Ok(KclValue::UserVal(uv))
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
fn increment = (i) => { return i + 1 }
|
||||
|
||||
xs = [0..2]
|
||||
ys = xs
|
||||
|> map(%, increment)
|
||||
|> map(%, increment)
|
@ -28,7 +28,15 @@ macro_rules! gen_test_fail {
|
||||
async fn run(code: &str) {
|
||||
let (ctx, program, id_generator) = setup(code).await;
|
||||
|
||||
ctx.run(&program, None, id_generator).await.unwrap();
|
||||
let res = ctx.run(&program, None, id_generator).await;
|
||||
match res {
|
||||
Ok(state) => {
|
||||
println!("{:#?}", state.memory);
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("{e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn setup(program: &str) -> (ExecutorContext, Program, IdGenerator) {
|
||||
@ -102,3 +110,4 @@ gen_test!(if_else);
|
||||
// );
|
||||
gen_test_fail!(comparisons_multiple, "syntax: Invalid number: true");
|
||||
gen_test!(add_lots);
|
||||
gen_test!(double_map);
|
||||
|
Reference in New Issue
Block a user