mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-23 05:24:51 +00:00
refactor: change loro value type
This commit is contained in:
parent
de84a633f9
commit
2ae4db14f6
6 changed files with 45 additions and 21 deletions
|
@ -127,7 +127,7 @@ impl Container for MapContainer {
|
|||
for (key, value) in self.state.iter() {
|
||||
map.insert(key.clone(), value.value.clone().into());
|
||||
}
|
||||
LoroValue::Map(map)
|
||||
LoroValue::Map(Box::new(map))
|
||||
}
|
||||
|
||||
fn checkout_version(&mut self, _vv: &crate::version::VersionVector) {
|
||||
|
|
|
@ -22,7 +22,7 @@ fn basic() {
|
|||
"haha".into() => LoroValue::Integer(1)
|
||||
);
|
||||
|
||||
assert_eq!(container.get_value(), LoroValue::Map(ans));
|
||||
assert_eq!(container.get_value(), LoroValue::Map(Box::new(ans)));
|
||||
}
|
||||
|
||||
mod map_proptest {
|
||||
|
|
|
@ -223,7 +223,7 @@ impl Container for TextContainer {
|
|||
);
|
||||
debug_log!(
|
||||
"BEFORE EFFECT STATE={}",
|
||||
self.get_value().as_string().unwrap().as_str()
|
||||
self.get_value().as_string().unwrap()
|
||||
);
|
||||
for effect in self.tracker.iter_effects(path.right) {
|
||||
debug_log!("EFFECT: {:?}", &effect);
|
||||
|
@ -237,7 +237,7 @@ impl Container for TextContainer {
|
|||
}
|
||||
debug_log!(
|
||||
"AFTER EFFECT STATE={}",
|
||||
self.get_value().as_string().unwrap().as_str()
|
||||
self.get_value().as_string().unwrap()
|
||||
);
|
||||
|
||||
self.head.push(new_op_id);
|
||||
|
@ -251,13 +251,13 @@ impl Container for TextContainer {
|
|||
|
||||
// TODO: maybe we need to let this return Cow
|
||||
fn get_value(&self) -> LoroValue {
|
||||
let mut ans_str = SmString::new();
|
||||
let mut ans_str = String::new();
|
||||
for v in self.state.iter() {
|
||||
let content = v.as_ref();
|
||||
ans_str.push_str(&self.raw_str.get_str(content));
|
||||
}
|
||||
|
||||
LoroValue::String(ans_str)
|
||||
LoroValue::String(ans_str.into_boxed_str())
|
||||
}
|
||||
|
||||
fn to_export(&self, op: &mut Op) {
|
||||
|
|
|
@ -283,7 +283,7 @@ pub fn test_single_client(mut actions: Vec<Action>) {
|
|||
text_container.apply_action(action);
|
||||
assert_eq!(
|
||||
ground_truth.as_str(),
|
||||
text_container.get_value().as_string().unwrap().as_str(),
|
||||
&**text_container.get_value().as_string().unwrap(),
|
||||
"{}",
|
||||
applied.table()
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use enum_as_inner::EnumAsInner;
|
||||
use fxhash::FxHashMap;
|
||||
|
||||
use crate::{container::ContainerID, smstring::SmString, InternalString};
|
||||
use crate::{container::ContainerID, InternalString};
|
||||
|
||||
/// [LoroValue] is used to represents the state of CRDT at a given version
|
||||
#[derive(Debug, PartialEq, Clone, serde::Serialize, EnumAsInner)]
|
||||
|
@ -10,10 +10,10 @@ pub enum LoroValue {
|
|||
Bool(bool),
|
||||
Double(f64),
|
||||
Integer(i32),
|
||||
String(SmString),
|
||||
List(Vec<LoroValue>),
|
||||
Map(FxHashMap<InternalString, LoroValue>),
|
||||
Unresolved(ContainerID),
|
||||
String(Box<str>),
|
||||
List(Box<Vec<LoroValue>>),
|
||||
Map(Box<FxHashMap<InternalString, LoroValue>>),
|
||||
Unresolved(Box<ContainerID>),
|
||||
}
|
||||
|
||||
impl Default for LoroValue {
|
||||
|
@ -22,6 +22,18 @@ impl Default for LoroValue {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<FxHashMap<InternalString, LoroValue>> for LoroValue {
|
||||
fn from(map: FxHashMap<InternalString, LoroValue>) -> Self {
|
||||
LoroValue::Map(Box::new(map))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<LoroValue>> for LoroValue {
|
||||
fn from(vec: Vec<LoroValue>) -> Self {
|
||||
LoroValue::List(Box::new(vec))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<InsertValue> for LoroValue {
|
||||
fn from(v: InsertValue) -> Self {
|
||||
match v {
|
||||
|
@ -57,8 +69,8 @@ pub enum InsertValue {
|
|||
Bool(bool),
|
||||
Double(f64),
|
||||
Int32(i32),
|
||||
String(SmString),
|
||||
Container(ContainerID),
|
||||
String(Box<str>),
|
||||
Container(Box<ContainerID>),
|
||||
}
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
|
@ -79,7 +91,7 @@ pub mod wasm {
|
|||
LoroValue::String(s) => JsValue::from_str(&s),
|
||||
LoroValue::List(list) => {
|
||||
let arr = Array::new_with_length(list.len() as u32);
|
||||
for v in list {
|
||||
for v in list.into_iter() {
|
||||
arr.push(&convert(v));
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,18 @@ fn map() {
|
|||
let mut loro = LoroCore::new(Default::default(), Some(10));
|
||||
let mut root = loro.get_or_create_root_map("root").unwrap();
|
||||
root.insert("haha".into(), InsertValue::Double(1.2));
|
||||
let value = root.get_value();
|
||||
assert_eq!(value.as_map().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
*value
|
||||
.as_map()
|
||||
.unwrap()
|
||||
.get(&"haha".into())
|
||||
.unwrap()
|
||||
.as_double()
|
||||
.unwrap(),
|
||||
1.2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -18,7 +30,7 @@ fn two_client_text_sync() {
|
|||
text_container.insert(1, "56");
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "0563412");
|
||||
assert_eq!(&**value, "0563412");
|
||||
drop(text_container);
|
||||
|
||||
let mut store_b = LoroCore::new(Default::default(), Some(11));
|
||||
|
@ -28,25 +40,25 @@ fn two_client_text_sync() {
|
|||
text_container.check();
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "0563412");
|
||||
assert_eq!(&**value, "0563412");
|
||||
|
||||
text_container.delete(0, 2);
|
||||
text_container.insert(4, "789");
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "63417892");
|
||||
assert_eq!(&**value, "63417892");
|
||||
drop(text_container);
|
||||
|
||||
store.import(store_b.export(store.vv()));
|
||||
let mut text_container = store.get_or_create_root_text("haha").unwrap();
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "63417892");
|
||||
assert_eq!(&**value, "63417892");
|
||||
text_container.delete(0, 8);
|
||||
text_container.insert(0, "abc");
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "abc");
|
||||
assert_eq!(&**value, "abc");
|
||||
drop(text_container);
|
||||
|
||||
store_b.import(store.export(Default::default()));
|
||||
|
@ -54,7 +66,7 @@ fn two_client_text_sync() {
|
|||
text_container.check();
|
||||
let value = text_container.get_value();
|
||||
let value = value.as_string().unwrap();
|
||||
assert_eq!(value.as_str(), "abc");
|
||||
assert_eq!(&**value, "abc");
|
||||
}
|
||||
|
||||
#[ctor]
|
||||
|
|
Loading…
Reference in a new issue