TypedValue::Keyword now wraps a NamespacedKeyword rather than a String. Fixes #203. r=nalexander" (#329)

This commit is contained in:
Jordan Santell 2017-02-17 14:07:57 -08:00 committed by GitHub
parent a59f9583ac
commit 6f67f8563b
4 changed files with 15 additions and 23 deletions

View file

@ -48,7 +48,7 @@ pub enum TypedValue {
Double(OrderedFloat<f64>), Double(OrderedFloat<f64>),
// TODO: &str throughout? // TODO: &str throughout?
String(String), String(String),
Keyword(String), Keyword(NamespacedKeyword),
} }
impl TypedValue { impl TypedValue {

View file

@ -10,7 +10,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use ::{to_namespaced_keyword};
use edn; use edn;
use errors::{ErrorKind, Result}; use errors::{ErrorKind, Result};
use edn::types::Value; use edn::types::Value;
@ -200,11 +199,10 @@ fn symbolic_schema_to_triples(ident_map: &IdentMap, symbolic_schema: &Value) ->
// bootstrap symbolic schema, or by representing the initial bootstrap // bootstrap symbolic schema, or by representing the initial bootstrap
// schema directly as Rust data. // schema directly as Rust data.
let typed_value = match TypedValue::from_edn_value(value) { let typed_value = match TypedValue::from_edn_value(value) {
Some(TypedValue::Keyword(ref s)) => { Some(TypedValue::Keyword(ref k)) => {
to_namespaced_keyword(s) ident_map.get(k)
.and_then(|ident| ident_map.get(&ident))
.map(|entid| TypedValue::Ref(*entid)) .map(|entid| TypedValue::Ref(*entid))
.ok_or(ErrorKind::UnrecognizedIdent(s.clone()))? .ok_or(ErrorKind::UnrecognizedIdent(k.to_string()))?
}, },
Some(v) => v, Some(v) => v,
_ => bail!(ErrorKind::BadBootstrapDefinition(format!("Expected Mentat typed value for value but got '{:?}'", value))) _ => bail!(ErrorKind::BadBootstrapDefinition(format!("Expected Mentat typed value for value but got '{:?}'", value)))

View file

@ -344,7 +344,11 @@ impl TypedSQLValue for TypedValue {
(5, rusqlite::types::Value::Integer(x)) => Ok(TypedValue::Long(x)), (5, rusqlite::types::Value::Integer(x)) => Ok(TypedValue::Long(x)),
(5, rusqlite::types::Value::Real(x)) => Ok(TypedValue::Double(x.into())), (5, rusqlite::types::Value::Real(x)) => Ok(TypedValue::Double(x.into())),
(10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(x)), (10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(x)),
(13, rusqlite::types::Value::Text(x)) => Ok(TypedValue::Keyword(x)), (13, rusqlite::types::Value::Text(x)) => {
to_namespaced_keyword(&x).map(|k| TypedValue::Keyword(k))
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
.ok_or(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", x)).into())
},
(_, value) => bail!(ErrorKind::BadSQLValuePair(value, value_type_tag)), (_, value) => bail!(ErrorKind::BadSQLValuePair(value, value_type_tag)),
} }
} }
@ -362,7 +366,7 @@ impl TypedSQLValue for TypedValue {
&Value::Integer(x) => Some(TypedValue::Long(x)), &Value::Integer(x) => Some(TypedValue::Long(x)),
&Value::Float(ref x) => Some(TypedValue::Double(x.clone())), &Value::Float(ref x) => Some(TypedValue::Double(x.clone())),
&Value::Text(ref x) => Some(TypedValue::String(x.clone())), &Value::Text(ref x) => Some(TypedValue::String(x.clone())),
&Value::NamespacedKeyword(ref x) => Some(TypedValue::Keyword(x.to_string())), &Value::NamespacedKeyword(ref x) => Some(TypedValue::Keyword(x.clone())),
_ => None _ => None
} }
} }
@ -376,7 +380,7 @@ impl TypedSQLValue for TypedValue {
&TypedValue::Long(x) => (rusqlite::types::Value::Integer(x).into(), 5), &TypedValue::Long(x) => (rusqlite::types::Value::Integer(x).into(), 5),
&TypedValue::Double(x) => (rusqlite::types::Value::Real(x.into_inner()).into(), 5), &TypedValue::Double(x) => (rusqlite::types::Value::Real(x.into_inner()).into(), 5),
&TypedValue::String(ref x) => (rusqlite::types::ValueRef::Text(x.as_str()).into(), 10), &TypedValue::String(ref x) => (rusqlite::types::ValueRef::Text(x.as_str()).into(), 10),
&TypedValue::Keyword(ref x) => (rusqlite::types::ValueRef::Text(x.as_str()).into(), 13), &TypedValue::Keyword(ref x) => (rusqlite::types::ValueRef::Text(&x.to_string()).into(), 13),
} }
} }
@ -388,13 +392,7 @@ impl TypedSQLValue for TypedValue {
&TypedValue::Long(x) => (Value::Integer(x), ValueType::Long), &TypedValue::Long(x) => (Value::Integer(x), ValueType::Long),
&TypedValue::Double(x) => (Value::Float(x), ValueType::Double), &TypedValue::Double(x) => (Value::Float(x), ValueType::Double),
&TypedValue::String(ref x) => (Value::Text(x.clone()), ValueType::String), &TypedValue::String(ref x) => (Value::Text(x.clone()), ValueType::String),
&TypedValue::Keyword(ref x) => { &TypedValue::Keyword(ref x) => (Value::NamespacedKeyword(x.clone()), ValueType::Keyword),
match to_namespaced_keyword(&x) {
Some(x) => (Value::NamespacedKeyword(x), ValueType::Keyword),
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
None => panic!(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", x))),
}
},
} }
} }
} }
@ -488,11 +486,7 @@ impl DB {
// Ref coerces a little: we interpret some things depending on the schema as a Ref. // Ref coerces a little: we interpret some things depending on the schema as a Ref.
(&ValueType::Ref, TypedValue::Long(x)) => Ok(TypedValue::Ref(x)), (&ValueType::Ref, TypedValue::Long(x)) => Ok(TypedValue::Ref(x)),
(&ValueType::Ref, TypedValue::Keyword(ref x)) => { (&ValueType::Ref, TypedValue::Keyword(ref x)) => {
match to_namespaced_keyword(x) { self.schema.require_entid(&x).map(|entid| TypedValue::Ref(entid))
Some(x) => self.schema.require_entid(&x).map(|entid| TypedValue::Ref(entid)),
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
None => bail!(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", x))),
}
} }
// Otherwise, we have a type mismatch. // Otherwise, we have a type mismatch.
(value_type, _) => bail!(ErrorKind::BadEDNValuePair(value.clone(), value_type.clone())), (value_type, _) => bail!(ErrorKind::BadEDNValuePair(value.clone(), value_type.clone())),

View file

@ -35,7 +35,7 @@ fn test_from_sql_value_pair() {
assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.5), 5).unwrap(), TypedValue::Double(OrderedFloat(0.5))); assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.5), 5).unwrap(), TypedValue::Double(OrderedFloat(0.5)));
assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 10).unwrap(), TypedValue::String(":db/keyword".into())); assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 10).unwrap(), TypedValue::String(":db/keyword".into()));
assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 13).unwrap(), TypedValue::Keyword(":db/keyword".into())); assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 13).unwrap(), TypedValue::Keyword(symbols::NamespacedKeyword::new("db", "keyword")));
} }
#[test] #[test]
@ -52,5 +52,5 @@ fn test_to_edn_value_pair() {
assert_eq!(TypedValue::Double(OrderedFloat(0.5)).to_edn_value_pair(), (edn::Value::Float(OrderedFloat(0.5)), ValueType::Double)); assert_eq!(TypedValue::Double(OrderedFloat(0.5)).to_edn_value_pair(), (edn::Value::Float(OrderedFloat(0.5)), ValueType::Double));
assert_eq!(TypedValue::String(":db/keyword".into()).to_edn_value_pair(), (edn::Value::Text(":db/keyword".into()), ValueType::String)); assert_eq!(TypedValue::String(":db/keyword".into()).to_edn_value_pair(), (edn::Value::Text(":db/keyword".into()), ValueType::String));
assert_eq!(TypedValue::Keyword(":db/keyword".into()).to_edn_value_pair(), (edn::Value::NamespacedKeyword(symbols::NamespacedKeyword::new("db", "keyword")), ValueType::Keyword)); assert_eq!(TypedValue::Keyword(symbols::NamespacedKeyword::new("db", "keyword")).to_edn_value_pair(), (edn::Value::NamespacedKeyword(symbols::NamespacedKeyword::new("db", "keyword")), ValueType::Keyword));
} }