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:
Adam Chalmers
2024-10-15 15:58:04 -07:00
committed by GitHub
parent b4c171a347
commit fbac9935fe
3 changed files with 28 additions and 1 deletions

View File

@ -28,6 +28,18 @@ pub async fn map(exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
memory: *f.memory, memory: *f.memory,
}; };
let new_array = inner_map(array, map_fn, exec_state, &args).await?; 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); let uv = UserVal::new(vec![args.source_range.into()], new_array);
Ok(KclValue::UserVal(uv)) Ok(KclValue::UserVal(uv))
} }

View File

@ -0,0 +1,6 @@
fn increment = (i) => { return i + 1 }
xs = [0..2]
ys = xs
|> map(%, increment)
|> map(%, increment)

View File

@ -28,7 +28,15 @@ macro_rules! gen_test_fail {
async fn run(code: &str) { async fn run(code: &str) {
let (ctx, program, id_generator) = setup(code).await; 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) { 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_fail!(comparisons_multiple, "syntax: Invalid number: true");
gen_test!(add_lots); gen_test!(add_lots);
gen_test!(double_map);