A lexicon-driven AppView for ATProto. happyview.dev
backfill firehose jetstream atproto appview oauth lexicon
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add `toarray()` so users can force a Lua table to serialize to an array

Trezy a1cd2b6a 8b550b5d

+39 -1
+39 -1
src/lua/sandbox.rs
··· 1 - use mlua::{Lua, Result as LuaResult}; 1 + use mlua::{Lua, LuaSerdeExt, Result as LuaResult}; 2 2 3 3 use super::tid::generate_tid; 4 4 ··· 48 48 // Utility: TID() returns a fresh AT Protocol TID string 49 49 let tid_fn = lua.create_function(|_, ()| Ok(generate_tid()))?; 50 50 globals.set("TID", tid_fn)?; 51 + 52 + // Utility: toarray(table) marks a table as a JSON array for serialization. 53 + // Ensures empty tables serialize as [] instead of {}. 54 + let toarray_fn = lua.create_function(|lua, table: mlua::Table| { 55 + let values: Vec<mlua::Value> = table.sequence_values().collect::<LuaResult<_>>()?; 56 + let seq = lua.create_sequence_from(values)?; 57 + seq.set_metatable(Some(lua.array_metatable()))?; 58 + Ok(seq) 59 + })?; 60 + globals.set("toarray", toarray_fn)?; 51 61 52 62 Ok(lua) 53 63 } ··· 142 152 fn validate_script_rejects_syntax_error() { 143 153 let result = validate_script("function handle("); 144 154 assert!(result.is_err()); 155 + } 156 + 157 + #[test] 158 + fn sandbox_provides_toarray() { 159 + let lua = create_sandbox().unwrap(); 160 + lua.load(r#"result = toarray({})"#).exec().unwrap(); 161 + } 162 + 163 + #[test] 164 + fn sandbox_toarray_preserves_values() { 165 + let lua = create_sandbox().unwrap(); 166 + let result: Vec<i64> = lua 167 + .load(r#"return toarray({10, 20, 30})"#) 168 + .eval::<mlua::Table>() 169 + .unwrap() 170 + .sequence_values() 171 + .collect::<LuaResult<_>>() 172 + .unwrap(); 173 + assert_eq!(result, vec![10, 20, 30]); 174 + } 175 + 176 + #[test] 177 + fn sandbox_toarray_empty_serializes_as_array() { 178 + use mlua::LuaSerdeExt; 179 + let lua = create_sandbox().unwrap(); 180 + let table: mlua::Table = lua.load(r#"return toarray({})"#).eval().unwrap(); 181 + let json: serde_json::Value = lua.from_value(mlua::Value::Table(table)).unwrap(); 182 + assert!(json.is_array(), "expected JSON array, got: {json}"); 145 183 } 146 184 }