Consolidate Entity::{Add, Retract} to Entity::AddOrRetract. Fixes #255. r=nalexander (#265)

This commit is contained in:
Jordan Santell 2017-02-08 15:45:09 -08:00 committed by GitHub
parent 688a644bd9
commit 21f7bdf493
4 changed files with 33 additions and 30 deletions

View file

@ -24,7 +24,7 @@ use edn::types::Value;
use entids; use entids;
use errors::*; use errors::*;
use mentat_tx::entities as entmod; use mentat_tx::entities as entmod;
use mentat_tx::entities::Entity; use mentat_tx::entities::{Entity, OpType};
use types::*; use types::*;
pub fn new_connection() -> rusqlite::Connection { pub fn new_connection() -> rusqlite::Connection {
@ -710,11 +710,11 @@ impl DB {
// underlying entities, in which case this expression is more natural than for loops. // underlying entities, in which case this expression is more natural than for loops.
let r: Vec<Result<()>> = entities.into_iter().map(|entity: &Entity| -> Result<()> { let r: Vec<Result<()>> = entities.into_iter().map(|entity: &Entity| -> Result<()> {
match *entity { match *entity {
Entity::Add { Entity::AddOrRetract {
op: OpType::Add,
e: entmod::EntidOrLookupRef::Entid(ref e_), e: entmod::EntidOrLookupRef::Entid(ref e_),
a: ref a_, a: ref a_,
v: entmod::ValueOrLookupRef::Value(ref v_), v: entmod::ValueOrLookupRef::Value(ref v_)} => {
tx: _ } => {
let e: i64 = match e_ { let e: i64 = match e_ {
&entmod::Entid::Entid(ref e__) => *e__, &entmod::Entid::Entid(ref e__) => *e__,
@ -745,7 +745,8 @@ impl DB {
Ok(()) Ok(())
}, },
Entity::Retract { Entity::AddOrRetract {
op: OpType::Retract,
e: entmod::EntidOrLookupRef::Entid(ref e_), e: entmod::EntidOrLookupRef::Entid(ref e_),
a: ref a_, a: ref a_,
v: entmod::ValueOrLookupRef::Value(ref v_) } => { v: entmod::ValueOrLookupRef::Value(ref v_) } => {

View file

@ -14,11 +14,11 @@ extern crate edn;
extern crate combine; extern crate combine;
extern crate mentat_tx; extern crate mentat_tx;
use combine::{any, eof, many, optional, parser, satisfy_map, token, Parser, ParseResult, Stream}; use combine::{any, eof, many, parser, satisfy_map, token, Parser, ParseResult, Stream};
use combine::combinator::{Expected, FnParser}; use combine::combinator::{Expected, FnParser};
use edn::symbols::NamespacedKeyword; use edn::symbols::NamespacedKeyword;
use edn::types::Value; use edn::types::Value;
use mentat_tx::entities::*; use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, LookupRef, OpType, ValueOrLookupRef};
pub struct Tx<I>(::std::marker::PhantomData<fn(I) -> I>); pub struct Tx<I>(::std::marker::PhantomData<fn(I) -> I>);
@ -114,15 +114,13 @@ impl<I> Tx<I>
Tx::<&[Value]>::entid(), Tx::<&[Value]>::entid(),
// TODO: handle lookup-ref. // TODO: handle lookup-ref.
any(), any(),
// TODO: entid or special keyword :db/tx?
optional(Tx::<&[Value]>::entid()),
eof()) eof())
.map(|(_, e, a, v, tx, _)| { .map(|(_, e, a, v, _)| {
Entity::Add { Entity::AddOrRetract {
op: OpType::Add,
e: e, e: e,
a: a, a: a,
v: ValueOrLookupRef::Value(v), v: ValueOrLookupRef::Value(v),
tx: tx,
} }
}); });
// TODO: use ok() with a type annotation rather than explicit match. // TODO: use ok() with a type annotation rather than explicit match.
@ -138,7 +136,7 @@ impl<I> Tx<I>
} }
fn add() -> TxParser<Entity, I> { fn add() -> TxParser<Entity, I> {
fn_parser(Tx::<I>::add_, "[:db/add e a v tx?]") fn_parser(Tx::<I>::add_, "[:db/add e a v]")
} }
fn retract_(input: I) -> ParseResult<Entity, I> { fn retract_(input: I) -> ParseResult<Entity, I> {
@ -152,7 +150,8 @@ impl<I> Tx<I>
any(), any(),
eof()) eof())
.map(|(_, e, a, v, _)| { .map(|(_, e, a, v, _)| {
Entity::Retract { Entity::AddOrRetract {
op: OpType::Retract,
e: e, e: e,
a: a, a: a,
v: ValueOrLookupRef::Value(v), v: ValueOrLookupRef::Value(v),
@ -235,12 +234,12 @@ mod tests {
let mut parser = Tx::entity(); let mut parser = Tx::entity();
let result = parser.parse(&input[..]); let result = parser.parse(&input[..]);
assert_eq!(result, assert_eq!(result,
Ok((Entity::Add { Ok((Entity::AddOrRetract {
op: OpType::Add,
e: EntidOrLookupRef::Entid(Entid::Ident(NamespacedKeyword::new("test", e: EntidOrLookupRef::Entid(Entid::Ident(NamespacedKeyword::new("test",
"entid"))), "entid"))),
a: Entid::Ident(NamespacedKeyword::new("test", "a")), a: Entid::Ident(NamespacedKeyword::new("test", "a")),
v: ValueOrLookupRef::Value(Value::Text("v".into())), v: ValueOrLookupRef::Value(Value::Text("v".into())),
tx: None,
}, },
&[][..]))); &[][..])));
} }
@ -254,7 +253,8 @@ mod tests {
let mut parser = Tx::entity(); let mut parser = Tx::entity();
let result = parser.parse(&input[..]); let result = parser.parse(&input[..]);
assert_eq!(result, assert_eq!(result,
Ok((Entity::Retract { Ok((Entity::AddOrRetract {
op: OpType::Retract,
e: EntidOrLookupRef::Entid(Entid::Entid(101)), e: EntidOrLookupRef::Entid(Entid::Entid(101)),
a: Entid::Ident(NamespacedKeyword::new("test", "a")), a: Entid::Ident(NamespacedKeyword::new("test", "a")),
v: ValueOrLookupRef::Value(Value::Text("v".into())), v: ValueOrLookupRef::Value(Value::Text("v".into())),
@ -272,14 +272,14 @@ mod tests {
let mut parser = Tx::entity(); let mut parser = Tx::entity();
let result = parser.parse(&input[..]); let result = parser.parse(&input[..]);
assert_eq!(result, assert_eq!(result,
Ok((Entity::Add { Ok((Entity::AddOrRetract {
op: OpType::Add,
e: EntidOrLookupRef::LookupRef(LookupRef { e: EntidOrLookupRef::LookupRef(LookupRef {
a: Entid::Ident(NamespacedKeyword::new("test", "a1")), a: Entid::Ident(NamespacedKeyword::new("test", "a1")),
v: Value::Text("v1".into()), v: Value::Text("v1".into()),
}), }),
a: Entid::Ident(NamespacedKeyword::new("test", "a")), a: Entid::Ident(NamespacedKeyword::new("test", "a")),
v: ValueOrLookupRef::Value(Value::Text("v".into())), v: ValueOrLookupRef::Value(Value::Text("v".into())),
tx: None,
}, },
&[][..]))); &[][..])));
} }

View file

@ -16,7 +16,7 @@ extern crate mentat_tx_parser;
use edn::parse; use edn::parse;
use edn::symbols::NamespacedKeyword; use edn::symbols::NamespacedKeyword;
use edn::types::Value; use edn::types::Value;
use mentat_tx::entities::*; use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, OpType, ValueOrLookupRef};
use mentat_tx_parser::Tx; use mentat_tx_parser::Tx;
#[test] #[test]
@ -32,16 +32,17 @@ fn test_entities() {
let result = Tx::parse(&input[..]); let result = Tx::parse(&input[..]);
assert_eq!(result, assert_eq!(result,
Ok(vec![ Ok(vec![
Entity::Add { Entity::AddOrRetract {
e: EntidOrLookupRef::Entid(Entid::Entid(101)), e: EntidOrLookupRef::Entid(Entid::Entid(101)),
a: Entid::Ident(NamespacedKeyword::new("test", "a")), a: Entid::Ident(NamespacedKeyword::new("test", "a")),
v: ValueOrLookupRef::Value(Value::Text("v".into())), v: ValueOrLookupRef::Value(Value::Text("v".into())),
tx: None, op: OpType::Add,
}, },
Entity::Retract { Entity::AddOrRetract {
e: EntidOrLookupRef::Entid(Entid::Entid(102)), e: EntidOrLookupRef::Entid(Entid::Entid(102)),
a: Entid::Ident(NamespacedKeyword::new("test", "b")), a: Entid::Ident(NamespacedKeyword::new("test", "b")),
v: ValueOrLookupRef::Value(Value::Text("w".into())), v: ValueOrLookupRef::Value(Value::Text("w".into())),
op: OpType::Retract,
}, },
])); ]));
} }

View file

@ -40,15 +40,16 @@ pub enum ValueOrLookupRef {
LookupRef(LookupRef), LookupRef(LookupRef),
} }
#[derive(Clone, Debug, PartialEq)]
pub enum OpType {
Add,
Retract,
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Entity { pub enum Entity {
Add { AddOrRetract {
e: EntidOrLookupRef, op: OpType,
a: Entid,
v: ValueOrLookupRef,
tx: Option<Entid>,
},
Retract {
e: EntidOrLookupRef, e: EntidOrLookupRef,
a: Entid, a: Entid,
v: ValueOrLookupRef, v: ValueOrLookupRef,