Skip to content

Commit

Permalink
Bugfix: double map (#4169)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adamchalmers authored and lf94 committed Oct 16, 2024
1 parent fa527ec commit 6ebfe59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/wasm-lib/kcl/src/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
6 changes: 6 additions & 0 deletions src/wasm-lib/tests/executor/inputs/no_visuals/double_map.kcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn increment = (i) => { return i + 1 }

xs = [0..2]
ys = xs
|> map(%, increment)
|> map(%, increment)
11 changes: 10 additions & 1 deletion src/wasm-lib/tests/executor/no_visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

0 comments on commit 6ebfe59

Please sign in to comment.