From dd74768ec62f5bfa3c9b7c60a5a49d3d5d551b99 Mon Sep 17 00:00:00 2001 From: Olivier Date: Wed, 19 Jun 2024 23:51:20 +0100 Subject: [PATCH] Support `.len()` on reflective lists and maps Signed-off-by: Olivier --- .../src/functions/defaults.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/hornbeam_interpreter/src/functions/defaults.rs b/hornbeam_interpreter/src/functions/defaults.rs index 06f190a..49e9953 100644 --- a/hornbeam_interpreter/src/functions/defaults.rs +++ b/hornbeam_interpreter/src/functions/defaults.rs @@ -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) -> Result { 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.