Skip to content

Commit

Permalink
Support indexing maps with any valid key type (#36)
Browse files Browse the repository at this point in the history
* Support indexing maps with any valid key type

* Use more appropriate variable name for numeric map access test

* Factor out duplicated code into Map::get implementation
  • Loading branch information
fore5fire authored Feb 26, 2024
1 parent cca79e6 commit 458c313
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::ops;
use serde::Serialize;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::{Infallible, TryInto};
use std::convert::{Infallible, TryFrom, TryInto};
use std::fmt::{Display, Formatter};
use std::rc::Rc;
use std::sync::Arc;
Expand All @@ -25,6 +25,22 @@ impl PartialOrd for Map {
}
}

impl Map {
/// Returns a reference to the value corresponding to the key. Implicitly converts between int
/// and uint keys.
pub fn get(&self, key: &Key) -> Option<&Value> {
self.map.get(key).or_else(|| {
// Also check keys that are cross type comparable.
let converted = match key {
Key::Int(k) => Key::Uint(u64::try_from(*k).ok()?),
Key::Uint(k) => Key::Int(i64::try_from(*k).ok()?),
_ => return None,
};
self.map.get(&converted)
})
}
}

#[derive(Debug, Eq, PartialEq, Hash, Ord, Clone, PartialOrd)]
pub enum Key {
Int(i64),
Expand Down Expand Up @@ -541,7 +557,21 @@ impl<'a> Value {
.into()
}
(Value::Map(map), Value::String(property)) => map
.map
.get(&property.into())
.cloned()
.unwrap_or(Value::Null)
.into(),
(Value::Map(map), Value::Bool(property)) => map
.get(&property.into())
.cloned()
.unwrap_or(Value::Null)
.into(),
(Value::Map(map), Value::Int(property)) => map
.get(&property.into())
.cloned()
.unwrap_or(Value::Null)
.into(),
(Value::Map(map), Value::UInt(property)) => map
.get(&property.into())
.cloned()
.unwrap_or(Value::Null)
Expand Down Expand Up @@ -775,6 +805,18 @@ mod tests {
assert_eq!(value, "application/json".into());
}

#[test]
fn test_numeric_map_access() {
let mut context = Context::default();
let mut numbers = HashMap::new();
numbers.insert(Key::Uint(1), "one".to_string());
context.add_variable_from_value("numbers", numbers);

let program = Program::compile("numbers[1]").unwrap();
let value = program.execute(&context).unwrap();
assert_eq!(value, "one".into());
}

#[test]
fn test_heterogeneous_compare() {
let context = Context::default();
Expand Down

0 comments on commit 458c313

Please sign in to comment.