From 390d1aa504276d55187725b06b30cb746a628478 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 14 Feb 2024 19:15:47 -0500 Subject: [PATCH] Revert "Remove structure from map types" This reverts commit 5e685d66415b67017acf5536d2bfc6c1fd4f6f9f. --- src/abstract_tree/built_in_value.rs | 13 ++++++++----- src/abstract_tree/index.rs | 12 +++++++++++- src/abstract_tree/type.rs | 24 ++++++++++++++---------- src/abstract_tree/value_node.rs | 12 ++++++++++-- src/value/mod.rs | 15 +++++++++++++-- tests/as.rs | 2 +- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/abstract_tree/built_in_value.rs b/src/abstract_tree/built_in_value.rs index 10c81c0..a71114f 100644 --- a/src/abstract_tree/built_in_value.rs +++ b/src/abstract_tree/built_in_value.rs @@ -6,7 +6,8 @@ use serde::{Deserialize, Serialize}; use crate::{ built_in_functions::{fs::fs_functions, json::json_functions, str::string_functions, Callable}, error::{RuntimeError, SyntaxError, ValidationError}, - AbstractTree, BuiltInFunction, Context, Format, Function, List, Map, SyntaxNode, Type, Value, + AbstractTree, BuiltInFunction, Context, Format, Function, List, Map, Structure, SyntaxNode, + Type, Value, }; static ARGS: OnceLock = OnceLock::new(); @@ -86,12 +87,14 @@ impl BuiltInValue { match self { BuiltInValue::Args => Type::list(Type::String), BuiltInValue::AssertEqual => BuiltInFunction::AssertEqual.r#type(), - BuiltInValue::Fs => Type::Map, - BuiltInValue::Json => Type::Map, + BuiltInValue::Fs => Type::Map(None), + BuiltInValue::Json => Type::Map(Some( + Structure::from_map(self.get().as_map().unwrap()).unwrap(), + )), BuiltInValue::Length => BuiltInFunction::Length.r#type(), BuiltInValue::Output => BuiltInFunction::Output.r#type(), - BuiltInValue::Random => Type::Map, - BuiltInValue::Str => Type::Map, + BuiltInValue::Random => Type::Map(None), + BuiltInValue::Str => Type::Map(None), } } diff --git a/src/abstract_tree/index.rs b/src/abstract_tree/index.rs index 4eb6c1b..a401235 100644 --- a/src/abstract_tree/index.rs +++ b/src/abstract_tree/index.rs @@ -48,7 +48,17 @@ impl AbstractTree for Index { fn expected_type(&self, context: &Context) -> Result { match self.collection.expected_type(context)? { Type::List(item_type) => Ok(*item_type.clone()), - Type::Map => Ok(Type::Any), + Type::Map(structure) => { + if let Some(structure) = structure { + if let IndexExpression::Identifier(identifier) = &self.index { + if let Some((_, r#type)) = structure.inner().get(identifier.inner()) { + return Ok(r#type.clone()); + } + } + } + + Ok(Type::Any) + } Type::None => Ok(Type::None), r#type => Ok(r#type), } diff --git a/src/abstract_tree/type.rs b/src/abstract_tree/type.rs index b1092f9..c8a68c8 100644 --- a/src/abstract_tree/type.rs +++ b/src/abstract_tree/type.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::{ error::{RuntimeError, SyntaxError, ValidationError}, - AbstractTree, Context, Format, Identifier, SyntaxNode, Value, + AbstractTree, Context, Format, Identifier, Structure, SyntaxNode, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -20,7 +20,7 @@ pub enum Type { }, Integer, List(Box), - Map, + Map(Option), None, Number, String, @@ -58,13 +58,13 @@ impl Type { | (Type::Collection, Type::Collection) | (Type::Collection, Type::List(_)) | (Type::List(_), Type::Collection) - | (Type::Collection, Type::Map) - | (Type::Map, Type::Collection) + | (Type::Collection, Type::Map(_)) + | (Type::Map(_), Type::Collection) | (Type::Collection, Type::String) | (Type::String, Type::Collection) | (Type::Float, Type::Float) | (Type::Integer, Type::Integer) - | (Type::Map, Type::Map) + | (Type::Map(_), Type::Map(_)) | (Type::Number, Type::Number) | (Type::Number, Type::Integer) | (Type::Number, Type::Float) @@ -121,7 +121,7 @@ impl Type { } pub fn is_map(&self) -> bool { - matches!(self, Type::Map) + matches!(self, Type::Map(_)) } } @@ -173,7 +173,7 @@ impl AbstractTree for Type { } } "int" => Type::Integer, - "map" => Type::Map, + "map" => Type::Map(None), "num" => Type::Number, "none" => Type::None, "str" => Type::String, @@ -241,8 +241,12 @@ impl Format for Type { item_type.format(output, indent_level); output.push(']'); } - Type::Map => { - output.push_str("map"); + Type::Map(structure_option) => { + if let Some(structure) = structure_option { + output.push_str(&structure.to_string()); + } else { + output.push_str("map"); + } } Type::None => output.push_str("none"), Type::Number => output.push_str("num"), @@ -284,7 +288,7 @@ impl Display for Type { } Type::Integer => write!(f, "int"), Type::List(item_type) => write!(f, "[{item_type}]"), - Type::Map => write!(f, "map"), + Type::Map(_) => write!(f, "map"), Type::Number => write!(f, "num"), Type::None => write!(f, "none"), Type::String => write!(f, "str"), diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 2e71137..1a15b9d 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -217,9 +217,17 @@ impl AbstractTree for ValueNode { Type::None } } - ValueNode::Map(_, _) => Type::Map, + ValueNode::Map(_, _) => Type::Map(None), ValueNode::BuiltInValue(built_in_value) => built_in_value.expected_type(context)?, - ValueNode::Structure(_) => Type::Map, + ValueNode::Structure(node_map) => { + let mut value_map = BTreeMap::new(); + + for (key, (_statement_option, r#type)) in node_map { + value_map.insert(key.to_string(), (None, r#type.clone())); + } + + Type::Map(Some(Structure::new(value_map))) + } ValueNode::Range(_) => Type::Range, }; diff --git a/src/value/mod.rs b/src/value/mod.rs index 27b6412..0b43767 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,5 +1,5 @@ //! Types that represent runtime values. -use crate::{error::RuntimeError, Type}; +use crate::{error::RuntimeError, Identifier, Type, TypeSpecification}; use serde::{ de::{MapAccess, SeqAccess, Visitor}, @@ -80,7 +80,18 @@ impl Value { Type::List(Box::new(Type::Any)) } } - Value::Map(_) => Type::Map, + Value::Map(map) => { + let mut identifier_types = Vec::new(); + + for (key, value) in map.inner().unwrap().iter() { + identifier_types.push(( + Identifier::new(key.clone()), + TypeSpecification::new(value.r#type()), + )); + } + + Type::Map(None) + } Value::Function(function) => function.r#type().clone(), Value::String(_) => Type::String, Value::Float(_) => Type::Float, diff --git a/tests/as.rs b/tests/as.rs index 5f6a29d..ec31c78 100644 --- a/tests/as.rs +++ b/tests/as.rs @@ -39,7 +39,7 @@ fn conversion_runtime_error() { interpret(&format!("json:parse('{JSON}') as [map]")), Err(Error::Runtime(RuntimeError::ConversionImpossible { value: json_value, - target_type: Type::List(Box::new(Type::Map)) + target_type: Type::List(Box::new(Type::Map(None))) })) ) }