use relate_core::temporal::TemporalKind;
use relate_core::Value;
use relate_ir::{PredefinedType, TypeExpr};
pub(crate) fn value_matches_type(value: &Value, ty: &TypeExpr) -> bool {
match ty {
TypeExpr::Predefined { kind, not_null } => {
if value.is_null() {
return accepts_null(*not_null) && *kind != PredefinedType::Nothing;
}
match kind {
PredefinedType::Nothing | PredefinedType::Null => false,
PredefinedType::Any => true,
PredefinedType::Boolean => matches!(value, Value::Bool(_)),
PredefinedType::String => matches!(value, Value::String(_)),
PredefinedType::Uuid => matches!(value, Value::Uuid(_)),
PredefinedType::Integer => matches!(value, Value::Int(_)),
PredefinedType::Float => matches!(value, Value::Float(_)),
PredefinedType::Date => matches_kind(value, TemporalKind::Date),
PredefinedType::Duration => matches_kind(value, TemporalKind::Duration),
PredefinedType::Point => matches!(value, Value::Point(_)),
PredefinedType::LocalTime => matches_kind(value, TemporalKind::LocalTime),
PredefinedType::ZonedTime => matches_kind(value, TemporalKind::Time),
PredefinedType::LocalDateTime => matches_kind(value, TemporalKind::LocalDateTime),
PredefinedType::ZonedDateTime => matches_kind(value, TemporalKind::DateTime),
PredefinedType::Node => matches!(value, Value::Node(_)),
PredefinedType::Relationship => matches!(value, Value::Relationship(_)),
PredefinedType::Path => matches!(value, Value::Path(_)),
PredefinedType::Map => matches!(value, Value::Map(_)),
}
}
TypeExpr::List { element, not_null } => {
if value.is_null() {
accepts_null(*not_null)
} else {
match value {
Value::List(xs) => xs.iter().all(|x| value_matches_type(x, element)),
Value::Bytes(b) => b
.iter()
.all(|&x| value_matches_type(&Value::Int(x.into()), element)),
_ => false,
}
}
}
TypeExpr::PropertyValue { not_null } => {
if value.is_null() {
accepts_null(*not_null)
} else {
is_property_value(value)
}
}
TypeExpr::Union(items) => items.iter().any(|t| value_matches_type(value, t)),
TypeExpr::Vector {
coord,
dimension,
not_null,
} => {
if value.is_null() {
return accepts_null(*not_null);
}
matches!(
value,
Value::Vector { coord: actual, coords }
if coord.as_ref().is_none_or(|c| c == actual.canonical_name())
&& dimension.is_none_or(|d| d as usize == coords.len())
)
}
}
}
fn accepts_null(not_null: bool) -> bool {
!not_null
}
pub(crate) fn is_property_value(value: &Value) -> bool {
!value.is_null() && crate::props::property_value_invalidity(value).is_none()
}
fn matches_kind(value: &Value, kind: TemporalKind) -> bool {
value.as_temporal().is_some_and(|t| t.kind() == kind)
}
#[cfg(test)]
mod tests {
use super::*;
const UUID: u128 = 0xf066_ecfc_5151_4121_8a36_b35e_00d7_2918;
#[test]
fn property_value_lists_are_homogeneous() {
let list = |xs: Vec<Value>| Value::List(xs.into());
assert!(is_property_value(&list(vec![Value::Int(42)])));
assert!(is_property_value(&list(vec![])));
assert!(value_matches_type(
&list(vec![]),
&TypeExpr::PropertyValue { not_null: true }
));
assert!(is_property_value(&list(vec![
Value::Int(1),
Value::Float(2.5)
])));
assert!(!is_property_value(&list(vec![
Value::String("abc".into()),
Value::Int(42)
])));
assert!(!is_property_value(&list(vec![Value::Null])));
assert!(!is_property_value(&list(vec![list(vec![Value::Int(42)])])));
}
#[test]
fn uuid_is_a_property_value() {
let u = Value::Uuid(UUID);
let list = Value::List(vec![u.clone()].into());
let any_property_value = TypeExpr::PropertyValue { not_null: true };
assert!(value_matches_type(&u, &any_property_value));
assert!(value_matches_type(&list, &any_property_value));
assert!(value_matches_type(
&Value::Null,
&TypeExpr::PropertyValue { not_null: false }
));
assert!(!value_matches_type(&Value::Null, &any_property_value));
assert!(!value_matches_type(
&u,
&TypeExpr::Predefined {
kind: PredefinedType::String,
not_null: false,
}
));
assert!(!value_matches_type(
&Value::List(vec![list].into()),
&any_property_value
));
}
#[test]
fn bytes_stay_out_of_the_property_value_set() {
let bytes = Value::Bytes(vec![1u8, 2].into());
assert!(!is_property_value(&bytes));
assert!(!value_matches_type(
&bytes,
&TypeExpr::PropertyValue { not_null: true }
));
}
}