From 38ee1e5a788184bee6d1c728f7fb76f681656d31 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Tue, 4 Jul 2023 12:10:36 +0800 Subject: [PATCH] refactor: rename LoroValue::Unresolved to Container --- .../src/container/list/list_container.rs | 14 +++++----- .../src/container/map/map_container.rs | 18 ++++++------ .../loro-internal/src/container/registry.rs | 6 ++-- crates/loro-internal/src/prelim.rs | 2 +- .../src/refactor/state/list_state.rs | 8 +++--- crates/loro-internal/src/value.rs | 28 +++++++++---------- crates/loro-internal/tests/test.rs | 2 +- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/crates/loro-internal/src/container/list/list_container.rs b/crates/loro-internal/src/container/list/list_container.rs index e3ab94cd..7a378f39 100644 --- a/crates/loro-internal/src/container/list/list_container.rs +++ b/crates/loro-internal/src/container/list/list_container.rs @@ -67,7 +67,7 @@ impl ListContainer { if let Some(prelim) = maybe_container { let type_ = value.into_container().unwrap(); let (id, idx) = txn.register_container(&self.id, type_); - let value = LoroValue::Unresolved(id.into()); + let value = LoroValue::Container(id.into()); self.insert_value(txn, pos, value); prelim.integrate(txn, idx)?; Ok(Some(idx)) @@ -185,7 +185,7 @@ impl ListContainer { continue; } for value in self.raw_data.slice(range).iter() { - if let LoroValue::Unresolved(container_id) = value { + if let LoroValue::Container(container_id) = value { debug_log::debug_log!("Deleted {:?}", container_id); hierarchy.remove_child(&self.id, container_id); ans.push(container_id.as_ref().clone()); @@ -221,7 +221,7 @@ impl ListContainer { for values in self.state.iter() { let value = values.as_ref(); for v in self.raw_data.slice(&value.0) { - if v.as_unresolved().map(|x| &**x == child).unwrap_or(false) { + if v.as_container().map(|x| &**x == child).unwrap_or(false) { return Some(Index::Seq(idx)); } @@ -234,7 +234,7 @@ impl ListContainer { fn update_hierarchy_on_insert(&mut self, hierarchy: &mut Hierarchy, content: &SliceRange) { for value in self.raw_data.slice(&content.0).iter() { - if let LoroValue::Unresolved(container_id) = value { + if let LoroValue::Container(container_id) = value { hierarchy.add_child(&self.id, container_id); } } @@ -430,7 +430,7 @@ impl ContainerTrait for ListContainer { let range = state.get_sliced(); if !range.is_unknown() { for value in self.raw_data.slice(&range.0).iter() { - if let LoroValue::Unresolved(container_id) = value { + if let LoroValue::Container(container_id) = value { debug_log::debug_log!("Deleted {:?}", container_id); hierarchy.remove_child(&self.id, container_id); } @@ -454,7 +454,7 @@ impl ContainerTrait for ListContainer { if !content.is_unknown() { for value in self.raw_data.slice(&content.0).iter() { // update hierarchy - if let LoroValue::Unresolved(container_id) = value { + if let LoroValue::Container(container_id) = value { hierarchy.add_child(&self.id, container_id); } } @@ -528,7 +528,7 @@ impl ContainerTrait for ListContainer { ) { if let StateContent::List { pool, state_len } = state_content { for v in pool.iter() { - if let LoroValue::Unresolved(child_container_id) = v { + if let LoroValue::Container(child_container_id) = v { hierarchy.add_child(self.id(), child_container_id.as_ref()); } } diff --git a/crates/loro-internal/src/container/map/map_container.rs b/crates/loro-internal/src/container/map/map_container.rs index 5e5fabee..6f14546e 100644 --- a/crates/loro-internal/src/container/map/map_container.rs +++ b/crates/loro-internal/src/container/map/map_container.rs @@ -83,7 +83,7 @@ impl MapContainer { if let Some(prelim) = maybe_container { let type_ = value.into_container().unwrap(); let (id, idx) = txn.register_container(self.id(), type_); - self.insert_value(txn, key, LoroValue::Unresolved(id.into())); + self.insert_value(txn, key, LoroValue::Container(id.into())); prelim.integrate(txn, idx)?; Ok(Some(idx)) } else { @@ -150,7 +150,7 @@ impl MapContainer { ) -> Option { if let Some(old_value) = self.state.get(key) { let v = &self.pool[old_value.value]; - if let Some(container) = v.as_unresolved() { + if let Some(container) = v.as_container() { h.remove_child(&self.id, container); return Some(container.as_ref().clone()); } @@ -161,7 +161,7 @@ impl MapContainer { pub fn index_of_child(&self, child: &ContainerID) -> Option { for (key, value) in self.state.iter() { if self.pool[value.value] - .as_unresolved() + .as_container() .map(|x| &**x == child) .unwrap_or(false) { @@ -224,11 +224,11 @@ impl ContainerTrait for MapContainer { for (key, value) in self.state.iter() { let index = value.value; let value = self.pool.slice(&(index..index + 1))[0].clone(); - if let Some(container_id) = value.as_unresolved() { + if let Some(container_id) = value.as_container() { map.insert( key.to_string(), // TODO: make a from - LoroValue::Unresolved(container_id.clone()), + LoroValue::Container(container_id.clone()), ); } else { map.insert(key.to_string(), value); @@ -293,10 +293,10 @@ impl ContainerTrait for MapContainer { } let old_val = &self.pool[slot.value]; - if let Some(container) = old_val.as_unresolved() { + if let Some(container) = old_val.as_container() { hierarchy.remove_child(&self.id, container); } - if let Some(container) = new_value.as_unresolved() { + if let Some(container) = new_value.as_container() { hierarchy.add_child(&self.id, container); } @@ -313,7 +313,7 @@ impl ContainerTrait for MapContainer { ctx.push_diff(&self.id, Diff::Map(map_diff)); } - if let Some(container) = new_value.as_unresolved() { + if let Some(container) = new_value.as_container() { hierarchy.add_child(&self.id, container); } @@ -402,7 +402,7 @@ impl ContainerTrait for MapContainer { ) { if let StateContent::Map { pool, keys, values } = state_content { for v in pool.iter() { - if let LoroValue::Unresolved(child_container_id) = v { + if let LoroValue::Container(child_container_id) = v { hierarchy.add_child(self.id(), child_container_id.as_ref()); } } diff --git a/crates/loro-internal/src/container/registry.rs b/crates/loro-internal/src/container/registry.rs index 4e984796..376d102e 100644 --- a/crates/loro-internal/src/container/registry.rs +++ b/crates/loro-internal/src/container/registry.rs @@ -452,19 +452,19 @@ pub trait ContainerWrapper { match &mut value { LoroValue::List(list) => { list.iter_mut().for_each(|x| { - if x.as_unresolved().is_some() { + if x.as_container().is_some() { *x = x.clone().resolve_deep(reg) } }); } LoroValue::Map(map) => { map.iter_mut().for_each(|(_, x)| { - if x.as_unresolved().is_some() { + if x.as_container().is_some() { *x = x.clone().resolve_deep(reg) } }); } - LoroValue::Unresolved(_) => unreachable!(), + LoroValue::Container(_) => unreachable!(), _ => {} } diff --git a/crates/loro-internal/src/prelim.rs b/crates/loro-internal/src/prelim.rs index 65d0be0d..ca87646c 100644 --- a/crates/loro-internal/src/prelim.rs +++ b/crates/loro-internal/src/prelim.rs @@ -33,7 +33,7 @@ where { fn convert_value(self) -> Result<(PrelimValue, Option), LoroError> { let value: LoroValue = self.into(); - if let LoroValue::Unresolved(_) = value { + if let LoroValue::Container(_) = value { return Err(LoroError::PrelimError); } Ok((PrelimValue::Value(value), None)) diff --git a/crates/loro-internal/src/refactor/state/list_state.rs b/crates/loro-internal/src/refactor/state/list_state.rs index 2d03c7ab..53127a45 100644 --- a/crates/loro-internal/src/refactor/state/list_state.rs +++ b/crates/loro-internal/src/refactor/state/list_state.rs @@ -87,7 +87,7 @@ impl ListState { let mapping: ContainerMapping = Arc::new(Mutex::new(Default::default())); let mapping_clone = mapping.clone(); tree.set_listener(Some(Box::new(move |event| { - if let LoroValue::Unresolved(container_id) = event.elem { + if let LoroValue::Container(container_id) = event.elem { let mut mapping = mapping_clone.lock().unwrap(); if let Some(leaf) = event.target_leaf { mapping.insert((**container_id).clone(), leaf); @@ -110,7 +110,7 @@ impl ListState { let elem_index = node .elements() .iter() - .position(|x| x.as_unresolved().map(|x| &**x) == Some(id))?; + .position(|x| x.as_container().map(|x| &**x) == Some(id))?; let mut index = 0; self.list.visit_previous_caches( QueryResult { @@ -203,8 +203,8 @@ mod test { fn id(name: &str) -> ContainerID { ContainerID::new_root(name, crate::ContainerType::List) } - list.insert(0, LoroValue::Unresolved(Box::new(id("abc")))); - list.insert(0, LoroValue::Unresolved(Box::new(id("x")))); + list.insert(0, LoroValue::Container(Box::new(id("abc")))); + list.insert(0, LoroValue::Container(Box::new(id("x")))); assert_eq!(list.get_child_container_index(&id("x")), Some(0)); assert_eq!(list.get_child_container_index(&id("abc")), Some(1)); list.insert(1, LoroValue::Bool(false)); diff --git a/crates/loro-internal/src/value.rs b/crates/loro-internal/src/value.rs index 1645de97..9c373b8e 100644 --- a/crates/loro-internal/src/value.rs +++ b/crates/loro-internal/src/value.rs @@ -22,7 +22,7 @@ pub enum LoroValue { String(Box), List(Box>), Map(Box>), - Unresolved(Box), + Container(Box), } #[derive(Serialize, Deserialize)] @@ -36,19 +36,19 @@ impl LoroValue { match &mut self { LoroValue::List(list) => { for v in list.iter_mut() { - if v.as_unresolved().is_some() { + if v.as_container().is_some() { *v = v.clone().resolve_deep(reg) } } } LoroValue::Map(map) => { for v in map.values_mut() { - if v.as_unresolved().is_some() { + if v.as_container().is_some() { *v = v.clone().resolve_deep(reg) } } } - LoroValue::Unresolved(id) => { + LoroValue::Container(id) => { self = reg .get(id) .map(|container| { @@ -58,19 +58,19 @@ impl LoroValue { match &mut value { LoroValue::List(list) => { for v in list.iter_mut() { - if v.as_unresolved().is_some() { + if v.as_container().is_some() { *v = v.clone().resolve_deep(reg) } } } LoroValue::Map(map) => { for v in map.values_mut() { - if v.as_unresolved().is_some() { + if v.as_container().is_some() { *v = v.clone().resolve_deep(reg) } } } - LoroValue::Unresolved(_) => unreachable!(), + LoroValue::Container(_) => unreachable!(), _ => {} } @@ -99,7 +99,7 @@ impl LoroValue { pub fn to_json_value(&self, reg: &ContainerRegistry) -> LoroValue { match self { - LoroValue::Unresolved(_) => self.clone().resolve_deep(reg).to_json_value(reg), + LoroValue::Container(_) => self.clone().resolve_deep(reg).to_json_value(reg), _ => self.clone(), } } @@ -183,7 +183,7 @@ impl From for LoroValue { impl From for LoroValue { fn from(v: ContainerID) -> Self { - LoroValue::Unresolved(Box::new(v)) + LoroValue::Container(Box::new(v)) } } @@ -309,7 +309,7 @@ impl LoroValue { } fn unresolved_to_collection(v: &LoroValue) -> LoroValue { - if let Some(container) = v.as_unresolved() { + if let Some(container) = v.as_container() { match container.container_type() { crate::ContainerType::Text => LoroValue::String(Default::default()), crate::ContainerType::Map => LoroValue::Map(Default::default()), @@ -356,7 +356,7 @@ pub mod wasm { map.into_js_result().unwrap() } - LoroValue::Unresolved(container_id) => JsValue::from(*container_id), + LoroValue::Container(container_id) => JsValue::from(*container_id), } } @@ -711,7 +711,7 @@ impl Serialize for LoroValue { LoroValue::String(s) => serializer.serialize_str(s), LoroValue::List(l) => serializer.collect_seq(l.iter()), LoroValue::Map(m) => serializer.collect_map(m.iter()), - LoroValue::Unresolved(id) => { + LoroValue::Container(id) => { let mut state = serializer.serialize_struct("Unresolved", 1)?; state.serialize_field("Unresolved", id)?; state.end() @@ -735,7 +735,7 @@ impl Serialize for LoroValue { serializer.serialize_newtype_variant("LoroValue", 5, "List", l) } LoroValue::Map(m) => serializer.serialize_newtype_variant("LoroValue", 6, "Map", m), - LoroValue::Unresolved(id) => { + LoroValue::Container(id) => { serializer.serialize_newtype_variant("LoroValue", 7, "Unresolved", id) } } @@ -887,7 +887,7 @@ impl<'de> serde::de::Visitor<'de> for LoroValueEnumVisitor { (LoroValueFields::String, v) => v.newtype_variant().map(LoroValue::String), (LoroValueFields::List, v) => v.newtype_variant().map(LoroValue::List), (LoroValueFields::Map, v) => v.newtype_variant().map(LoroValue::Map), - (LoroValueFields::Unresolved, v) => v.newtype_variant().map(LoroValue::Unresolved), + (LoroValueFields::Unresolved, v) => v.newtype_variant().map(LoroValue::Container), } } } diff --git a/crates/loro-internal/tests/test.rs b/crates/loro-internal/tests/test.rs index 1a1fb1f0..be97e2e3 100644 --- a/crates/loro-internal/tests/test.rs +++ b/crates/loro-internal/tests/test.rs @@ -166,7 +166,7 @@ fn map() { assert_eq!(value.as_map().unwrap().len(), 2); let map = value.as_map().unwrap(); assert_eq!(*map.get("haha").unwrap().as_double().unwrap(), 1.2); - assert!(map.get("map").unwrap().as_unresolved().is_some()); + assert!(map.get("map").unwrap().as_container().is_some()); println!("{}", value.to_json()); let deep_value = root.get_value_deep(&loro); assert_eq!(deep_value.as_map().unwrap().len(), 2);