Skip to content

Commit

Permalink
chore: type option cell data trait cleanup (AppFlowy-IO#4782)
Browse files Browse the repository at this point in the history
* chore: remove dead code

* chore: remove legacy FromCellStringTrait
  • Loading branch information
richardshiue authored Feb 29, 2024
1 parent f4ca3ef commit 682bf19
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 264 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::ErrorCode;

Expand Down Expand Up @@ -48,7 +50,7 @@ impl FromFilterString for SelectOptionFilterPB {
where
Self: Sized,
{
let ids = SelectOptionIds::from(filter.content.clone());
let ids = SelectOptionIds::from_str(&filter.content).unwrap_or_default();
SelectOptionFilterPB {
condition: SelectOptionConditionPB::try_from(filter.condition as u8)
.unwrap_or(SelectOptionConditionPB::OptionIs),
Expand All @@ -59,7 +61,7 @@ impl FromFilterString for SelectOptionFilterPB {

impl std::convert::From<&Filter> for SelectOptionFilterPB {
fn from(filter: &Filter) -> Self {
let ids = SelectOptionIds::from(filter.content.clone());
let ids = SelectOptionIds::from_str(&filter.content).unwrap_or_default();
SelectOptionFilterPB {
condition: SelectOptionConditionPB::try_from(filter.condition as u8)
.unwrap_or(SelectOptionConditionPB::OptionIs),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;

use collab_database::fields::Field;
use collab_database::rows::{get_field_type_from_cell, Cell, Cells};
Expand Down Expand Up @@ -245,13 +246,6 @@ pub fn delete_select_option_cell(option_ids: Vec<String>, field: &Field) -> Cell
apply_cell_changeset(BoxAny::new(changeset), None, field, None).unwrap()
}

/// Deserialize the String into cell specific data type.
pub trait FromCellString {
fn from_cell_str(s: &str) -> FlowyResult<Self>
where
Self: Sized;
}

pub struct CellBuilder<'a> {
cells: Cells,
field_maps: HashMap<String, &'a Field>,
Expand Down Expand Up @@ -290,20 +284,20 @@ impl<'a> CellBuilder<'a> {
tracing::warn!("Shouldn't insert cell data to cell whose field type is LastEditedTime or CreatedTime");
},
FieldType::SingleSelect | FieldType::MultiSelect => {
if let Ok(ids) = SelectOptionIds::from_cell_str(&cell_str) {
if let Ok(ids) = SelectOptionIds::from_str(&cell_str) {
cells.insert(field_id, insert_select_option_cell(ids.into_inner(), field));
}
},
FieldType::Checkbox => {
if let Ok(value) = CheckboxCellDataPB::from_cell_str(&cell_str) {
if let Ok(value) = CheckboxCellDataPB::from_str(&cell_str) {
cells.insert(field_id, insert_checkbox_cell(value.is_checked, field));
}
},
FieldType::URL => {
cells.insert(field_id, insert_url_cell(cell_str, field));
},
FieldType::Checklist => {
if let Ok(ids) = SelectOptionIds::from_cell_str(&cell_str) {
if let Ok(ids) = SelectOptionIds::from_str(&cell_str) {
cells.insert(field_id, insert_select_option_cell(ids.into_inner(), field));
}
},
Expand Down
112 changes: 2 additions & 110 deletions frontend/rust-lib/flowy-database2/src/services/cell/type_cell_data.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,6 @@
use bytes::Bytes;
use serde::{Deserialize, Serialize};

use flowy_error::{internal_error, FlowyError, FlowyResult};

use crate::entities::FieldType;

/// TypeCellData is a generic CellData, you can parse the type_cell_data according to the field_type.
/// The `data` is encoded by JSON format. You can use `IntoCellData` to decode the opaque data to
/// concrete cell type.
/// TypeCellData -> IntoCellData<T> -> T
///
/// The `TypeCellData` is the same as the cell data that was saved to disk except it carries the
/// field_type. The field_type indicates the cell data original `FieldType`. The field_type will
/// be changed if the current Field's type switch from one to another.
///
#[derive(Debug, Serialize, Deserialize)]
pub struct TypeCellData {
#[serde(rename = "data")]
pub cell_str: String,
pub field_type: FieldType,
}

impl TypeCellData {
pub fn from_field_type(field_type: &FieldType) -> TypeCellData {
Self {
cell_str: "".to_string(),
field_type: *field_type,
}
}

pub fn from_json_str(s: &str) -> FlowyResult<Self> {
let type_cell_data: TypeCellData = serde_json::from_str(s).map_err(|err| {
let msg = format!("Deserialize {} to type cell data failed.{}", s, err);
FlowyError::internal().with_context(msg)
})?;
Ok(type_cell_data)
}

pub fn into_inner(self) -> String {
self.cell_str
}
}

impl std::convert::TryFrom<String> for TypeCellData {
type Error = FlowyError;

fn try_from(value: String) -> Result<Self, Self::Error> {
TypeCellData::from_json_str(&value)
}
}

impl ToString for TypeCellData {
fn to_string(&self) -> String {
self.cell_str.clone()
}
}

impl TypeCellData {
pub fn new(cell_str: String, field_type: FieldType) -> Self {
TypeCellData {
cell_str,
field_type,
}
}

pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
}

pub fn is_number(&self) -> bool {
self.field_type == FieldType::Number
}

pub fn is_text(&self) -> bool {
self.field_type == FieldType::RichText
}

pub fn is_checkbox(&self) -> bool {
self.field_type == FieldType::Checkbox
}

pub fn is_date(&self) -> bool {
self.field_type == FieldType::DateTime
}

pub fn is_single_select(&self) -> bool {
self.field_type == FieldType::SingleSelect
}

pub fn is_multi_select(&self) -> bool {
self.field_type == FieldType::MultiSelect
}

pub fn is_checklist(&self) -> bool {
self.field_type == FieldType::Checklist
}

pub fn is_url(&self) -> bool {
self.field_type == FieldType::URL
}

pub fn is_select_option(&self) -> bool {
self.field_type == FieldType::MultiSelect || self.field_type == FieldType::SingleSelect
}
}
use flowy_error::{internal_error, FlowyResult};

/// The data is encoded by protobuf or utf8. You should choose the corresponding decode struct to parse it.
///
Expand All @@ -116,13 +13,8 @@ impl TypeCellData {
#[derive(Default, Debug)]
pub struct CellProtobufBlob(pub Bytes);

pub trait DecodedCellData {
type Object;
fn is_empty(&self) -> bool;
}

pub trait CellProtobufBlobParser {
type Object: DecodedCellData;
type Object;
fn parser(bytes: &Bytes) -> FlowyResult<Self::Object>;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#[cfg(test)]
mod tests {
use std::str::FromStr;

use collab_database::fields::Field;

use crate::entities::CheckboxCellDataPB;
use crate::entities::FieldType;
use crate::services::cell::CellDataDecoder;
use crate::services::cell::FromCellString;
use crate::services::field::type_options::checkbox_type_option::*;
use crate::services::field::FieldBuilder;

Expand Down Expand Up @@ -43,7 +44,7 @@ mod tests {
assert_eq!(
type_option
.decode_cell(
&CheckboxCellDataPB::from_cell_str(input_str).unwrap().into(),
&CheckboxCellDataPB::from_str(input_str).unwrap().into(),
field_type,
field
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use collab_database::rows::{new_cell_builder, Cell};
use flowy_error::{FlowyError, FlowyResult};

use crate::entities::{CheckboxCellDataPB, FieldType};
use crate::services::cell::{CellProtobufBlobParser, DecodedCellData, FromCellString};
use crate::services::cell::CellProtobufBlobParser;
use crate::services::field::{TypeOptionCellData, CELL_DATA};

pub const CHECK: &str = "Yes";
Expand All @@ -22,7 +22,7 @@ impl TypeOptionCellData for CheckboxCellDataPB {
impl From<&Cell> for CheckboxCellDataPB {
fn from(cell: &Cell) -> Self {
let value = cell.get_str_value(CELL_DATA).unwrap_or_default();
CheckboxCellDataPB::from_cell_str(&value).unwrap_or_default()
CheckboxCellDataPB::from_str(&value).unwrap_or_default()
}
}

Expand All @@ -49,15 +49,6 @@ impl FromStr for CheckboxCellDataPB {
}
}

impl FromCellString for CheckboxCellDataPB {
fn from_cell_str(s: &str) -> FlowyResult<Self>
where
Self: Sized,
{
Self::from_str(s)
}
}

impl ToString for CheckboxCellDataPB {
fn to_string(&self) -> String {
if self.is_checked {
Expand All @@ -68,14 +59,6 @@ impl ToString for CheckboxCellDataPB {
}
}

impl DecodedCellData for CheckboxCellDataPB {
type Object = CheckboxCellDataPB;

fn is_empty(&self) -> bool {
false
}
}

pub struct CheckboxCellDataParser();
impl CellProtobufBlobParser for CheckboxCellDataParser {
type Object = CheckboxCellDataPB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use strum_macros::EnumIter;
use flowy_error::{internal_error, FlowyResult};

use crate::entities::{DateCellDataPB, FieldType};
use crate::services::cell::{CellProtobufBlobParser, DecodedCellData, FromCellString};
use crate::services::cell::CellProtobufBlobParser;
use crate::services::field::{TypeOptionCellData, CELL_DATA};

#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -196,16 +196,6 @@ impl<'de> serde::Deserialize<'de> for DateCellData {
}
}

impl FromCellString for DateCellData {
fn from_cell_str(s: &str) -> FlowyResult<Self>
where
Self: Sized,
{
let result: DateCellData = serde_json::from_str(s).unwrap();
Ok(result)
}
}

impl ToString for DateCellData {
fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
Expand Down Expand Up @@ -288,14 +278,6 @@ impl TimeFormat {
}
}

impl DecodedCellData for DateCellDataPB {
type Object = DateCellDataPB;

fn is_empty(&self) -> bool {
self.date.is_empty()
}
}

pub struct DateCellDataParser();
impl CellProtobufBlobParser for DateCellDataParser {
type Object = DateCellDataPB;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::services::cell::{CellBytesCustomParser, CellProtobufBlobParser, DecodedCellData};
use crate::services::cell::{CellBytesCustomParser, CellProtobufBlobParser};
use crate::services::field::number_currency::Currency;
use crate::services::field::{NumberFormat, EXTRACT_NUM_REGEX, START_WITH_DOT_NUM_REGEX};
use bytes::Bytes;
Expand Down Expand Up @@ -108,14 +108,6 @@ impl ToString for NumberCellFormat {
}
}

impl DecodedCellData for NumberCellFormat {
type Object = NumberCellFormat;

fn is_empty(&self) -> bool {
self.decimal.is_none()
}
}

pub struct NumberCellDataParser();
impl CellProtobufBlobParser for NumberCellDataParser {
type Object = NumberCellFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ mod tests {
debug_assert_eq!(multi_select.options.len(), 2);
}

// #[test]

#[test]
fn multi_select_insert_multi_option_test() {
let google = SelectOption::new("Google");
Expand Down
Loading

0 comments on commit 682bf19

Please sign in to comment.