Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

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>
Changeset4b18bb338a60b7f5c7c65fd74b3d92196b348217
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsda561bfa6b66
Children
Pushlog
by timo via ssh

modified crates/feel-pg/src/lib.rs +43 −13

@@ -37,6 +37,8 @@
3737
     match value {
3838
         Value::Number(n) => number_to_json(*n),
3939
         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()),
4042
         Value::Null => serde_json::Value::Null,
4143
     }
4244
 }
@@ -51,29 +53,32 @@
5153
     }
5254
 }
5355
 
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`].
5758
 fn context_from_json(json: &serde_json::Value) -> Result<Context, String> {
5859
     let mut ctx = Context::new();
5960
     match json {
6061
         serde_json::Value::Object(map) => {
6162
             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));
7264
             }
7365
             Ok(ctx)
7466
         }
7567
         serde_json::Value::Null => Ok(ctx),
7668
         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,
7782
     }
7883
 }
7984
 
@@ -104,6 +109,31 @@
104109
             .expect("SPI query failed");
105110
         assert_eq!(matched, Some(true));
106111
     }
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
+    }
107137
 }
108138
 
109139
 /// Required pgrx test harness hooks.

modified crates/feel-wasm/smoke.mjs +24 −16

@@ -5,25 +5,33 @@
55
 // Then run:
66
 //   node crates/feel-wasm/smoke.mjs
77
 //
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
1114
 
1215
 import { evaluate } from "./pkg/feel_wasm.js";
1316
 
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
+];
1626
 
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
+  }
2435
 }
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);
2937
 console.error("smoke test passed");

modified crates/feel-wasm/src/lib.rs +16 −10

@@ -36,6 +36,8 @@
3636
     match value {
3737
         Value::Number(n) => number_to_json(*n),
3838
         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()),
3941
         Value::Null => serde_json::Value::Null,
4042
     }
4143
 }
@@ -55,16 +57,7 @@
5557
     match json {
5658
         serde_json::Value::Object(map) => {
5759
             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));
6861
             }
6962
             Ok(ctx)
7063
         }
@@ -72,3 +65,16 @@
7265
         other => Err(format!("expected a JSON object, got {other}")),
7366
     }
7467
 }
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
+}