refactor: rename LoroValue::Unresolved to Container

This commit is contained in:
Zixuan Chen 2023-07-04 12:10:36 +08:00
parent 2cbe21463c
commit 38ee1e5a78
7 changed files with 39 additions and 39 deletions

View file

@ -67,7 +67,7 @@ impl ListContainer {
if let Some(prelim) = maybe_container { if let Some(prelim) = maybe_container {
let type_ = value.into_container().unwrap(); let type_ = value.into_container().unwrap();
let (id, idx) = txn.register_container(&self.id, type_); 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); self.insert_value(txn, pos, value);
prelim.integrate(txn, idx)?; prelim.integrate(txn, idx)?;
Ok(Some(idx)) Ok(Some(idx))
@ -185,7 +185,7 @@ impl ListContainer {
continue; continue;
} }
for value in self.raw_data.slice(range).iter() { 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); debug_log::debug_log!("Deleted {:?}", container_id);
hierarchy.remove_child(&self.id, container_id); hierarchy.remove_child(&self.id, container_id);
ans.push(container_id.as_ref().clone()); ans.push(container_id.as_ref().clone());
@ -221,7 +221,7 @@ impl ListContainer {
for values in self.state.iter() { for values in self.state.iter() {
let value = values.as_ref(); let value = values.as_ref();
for v in self.raw_data.slice(&value.0) { 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)); return Some(Index::Seq(idx));
} }
@ -234,7 +234,7 @@ impl ListContainer {
fn update_hierarchy_on_insert(&mut self, hierarchy: &mut Hierarchy, content: &SliceRange) { fn update_hierarchy_on_insert(&mut self, hierarchy: &mut Hierarchy, content: &SliceRange) {
for value in self.raw_data.slice(&content.0).iter() { 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); hierarchy.add_child(&self.id, container_id);
} }
} }
@ -430,7 +430,7 @@ impl ContainerTrait for ListContainer {
let range = state.get_sliced(); let range = state.get_sliced();
if !range.is_unknown() { if !range.is_unknown() {
for value in self.raw_data.slice(&range.0).iter() { 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); debug_log::debug_log!("Deleted {:?}", container_id);
hierarchy.remove_child(&self.id, container_id); hierarchy.remove_child(&self.id, container_id);
} }
@ -454,7 +454,7 @@ impl ContainerTrait for ListContainer {
if !content.is_unknown() { if !content.is_unknown() {
for value in self.raw_data.slice(&content.0).iter() { for value in self.raw_data.slice(&content.0).iter() {
// update hierarchy // update hierarchy
if let LoroValue::Unresolved(container_id) = value { if let LoroValue::Container(container_id) = value {
hierarchy.add_child(&self.id, container_id); hierarchy.add_child(&self.id, container_id);
} }
} }
@ -528,7 +528,7 @@ impl ContainerTrait for ListContainer {
) { ) {
if let StateContent::List { pool, state_len } = state_content { if let StateContent::List { pool, state_len } = state_content {
for v in pool.iter() { 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()); hierarchy.add_child(self.id(), child_container_id.as_ref());
} }
} }

View file

@ -83,7 +83,7 @@ impl MapContainer {
if let Some(prelim) = maybe_container { if let Some(prelim) = maybe_container {
let type_ = value.into_container().unwrap(); let type_ = value.into_container().unwrap();
let (id, idx) = txn.register_container(self.id(), type_); 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)?; prelim.integrate(txn, idx)?;
Ok(Some(idx)) Ok(Some(idx))
} else { } else {
@ -150,7 +150,7 @@ impl MapContainer {
) -> Option<ContainerID> { ) -> Option<ContainerID> {
if let Some(old_value) = self.state.get(key) { if let Some(old_value) = self.state.get(key) {
let v = &self.pool[old_value.value]; 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); h.remove_child(&self.id, container);
return Some(container.as_ref().clone()); return Some(container.as_ref().clone());
} }
@ -161,7 +161,7 @@ impl MapContainer {
pub fn index_of_child(&self, child: &ContainerID) -> Option<Index> { pub fn index_of_child(&self, child: &ContainerID) -> Option<Index> {
for (key, value) in self.state.iter() { for (key, value) in self.state.iter() {
if self.pool[value.value] if self.pool[value.value]
.as_unresolved() .as_container()
.map(|x| &**x == child) .map(|x| &**x == child)
.unwrap_or(false) .unwrap_or(false)
{ {
@ -224,11 +224,11 @@ impl ContainerTrait for MapContainer {
for (key, value) in self.state.iter() { for (key, value) in self.state.iter() {
let index = value.value; let index = value.value;
let value = self.pool.slice(&(index..index + 1))[0].clone(); 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( map.insert(
key.to_string(), key.to_string(),
// TODO: make a from // TODO: make a from
LoroValue::Unresolved(container_id.clone()), LoroValue::Container(container_id.clone()),
); );
} else { } else {
map.insert(key.to_string(), value); map.insert(key.to_string(), value);
@ -293,10 +293,10 @@ impl ContainerTrait for MapContainer {
} }
let old_val = &self.pool[slot.value]; 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); 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); hierarchy.add_child(&self.id, container);
} }
@ -313,7 +313,7 @@ impl ContainerTrait for MapContainer {
ctx.push_diff(&self.id, Diff::Map(map_diff)); 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); hierarchy.add_child(&self.id, container);
} }
@ -402,7 +402,7 @@ impl ContainerTrait for MapContainer {
) { ) {
if let StateContent::Map { pool, keys, values } = state_content { if let StateContent::Map { pool, keys, values } = state_content {
for v in pool.iter() { 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()); hierarchy.add_child(self.id(), child_container_id.as_ref());
} }
} }

View file

@ -452,19 +452,19 @@ pub trait ContainerWrapper {
match &mut value { match &mut value {
LoroValue::List(list) => { LoroValue::List(list) => {
list.iter_mut().for_each(|x| { list.iter_mut().for_each(|x| {
if x.as_unresolved().is_some() { if x.as_container().is_some() {
*x = x.clone().resolve_deep(reg) *x = x.clone().resolve_deep(reg)
} }
}); });
} }
LoroValue::Map(map) => { LoroValue::Map(map) => {
map.iter_mut().for_each(|(_, x)| { map.iter_mut().for_each(|(_, x)| {
if x.as_unresolved().is_some() { if x.as_container().is_some() {
*x = x.clone().resolve_deep(reg) *x = x.clone().resolve_deep(reg)
} }
}); });
} }
LoroValue::Unresolved(_) => unreachable!(), LoroValue::Container(_) => unreachable!(),
_ => {} _ => {}
} }

View file

@ -33,7 +33,7 @@ where
{ {
fn convert_value(self) -> Result<(PrelimValue, Option<Self>), LoroError> { fn convert_value(self) -> Result<(PrelimValue, Option<Self>), LoroError> {
let value: LoroValue = self.into(); let value: LoroValue = self.into();
if let LoroValue::Unresolved(_) = value { if let LoroValue::Container(_) = value {
return Err(LoroError::PrelimError); return Err(LoroError::PrelimError);
} }
Ok((PrelimValue::Value(value), None)) Ok((PrelimValue::Value(value), None))

View file

@ -87,7 +87,7 @@ impl ListState {
let mapping: ContainerMapping = Arc::new(Mutex::new(Default::default())); let mapping: ContainerMapping = Arc::new(Mutex::new(Default::default()));
let mapping_clone = mapping.clone(); let mapping_clone = mapping.clone();
tree.set_listener(Some(Box::new(move |event| { 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(); let mut mapping = mapping_clone.lock().unwrap();
if let Some(leaf) = event.target_leaf { if let Some(leaf) = event.target_leaf {
mapping.insert((**container_id).clone(), leaf); mapping.insert((**container_id).clone(), leaf);
@ -110,7 +110,7 @@ impl ListState {
let elem_index = node let elem_index = node
.elements() .elements()
.iter() .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; let mut index = 0;
self.list.visit_previous_caches( self.list.visit_previous_caches(
QueryResult { QueryResult {
@ -203,8 +203,8 @@ mod test {
fn id(name: &str) -> ContainerID { fn id(name: &str) -> ContainerID {
ContainerID::new_root(name, crate::ContainerType::List) ContainerID::new_root(name, crate::ContainerType::List)
} }
list.insert(0, LoroValue::Unresolved(Box::new(id("abc")))); list.insert(0, LoroValue::Container(Box::new(id("abc"))));
list.insert(0, LoroValue::Unresolved(Box::new(id("x")))); 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("x")), Some(0));
assert_eq!(list.get_child_container_index(&id("abc")), Some(1)); assert_eq!(list.get_child_container_index(&id("abc")), Some(1));
list.insert(1, LoroValue::Bool(false)); list.insert(1, LoroValue::Bool(false));

View file

@ -22,7 +22,7 @@ pub enum LoroValue {
String(Box<str>), String(Box<str>),
List(Box<Vec<LoroValue>>), List(Box<Vec<LoroValue>>),
Map(Box<FxHashMap<String, LoroValue>>), Map(Box<FxHashMap<String, LoroValue>>),
Unresolved(Box<ContainerID>), Container(Box<ContainerID>),
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -36,19 +36,19 @@ impl LoroValue {
match &mut self { match &mut self {
LoroValue::List(list) => { LoroValue::List(list) => {
for v in list.iter_mut() { for v in list.iter_mut() {
if v.as_unresolved().is_some() { if v.as_container().is_some() {
*v = v.clone().resolve_deep(reg) *v = v.clone().resolve_deep(reg)
} }
} }
} }
LoroValue::Map(map) => { LoroValue::Map(map) => {
for v in map.values_mut() { for v in map.values_mut() {
if v.as_unresolved().is_some() { if v.as_container().is_some() {
*v = v.clone().resolve_deep(reg) *v = v.clone().resolve_deep(reg)
} }
} }
} }
LoroValue::Unresolved(id) => { LoroValue::Container(id) => {
self = reg self = reg
.get(id) .get(id)
.map(|container| { .map(|container| {
@ -58,19 +58,19 @@ impl LoroValue {
match &mut value { match &mut value {
LoroValue::List(list) => { LoroValue::List(list) => {
for v in list.iter_mut() { for v in list.iter_mut() {
if v.as_unresolved().is_some() { if v.as_container().is_some() {
*v = v.clone().resolve_deep(reg) *v = v.clone().resolve_deep(reg)
} }
} }
} }
LoroValue::Map(map) => { LoroValue::Map(map) => {
for v in map.values_mut() { for v in map.values_mut() {
if v.as_unresolved().is_some() { if v.as_container().is_some() {
*v = v.clone().resolve_deep(reg) *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 { pub fn to_json_value(&self, reg: &ContainerRegistry) -> LoroValue {
match self { 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(), _ => self.clone(),
} }
} }
@ -183,7 +183,7 @@ impl From<String> for LoroValue {
impl From<ContainerID> for LoroValue { impl From<ContainerID> for LoroValue {
fn from(v: ContainerID) -> Self { 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 { 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() { match container.container_type() {
crate::ContainerType::Text => LoroValue::String(Default::default()), crate::ContainerType::Text => LoroValue::String(Default::default()),
crate::ContainerType::Map => LoroValue::Map(Default::default()), crate::ContainerType::Map => LoroValue::Map(Default::default()),
@ -356,7 +356,7 @@ pub mod wasm {
map.into_js_result().unwrap() 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::String(s) => serializer.serialize_str(s),
LoroValue::List(l) => serializer.collect_seq(l.iter()), LoroValue::List(l) => serializer.collect_seq(l.iter()),
LoroValue::Map(m) => serializer.collect_map(m.iter()), LoroValue::Map(m) => serializer.collect_map(m.iter()),
LoroValue::Unresolved(id) => { LoroValue::Container(id) => {
let mut state = serializer.serialize_struct("Unresolved", 1)?; let mut state = serializer.serialize_struct("Unresolved", 1)?;
state.serialize_field("Unresolved", id)?; state.serialize_field("Unresolved", id)?;
state.end() state.end()
@ -735,7 +735,7 @@ impl Serialize for LoroValue {
serializer.serialize_newtype_variant("LoroValue", 5, "List", l) serializer.serialize_newtype_variant("LoroValue", 5, "List", l)
} }
LoroValue::Map(m) => serializer.serialize_newtype_variant("LoroValue", 6, "Map", m), 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) 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::String, v) => v.newtype_variant().map(LoroValue::String),
(LoroValueFields::List, v) => v.newtype_variant().map(LoroValue::List), (LoroValueFields::List, v) => v.newtype_variant().map(LoroValue::List),
(LoroValueFields::Map, v) => v.newtype_variant().map(LoroValue::Map), (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),
} }
} }
} }

View file

@ -166,7 +166,7 @@ fn map() {
assert_eq!(value.as_map().unwrap().len(), 2); assert_eq!(value.as_map().unwrap().len(), 2);
let map = value.as_map().unwrap(); let map = value.as_map().unwrap();
assert_eq!(*map.get("haha").unwrap().as_double().unwrap(), 1.2); 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()); println!("{}", value.to_json());
let deep_value = root.get_value_deep(&loro); let deep_value = root.get_value_deep(&loro);
assert_eq!(deep_value.as_map().unwrap().len(), 2); assert_eq!(deep_value.as_map().unwrap().len(), 2);