From 598926f6e809374a116aa123cfe97c3424984e07 Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Wed, 2 Mar 2022 16:10:13 +0000 Subject: [PATCH 1/2] Add make to transaction API --- automerge/src/autocommit.rs | 17 ++++++-- automerge/src/transaction/inner.rs | 41 +++++++++++++++---- .../src/transaction/manual_transaction.rs | 18 ++++++-- automerge/src/transaction/transactable.rs | 26 ++++++++++-- 4 files changed, 84 insertions(+), 18 deletions(-) diff --git a/automerge/src/autocommit.rs b/automerge/src/autocommit.rs index 3d833266..e7910370 100644 --- a/automerge/src/autocommit.rs +++ b/automerge/src/autocommit.rs @@ -5,7 +5,7 @@ use crate::{ change::export_change, transaction::TransactionInner, ActorId, Automerge, AutomergeError, 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. #[derive(Debug, Clone)] @@ -325,17 +325,28 @@ impl Transactable for AutoCommit { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.ensure_transaction_open(); let tx = self.transaction.as_mut().unwrap(); tx.set(&mut self.doc, obj, prop, value) } + fn make, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + self.ensure_transaction_open(); + let tx = self.transaction.as_mut().unwrap(); + tx.make(&mut self.doc, obj, prop, value) + } + fn insert>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/inner.rs b/automerge/src/transaction/inner.rs index 4aec42de..c0782e02 100644 --- a/automerge/src/transaction/inner.rs +++ b/automerge/src/transaction/inner.rs @@ -2,7 +2,7 @@ use crate::exid::ExId; use crate::query::{self, OpIdSearch}; use crate::types::{Key, ObjId, OpId}; use crate::{change::export_change, types::Op, Automerge, ChangeHash, Prop, Value}; -use crate::{AutomergeError, OpType}; +use crate::{AutomergeError, ObjType, OpType, ScalarValue}; #[derive(Debug, Clone)] pub struct TransactionInner { @@ -74,20 +74,43 @@ impl TransactionInner { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - pub fn set, V: Into>( + pub fn set, V: Into>( &mut self, doc: &mut Automerge, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { let obj = doc.exid_to_obj(obj)?; - let value = value.into(); - if let Some(id) = self.local_op(doc, obj, prop.into(), value.into())? { - Ok(Some(doc.id_to_exid(id))) - } else { - Ok(None) - } + let value = Value::Scalar(value.into()); + self.local_op(doc, obj, prop.into(), value.into())?; + Ok(()) + } + + /// 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, V: Into>( + &mut self, + doc: &mut Automerge, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + 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 { diff --git a/automerge/src/transaction/manual_transaction.rs b/automerge/src/transaction/manual_transaction.rs index 93dcf615..144db19b 100644 --- a/automerge/src/transaction/manual_transaction.rs +++ b/automerge/src/transaction/manual_transaction.rs @@ -1,5 +1,5 @@ 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 super::{CommitOptions, Transactable, TransactionInner}; @@ -85,15 +85,27 @@ impl<'a> Transactable for Transaction<'a> { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.inner.as_mut().unwrap().set(self.doc, obj, prop, value) } + fn make, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + self.inner + .as_mut() + .unwrap() + .make(self.doc, obj, prop, value) + } + fn insert>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/transactable.rs b/automerge/src/transaction/transactable.rs index 93d65cd1..f3546ad7 100644 --- a/automerge/src/transaction/transactable.rs +++ b/automerge/src/transaction/transactable.rs @@ -1,5 +1,5 @@ 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; /// A way of mutating a document within a single change. @@ -20,12 +20,32 @@ pub trait Transactable { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, 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, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result; /// Insert a value into a list at the given index. fn insert>( From 589375e707c6e489f2f257ab0cb760c6b3485294 Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Wed, 2 Mar 2022 16:45:50 +0000 Subject: [PATCH 2/2] Example of make in the API --- automerge/src/autocommit.rs | 15 +++++++++-- automerge/src/transaction/inner.rs | 25 +++++++++++++------ .../src/transaction/manual_transaction.rs | 16 ++++++++++-- automerge/src/transaction/transactable.rs | 12 +++++++-- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/automerge/src/autocommit.rs b/automerge/src/autocommit.rs index e7910370..e84185e5 100644 --- a/automerge/src/autocommit.rs +++ b/automerge/src/autocommit.rs @@ -347,17 +347,28 @@ impl Transactable for AutoCommit { tx.make(&mut self.doc, obj, prop, value) } - fn insert>( + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.ensure_transaction_open(); let tx = self.transaction.as_mut().unwrap(); tx.insert(&mut self.doc, obj, index, value) } + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + self.ensure_transaction_open(); + let tx = self.transaction.as_mut().unwrap(); + tx.make_insert(&mut self.doc, obj, index, value) + } + fn inc>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/inner.rs b/automerge/src/transaction/inner.rs index c0782e02..453fc362 100644 --- a/automerge/src/transaction/inner.rs +++ b/automerge/src/transaction/inner.rs @@ -131,19 +131,30 @@ impl TransactionInner { self.operations.push(op); } - pub fn insert>( + pub fn insert>( &mut self, doc: &mut Automerge, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { let obj = doc.exid_to_obj(obj)?; - if let Some(id) = self.do_insert(doc, obj, index, value)? { - Ok(Some(doc.id_to_exid(id))) - } else { - Ok(None) - } + self.do_insert(doc, obj, index, Value::Scalar(value.into()))?; + Ok(()) + } + + pub fn make_insert>( + &mut self, + doc: &mut Automerge, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + 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>( diff --git a/automerge/src/transaction/manual_transaction.rs b/automerge/src/transaction/manual_transaction.rs index 144db19b..e06e1552 100644 --- a/automerge/src/transaction/manual_transaction.rs +++ b/automerge/src/transaction/manual_transaction.rs @@ -106,18 +106,30 @@ impl<'a> Transactable for Transaction<'a> { .make(self.doc, obj, prop, value) } - fn insert>( + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.inner .as_mut() .unwrap() .insert(self.doc, obj, index, value) } + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + self.inner + .as_mut() + .unwrap() + .make_insert(self.doc, obj, index, value) + } + fn inc>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/transactable.rs b/automerge/src/transaction/transactable.rs index f3546ad7..ea863af0 100644 --- a/automerge/src/transaction/transactable.rs +++ b/automerge/src/transaction/transactable.rs @@ -48,12 +48,20 @@ pub trait Transactable { ) -> Result; /// Insert a value into a list at the given index. - fn insert>( + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError>; + ) -> Result<(), AutomergeError>; + + /// Insert a value into a list at the given index. + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result; /// Increment the counter at the prop in the object by `value`. fn inc>(&mut self, obj: &ExId, prop: P, value: i64)