Add functions on SQL Value for matching on type

This commit is contained in:
Tobie Morgan Hitchcock 2022-07-04 02:02:46 +01:00
parent 410295c800
commit 8dc7341cb1

View file

@ -510,6 +510,26 @@ impl Value {
}
}
pub fn is_uuid(&self) -> bool {
matches!(self, Value::Uuid(_))
}
pub fn is_thing(&self) -> bool {
matches!(self, Value::Thing(_))
}
pub fn is_strand(&self) -> bool {
matches!(self, Value::Strand(_))
}
pub fn is_array(&self) -> bool {
matches!(self, Value::Array(_))
}
pub fn is_object(&self) -> bool {
matches!(self, Value::Object(_))
}
pub fn is_type_record(&self, types: &[Table]) -> bool {
match self {
Value::Thing(v) => types.iter().any(|tb| tb.0 == v.tb),
@ -736,6 +756,23 @@ impl Value {
}
}
pub fn make_table(self) -> Value {
match self {
Value::Table(_) => self,
Value::Strand(v) => Value::Table(Table(v.0)),
_ => Value::Table(Table(self.as_strand().0)),
}
}
pub fn make_table_or_thing(self) -> Value {
match self {
Value::Table(_) => self,
Value::Thing(_) => self,
Value::Strand(v) => Value::Table(Table(v.0)),
_ => Value::Table(Table(self.as_strand().0)),
}
}
pub fn convert_to(self, kind: &Kind) -> Value {
match kind {
Kind::Any => self,