feat(pg,wasm): map strings and lists across the JSON boundary
public
feat(pg,wasm): map strings and lists across the JSON boundary The core engine now produces String and List values, so both target wrappers must serialise them or "same core, same result" breaks. Extend `value_to_json` in feel-pg and feel-wasm to emit JSON strings and arrays, and replace the ad-hoc per-type context mapping with a shared recursive `json_to_value` (numbers, booleans, strings, arrays -> list, null; nested objects -> null since there is no context/map value type in this slice). Add feel-pg pg_test cases for `if/then/else`, a `for` loop returning a jsonb array, and string comparison; extend smoke.mjs with the same expressions so all three targets are cross-validated on the widened grammar. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | 4b18bb338a60b7f5c7c65fd74b3d92196b348217 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | da561bfa6b66 |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-pg/src/lib.rs +43 −13
| @@ -37,6 +37,8 @@ | ||
| 37 | 37 | match value { |
| 38 | 38 | Value::Number(n) => number_to_json(*n), |
| 39 | 39 | Value::Boolean(b) => serde_json::Value::Bool(*b), |
| 40 | + Value::String(s) => serde_json::Value::String(s.clone()), | |
| 41 | + Value::List(items) => serde_json::Value::Array(items.iter().map(value_to_json).collect()), | |
| 40 | 42 | Value::Null => serde_json::Value::Null, |
| 41 | 43 | } |
| 42 | 44 | } |
| @@ -51,29 +53,32 @@ | ||
| 51 | 53 | } |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | -/// Build a [`Context`] from a JSON object. Supported scalar value types are | |
| 55 | -/// number, boolean and null; anything else is currently ignored (out of slice | |
| 56 | -/// scope). | |
| 56 | +/// Build a [`Context`] from a JSON object, mapping each member value with | |
| 57 | +/// [`json_to_value`]. | |
| 57 | 58 | fn context_from_json(json: &serde_json::Value) -> Result<Context, String> { |
| 58 | 59 | let mut ctx = Context::new(); |
| 59 | 60 | match json { |
| 60 | 61 | serde_json::Value::Object(map) => { |
| 61 | 62 | for (key, val) in map { |
| 62 | - match val { | |
| 63 | - serde_json::Value::Number(num) => { | |
| 64 | - if let Some(f) = num.as_f64() { | |
| 65 | - ctx.insert(key.clone(), Value::Number(f)); | |
| 66 | - } | |
| 67 | - } | |
| 68 | - serde_json::Value::Bool(b) => ctx.insert(key.clone(), Value::Boolean(*b)), | |
| 69 | - serde_json::Value::Null => ctx.insert(key.clone(), Value::Null), | |
| 70 | - _ => { /* strings/arrays/objects are out of scope for now */ } | |
| 71 | - } | |
| 63 | + ctx.insert(key.clone(), json_to_value(val)); | |
| 72 | 64 | } |
| 73 | 65 | Ok(ctx) |
| 74 | 66 | } |
| 75 | 67 | serde_json::Value::Null => Ok(ctx), |
| 76 | 68 | other => Err(format!("expected a JSON object, got {other}")), |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +/// Map a JSON value to a `feel-core` [`Value`]. Numbers, booleans, strings, | |
| 73 | +/// arrays (-> list) and null are supported; nested objects map to null (there is | |
| 74 | +/// no context/map value type in this slice). | |
| 75 | +fn json_to_value(json: &serde_json::Value) -> Value { | |
| 76 | + match json { | |
| 77 | + serde_json::Value::Number(num) => num.as_f64().map(Value::Number).unwrap_or(Value::Null), | |
| 78 | + serde_json::Value::Bool(b) => Value::Boolean(*b), | |
| 79 | + serde_json::Value::String(s) => Value::String(s.clone()), | |
| 80 | + serde_json::Value::Array(arr) => Value::List(arr.iter().map(json_to_value).collect()), | |
| 81 | + serde_json::Value::Null | serde_json::Value::Object(_) => Value::Null, | |
| 77 | 82 | } |
| 78 | 83 | } |
| 79 | 84 | |
| @@ -104,6 +109,31 @@ | ||
| 104 | 109 | .expect("SPI query failed"); |
| 105 | 110 | assert_eq!(matched, Some(true)); |
| 106 | 111 | } |
| 112 | + | |
| 113 | + #[pg_test] | |
| 114 | + fn if_then_else_branches() { | |
| 115 | + let matched = | |
| 116 | + Spi::get_one::<bool>("SELECT feel_eval('if 1 < 2 then 10 else 20') = '10'::jsonb") | |
| 117 | + .expect("SPI query failed"); | |
| 118 | + assert_eq!(matched, Some(true)); | |
| 119 | + } | |
| 120 | + | |
| 121 | + #[pg_test] | |
| 122 | + fn for_loop_returns_jsonb_array() { | |
| 123 | + let matched = | |
| 124 | + Spi::get_one::<bool>("SELECT feel_eval('for i in [1,2,3] return i + 1') = '[2,3,4]'::jsonb") | |
| 125 | + .expect("SPI query failed"); | |
| 126 | + assert_eq!(matched, Some(true)); | |
| 127 | + } | |
| 128 | + | |
| 129 | + #[pg_test] | |
| 130 | + fn string_comparison_is_lexicographic() { | |
| 131 | + let matched = Spi::get_one::<bool>( | |
| 132 | + r#"SELECT feel_eval('"apple" < "banana"') = 'true'::jsonb"#, | |
| 133 | + ) | |
| 134 | + .expect("SPI query failed"); | |
| 135 | + assert_eq!(matched, Some(true)); | |
| 136 | + } | |
| 107 | 137 | } |
| 108 | 138 | |
| 109 | 139 | /// Required pgrx test harness hooks. |
modified crates/feel-wasm/smoke.mjs +24 −16
| @@ -5,25 +5,33 @@ | ||
| 5 | 5 | // Then run: |
| 6 | 6 | // node crates/feel-wasm/smoke.mjs |
| 7 | 7 | // |
| 8 | -// Expected output: | |
| 9 | -// 2 | |
| 10 | -// true | |
| 8 | +// Expected output (one line per case): | |
| 9 | +// 1 + 1 => 2 | |
| 10 | +// 5 in [1..10] => true | |
| 11 | +// if 1 < 2 then 10 else 20 => 10 | |
| 12 | +// for i in [1,2,3] return i + 1 => [2,3,4] | |
| 13 | +// "apple" < "banana" => true | |
| 11 | 14 | |
| 12 | 15 | import { evaluate } from "./pkg/feel_wasm.js"; |
| 13 | 16 | |
| 14 | -const sum = evaluate("1 + 1", "{}"); | |
| 15 | -console.log(sum); // -> 2 | |
| 17 | +// expression -> expected JSON string. These mirror the feel-core and feel-pg | |
| 18 | +// tests, demonstrating "same core, same result" across all three targets. | |
| 19 | +const cases = [ | |
| 20 | + ["1 + 1", "2"], | |
| 21 | + ["5 in [1..10]", "true"], | |
| 22 | + ["if 1 < 2 then 10 else 20", "10"], | |
| 23 | + ["for i in [1,2,3] return i + 1", "[2,3,4]"], | |
| 24 | + ['"apple" < "banana"', "true"], | |
| 25 | +]; | |
| 16 | 26 | |
| 17 | -const inRange = evaluate("5 in [1..10]", "{}"); | |
| 18 | -console.log(inRange); // -> true | |
| 19 | - | |
| 20 | -// Minimal assertions so the script exits non-zero on regression. | |
| 21 | -if (sum !== "2") { | |
| 22 | - console.error(`FAIL: expected "2", got ${JSON.stringify(sum)}`); | |
| 23 | - process.exit(1); | |
| 27 | +let ok = true; | |
| 28 | +for (const [expr, expected] of cases) { | |
| 29 | + const got = evaluate(expr, "{}"); | |
| 30 | + console.log(`${expr} => ${got}`); | |
| 31 | + if (got !== expected) { | |
| 32 | + console.error(`FAIL: expected ${JSON.stringify(expected)}, got ${JSON.stringify(got)}`); | |
| 33 | + ok = false; | |
| 34 | + } | |
| 24 | 35 | } |
| 25 | -if (inRange !== "true") { | |
| 26 | - console.error(`FAIL: expected "true", got ${JSON.stringify(inRange)}`); | |
| 27 | - process.exit(1); | |
| 28 | -} | |
| 36 | +if (!ok) process.exit(1); | |
| 29 | 37 | console.error("smoke test passed"); |
modified crates/feel-wasm/src/lib.rs +16 −10
| @@ -36,6 +36,8 @@ | ||
| 36 | 36 | match value { |
| 37 | 37 | Value::Number(n) => number_to_json(*n), |
| 38 | 38 | Value::Boolean(b) => serde_json::Value::Bool(*b), |
| 39 | + Value::String(s) => serde_json::Value::String(s.clone()), | |
| 40 | + Value::List(items) => serde_json::Value::Array(items.iter().map(value_to_json).collect()), | |
| 39 | 41 | Value::Null => serde_json::Value::Null, |
| 40 | 42 | } |
| 41 | 43 | } |
| @@ -55,16 +57,7 @@ | ||
| 55 | 57 | match json { |
| 56 | 58 | serde_json::Value::Object(map) => { |
| 57 | 59 | for (key, val) in map { |
| 58 | - match val { | |
| 59 | - serde_json::Value::Number(num) => { | |
| 60 | - if let Some(f) = num.as_f64() { | |
| 61 | - ctx.insert(key.clone(), Value::Number(f)); | |
| 62 | - } | |
| 63 | - } | |
| 64 | - serde_json::Value::Bool(b) => ctx.insert(key.clone(), Value::Boolean(*b)), | |
| 65 | - serde_json::Value::Null => ctx.insert(key.clone(), Value::Null), | |
| 66 | - _ => { /* strings/arrays/objects are out of scope for now */ } | |
| 67 | - } | |
| 60 | + ctx.insert(key.clone(), json_to_value(val)); | |
| 68 | 61 | } |
| 69 | 62 | Ok(ctx) |
| 70 | 63 | } |
| @@ -72,3 +65,16 @@ | ||
| 72 | 65 | other => Err(format!("expected a JSON object, got {other}")), |
| 73 | 66 | } |
| 74 | 67 | } |
| 68 | + | |
| 69 | +/// Map a JSON value to a `feel-core` [`Value`]. Numbers, booleans, strings, | |
| 70 | +/// arrays (-> list) and null are supported; nested objects map to null (there is | |
| 71 | +/// no context/map value type in this slice). | |
| 72 | +fn json_to_value(json: &serde_json::Value) -> Value { | |
| 73 | + match json { | |
| 74 | + serde_json::Value::Number(num) => num.as_f64().map(Value::Number).unwrap_or(Value::Null), | |
| 75 | + serde_json::Value::Bool(b) => Value::Boolean(*b), | |
| 76 | + serde_json::Value::String(s) => Value::String(s.clone()), | |
| 77 | + serde_json::Value::Array(arr) => Value::List(arr.iter().map(json_to_value).collect()), | |
| 78 | + serde_json::Value::Null | serde_json::Value::Object(_) => Value::Null, | |
| 79 | + } | |
| 80 | +} | |