Compare commits

...

2 commits

Author SHA1 Message Date
Andrew Jeffery
589375e707 Example of make in the API 2022-03-02 16:46:49 +00:00
Andrew Jeffery
598926f6e8 Add make to transaction API 2022-03-02 16:46:49 +00:00
4 changed files with 139 additions and 31 deletions

View file

@ -5,7 +5,7 @@ use crate::{
change::export_change, transaction::TransactionInner, ActorId, Automerge, AutomergeError, change::export_change, transaction::TransactionInner, ActorId, Automerge, AutomergeError,
Change, ChangeHash, Prop, Value, Change, ChangeHash, Prop, Value,
}; };
use crate::{Keys, KeysAt, SyncMessage, SyncState}; use crate::{Keys, KeysAt, ObjType, ScalarValue, SyncMessage, SyncState};
/// An automerge document that automatically manages transactions. /// An automerge document that automatically manages transactions.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -325,28 +325,50 @@ impl Transactable for AutoCommit {
/// - The object does not exist /// - The object does not exist
/// - The key is the wrong type for the object /// - The key is the wrong type for the object
/// - The key does not exist in the object /// - The key does not exist in the object
fn set<P: Into<Prop>, V: Into<Value>>( fn set<P: Into<Prop>, V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
prop: P, prop: P,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
self.ensure_transaction_open(); self.ensure_transaction_open();
let tx = self.transaction.as_mut().unwrap(); let tx = self.transaction.as_mut().unwrap();
tx.set(&mut self.doc, obj, prop, value) tx.set(&mut self.doc, obj, prop, value)
} }
fn insert<V: Into<Value>>( fn make<P: Into<Prop>, V: Into<ObjType>>(
&mut self,
obj: &ExId,
prop: P,
value: V,
) -> Result<ExId, AutomergeError> {
self.ensure_transaction_open();
let tx = self.transaction.as_mut().unwrap();
tx.make(&mut self.doc, obj, prop, value)
}
fn insert<V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
index: usize, index: usize,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
self.ensure_transaction_open(); self.ensure_transaction_open();
let tx = self.transaction.as_mut().unwrap(); let tx = self.transaction.as_mut().unwrap();
tx.insert(&mut self.doc, obj, index, value) tx.insert(&mut self.doc, obj, index, value)
} }
fn make_insert<V: Into<ObjType>>(
&mut self,
obj: &ExId,
index: usize,
value: V,
) -> Result<ExId, AutomergeError> {
self.ensure_transaction_open();
let tx = self.transaction.as_mut().unwrap();
tx.make_insert(&mut self.doc, obj, index, value)
}
fn inc<P: Into<Prop>>( fn inc<P: Into<Prop>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,

View file

@ -2,7 +2,7 @@ use crate::exid::ExId;
use crate::query::{self, OpIdSearch}; use crate::query::{self, OpIdSearch};
use crate::types::{Key, ObjId, OpId}; use crate::types::{Key, ObjId, OpId};
use crate::{change::export_change, types::Op, Automerge, ChangeHash, Prop, Value}; use crate::{change::export_change, types::Op, Automerge, ChangeHash, Prop, Value};
use crate::{AutomergeError, OpType}; use crate::{AutomergeError, ObjType, OpType, ScalarValue};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TransactionInner { pub struct TransactionInner {
@ -74,20 +74,43 @@ impl TransactionInner {
/// - The object does not exist /// - The object does not exist
/// - The key is the wrong type for the object /// - The key is the wrong type for the object
/// - The key does not exist in the object /// - The key does not exist in the object
pub fn set<P: Into<Prop>, V: Into<Value>>( pub fn set<P: Into<Prop>, V: Into<ScalarValue>>(
&mut self, &mut self,
doc: &mut Automerge, doc: &mut Automerge,
obj: &ExId, obj: &ExId,
prop: P, prop: P,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
let obj = doc.exid_to_obj(obj)?; let obj = doc.exid_to_obj(obj)?;
let value = value.into(); let value = Value::Scalar(value.into());
if let Some(id) = self.local_op(doc, obj, prop.into(), value.into())? { self.local_op(doc, obj, prop.into(), value.into())?;
Ok(Some(doc.id_to_exid(id))) Ok(())
} else { }
Ok(None)
} /// Set the value of property `P` to value `V` in object `obj`.
///
/// # Returns
///
/// The opid of the operation which was created, or None if this operation doesn't change the
/// document
///
/// # Errors
///
/// This will return an error if
/// - The object does not exist
/// - The key is the wrong type for the object
/// - The key does not exist in the object
pub fn make<P: Into<Prop>, V: Into<ObjType>>(
&mut self,
doc: &mut Automerge,
obj: &ExId,
prop: P,
value: V,
) -> Result<ExId, AutomergeError> {
let obj = doc.exid_to_obj(obj)?;
let value = Value::Object(value.into());
let id = self.local_op(doc, obj, prop.into(), value.into())?.unwrap();
Ok(doc.id_to_exid(id))
} }
fn next_id(&mut self) -> OpId { fn next_id(&mut self) -> OpId {
@ -108,19 +131,30 @@ impl TransactionInner {
self.operations.push(op); self.operations.push(op);
} }
pub fn insert<V: Into<Value>>( pub fn insert<V: Into<ScalarValue>>(
&mut self, &mut self,
doc: &mut Automerge, doc: &mut Automerge,
obj: &ExId, obj: &ExId,
index: usize, index: usize,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
let obj = doc.exid_to_obj(obj)?; let obj = doc.exid_to_obj(obj)?;
if let Some(id) = self.do_insert(doc, obj, index, value)? { self.do_insert(doc, obj, index, Value::Scalar(value.into()))?;
Ok(Some(doc.id_to_exid(id))) Ok(())
} else { }
Ok(None)
} pub fn make_insert<V: Into<ObjType>>(
&mut self,
doc: &mut Automerge,
obj: &ExId,
index: usize,
value: V,
) -> Result<ExId, AutomergeError> {
let obj = doc.exid_to_obj(obj)?;
let id = self
.do_insert(doc, obj, index, Value::Object(value.into()))?
.unwrap();
Ok(doc.id_to_exid(id))
} }
fn do_insert<V: Into<Value>>( fn do_insert<V: Into<Value>>(

View file

@ -1,5 +1,5 @@
use crate::exid::ExId; use crate::exid::ExId;
use crate::{Automerge, ChangeHash, KeysAt, Prop, Value}; use crate::{Automerge, ChangeHash, KeysAt, ObjType, Prop, ScalarValue, Value};
use crate::{AutomergeError, Keys}; use crate::{AutomergeError, Keys};
use super::{CommitOptions, Transactable, TransactionInner}; use super::{CommitOptions, Transactable, TransactionInner};
@ -85,27 +85,51 @@ impl<'a> Transactable for Transaction<'a> {
/// - The object does not exist /// - The object does not exist
/// - The key is the wrong type for the object /// - The key is the wrong type for the object
/// - The key does not exist in the object /// - The key does not exist in the object
fn set<P: Into<Prop>, V: Into<Value>>( fn set<P: Into<Prop>, V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
prop: P, prop: P,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
self.inner.as_mut().unwrap().set(self.doc, obj, prop, value) self.inner.as_mut().unwrap().set(self.doc, obj, prop, value)
} }
fn insert<V: Into<Value>>( fn make<P: Into<Prop>, V: Into<ObjType>>(
&mut self,
obj: &ExId,
prop: P,
value: V,
) -> Result<ExId, AutomergeError> {
self.inner
.as_mut()
.unwrap()
.make(self.doc, obj, prop, value)
}
fn insert<V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
index: usize, index: usize,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError> { ) -> Result<(), AutomergeError> {
self.inner self.inner
.as_mut() .as_mut()
.unwrap() .unwrap()
.insert(self.doc, obj, index, value) .insert(self.doc, obj, index, value)
} }
fn make_insert<V: Into<ObjType>>(
&mut self,
obj: &ExId,
index: usize,
value: V,
) -> Result<ExId, AutomergeError> {
self.inner
.as_mut()
.unwrap()
.make_insert(self.doc, obj, index, value)
}
fn inc<P: Into<Prop>>( fn inc<P: Into<Prop>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,

View file

@ -1,5 +1,5 @@
use crate::exid::ExId; use crate::exid::ExId;
use crate::{AutomergeError, ChangeHash, Keys, KeysAt, Prop, Value}; use crate::{AutomergeError, ChangeHash, Keys, KeysAt, ObjType, Prop, ScalarValue, Value};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
/// A way of mutating a document within a single change. /// A way of mutating a document within a single change.
@ -20,20 +20,48 @@ pub trait Transactable {
/// - The object does not exist /// - The object does not exist
/// - The key is the wrong type for the object /// - The key is the wrong type for the object
/// - The key does not exist in the object /// - The key does not exist in the object
fn set<P: Into<Prop>, V: Into<Value>>( fn set<P: Into<Prop>, V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
prop: P, prop: P,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError>; ) -> Result<(), AutomergeError>;
/// Set the value of property `P` to value `V` in object `obj`.
///
/// # Returns
///
/// The opid of the operation which was created, or None if this operation doesn't change the
/// document
///
/// # Errors
///
/// This will return an error if
/// - The object does not exist
/// - The key is the wrong type for the object
/// - The key does not exist in the object
fn make<P: Into<Prop>, V: Into<ObjType>>(
&mut self,
obj: &ExId,
prop: P,
value: V,
) -> Result<ExId, AutomergeError>;
/// Insert a value into a list at the given index. /// Insert a value into a list at the given index.
fn insert<V: Into<Value>>( fn insert<V: Into<ScalarValue>>(
&mut self, &mut self,
obj: &ExId, obj: &ExId,
index: usize, index: usize,
value: V, value: V,
) -> Result<Option<ExId>, AutomergeError>; ) -> Result<(), AutomergeError>;
/// Insert a value into a list at the given index.
fn make_insert<V: Into<ObjType>>(
&mut self,
obj: &ExId,
index: usize,
value: V,
) -> Result<ExId, AutomergeError>;
/// Increment the counter at the prop in the object by `value`. /// Increment the counter at the prop in the object by `value`.
fn inc<P: Into<Prop>>(&mut self, obj: &ExId, prop: P, value: i64) fn inc<P: Into<Prop>>(&mut self, obj: &ExId, prop: P, value: i64)