Support `.len()` on reflective lists and maps

Signed-off-by: Olivier <olivier@librepush.net>
This commit is contained in:
Olivier 'reivilibre' 2024-06-19 23:51:20 +01:00
parent cdc34d45f7
commit dd74768ec6
1 changed files with 15 additions and 4 deletions

View File

@ -1,5 +1,6 @@
use std::{collections::BTreeMap, sync::Arc};
use bevy_reflect::ReflectRef;
use percent_encoding::NON_ALPHANUMERIC;
use crate::interface::Value;
@ -93,15 +94,25 @@ pub fn len(obj: Value, args: Vec<Value>) -> Result<Value, String> {
return Err(format!("len takes 0 args, not {}", args.len()));
}
match obj {
Value::Str(string) => Ok(Value::Int(string.len() as i64)),
Value::List(list) => Ok(Value::Int(list.len() as i64)),
Ok(Value::Int(match obj {
Value::Str(string) => string.len() as i64,
Value::List(list) => list.len() as i64,
Value::Reflective(reflect) => match reflect.reflect_ref() {
ReflectRef::List(list) => list.len() as i64,
ReflectRef::Array(array) => array.len() as i64,
ReflectRef::Map(map) => map.len() as i64,
_ => {
return Err(format!(
"reflective {reflect:?} is not a list or map: can't get length!"
));
}
},
_ => {
return Err(format!(
"{obj:?} is not a string or list: can't get length!"
));
}
}
}))
}
/// Splits a string by given delimiters.