chore: fix warnings

This commit is contained in:
appflowy 2022-04-01 22:49:26 +08:00
parent 915d71991a
commit 82eed0506b
14 changed files with 75 additions and 78 deletions

View File

@ -142,7 +142,7 @@ async fn make_field_edit_context(
) -> FlowyResult<EditFieldContext> { ) -> FlowyResult<EditFieldContext> {
let field_meta = field_meta.unwrap_or(get_or_create_field_meta(field_id, &field_type, editor).await?); let field_meta = field_meta.unwrap_or(get_or_create_field_meta(field_id, &field_type, editor).await?);
let s = field_meta let s = field_meta
.get_type_option_str() .get_type_option_str(None)
.unwrap_or_else(|| default_type_option_builder_from_type(&field_type).entry().json_str()); .unwrap_or_else(|| default_type_option_builder_from_type(&field_type).entry().json_str());
let builder = type_option_builder_from_json_str(&s, &field_meta.field_type); let builder = type_option_builder_from_json_str(&s, &field_meta.field_type);

View File

@ -30,10 +30,7 @@ macro_rules! impl_type_option {
($target: ident, $field_type:expr) => { ($target: ident, $field_type:expr) => {
impl std::convert::From<&FieldMeta> for $target { impl std::convert::From<&FieldMeta> for $target {
fn from(field_meta: &FieldMeta) -> $target { fn from(field_meta: &FieldMeta) -> $target {
match field_meta match field_meta.get_type_option_entry::<$target>(Some($field_type)) {
.type_option_by_field_type_id
.get_entry::<$target>(&$field_type)
{
None => $target::default(), None => $target::default(),
Some(target) => target, Some(target) => target,
} }
@ -66,7 +63,7 @@ macro_rules! impl_type_option {
} }
} }
impl TypeOptionDataFrom for $target { impl TypeOptionDataEntity for $target {
fn from_json_str(s: &str) -> $target { fn from_json_str(s: &str) -> $target {
match serde_json::from_str(s) { match serde_json::from_str(s) {
Ok(obj) => obj, Ok(obj) => obj,

View File

@ -1,8 +1,6 @@
use crate::services::field::type_options::*; use crate::services::field::type_options::*;
use bytes::Bytes; use bytes::Bytes;
use flowy_grid_data_model::entities::{ use flowy_grid_data_model::entities::{Field, FieldMeta, FieldType, TypeOptionDataByFieldTypeId, TypeOptionDataEntry};
Field, FieldMeta, FieldType, TypeOptionDataByFieldTypeId, TypeOptionDataEntry, TypeOptionDataFrom,
};
pub struct FieldBuilder { pub struct FieldBuilder {
field_meta: FieldMeta, field_meta: FieldMeta,
@ -71,9 +69,7 @@ impl FieldBuilder {
pub fn build(self) -> FieldMeta { pub fn build(self) -> FieldMeta {
debug_assert_eq!(self.field_meta.field_type, self.type_option_builder.field_type()); debug_assert_eq!(self.field_meta.field_type, self.type_option_builder.field_type());
let mut field_meta = self.field_meta; let mut field_meta = self.field_meta;
field_meta field_meta.insert_type_option_entry(self.type_option_builder.entry());
.type_option_by_field_type_id
.insert_entry(self.type_option_builder.entry());
field_meta field_meta
} }
} }

View File

@ -1,13 +1,10 @@
use crate::impl_type_option; use crate::impl_type_option;
use crate::services::field::{ use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
BoxTypeOptionBuilder, DateTypeOption, MultiSelectTypeOption, NumberTypeOption, RichTextTypeOption,
SingleSelectTypeOption, TypeOptionBuilder,
};
use crate::services::row::CellDataSerde; use crate::services::row::CellDataSerde;
use bytes::Bytes; use bytes::Bytes;
use flowy_derive::ProtoBuf; use flowy_derive::ProtoBuf;
use flowy_error::FlowyError; use flowy_error::FlowyError;
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry, TypeOptionDataFrom}; use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Default)] #[derive(Default)]

View File

@ -5,10 +5,10 @@ use chrono::format::strftime::StrftimeItems;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::FlowyError; use flowy_error::FlowyError;
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry, TypeOptionDataFrom}; use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::services::field::{BoxTypeOptionBuilder, RichTextTypeOption, TypeOptionBuilder}; use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
use strum_macros::EnumIter; use strum_macros::EnumIter;
// Date // Date

View File

@ -2,7 +2,7 @@ use crate::impl_type_option;
use crate::services::row::CellDataSerde; use crate::services::row::CellDataSerde;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::FlowyError; use flowy_error::FlowyError;
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry, TypeOptionDataFrom}; use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rust_decimal::prelude::Zero; use rust_decimal::prelude::Zero;
use rust_decimal::Decimal; use rust_decimal::Decimal;

View File

@ -5,7 +5,7 @@ use crate::services::util::*;
use bytes::Bytes; use bytes::Bytes;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::{FlowyError, FlowyResult}; use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry, TypeOptionDataFrom}; use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;

View File

@ -4,7 +4,7 @@ use crate::services::row::CellDataSerde;
use bytes::Bytes; use bytes::Bytes;
use flowy_derive::ProtoBuf; use flowy_derive::ProtoBuf;
use flowy_error::FlowyError; use flowy_error::FlowyError;
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry, TypeOptionDataFrom}; use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Default)] #[derive(Default)]

View File

@ -1,7 +1,3 @@
use flowy_grid_data_model::entities::FieldType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)] // #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
// pub struct TypeOptionData { // pub struct TypeOptionData {
// #[pb(index = 1)] // #[pb(index = 1)]

View File

@ -1,10 +1,7 @@
use crate::dart_notification::{send_dart_notification, GridNotification}; use crate::dart_notification::{send_dart_notification, GridNotification};
use crate::manager::GridUser; use crate::manager::GridUser;
use crate::services::block_meta_editor::GridBlockMetaEditorManager; use crate::services::block_meta_editor::GridBlockMetaEditorManager;
use crate::services::field::{ use crate::services::field::{default_type_option_builder_from_type, type_option_builder_from_bytes, FieldBuilder};
default_type_option_builder_from_type, type_option_builder_from_bytes, type_option_builder_from_json_str,
FieldBuilder,
};
use crate::services::row::*; use crate::services::row::*;
use bytes::Bytes; use bytes::Bytes;
use flowy_error::{ErrorCode, FlowyError, FlowyResult}; use flowy_error::{ErrorCode, FlowyError, FlowyResult};

View File

@ -6,7 +6,8 @@ use flowy_grid::services::field::{
}; };
use flowy_grid::services::row::{deserialize_cell_data, serialize_cell_data, CellDataSerde, CreateRowMetaBuilder}; use flowy_grid::services::row::{deserialize_cell_data, serialize_cell_data, CellDataSerde, CreateRowMetaBuilder};
use flowy_grid_data_model::entities::{ use flowy_grid_data_model::entities::{
CellMetaChangeset, FieldChangeset, FieldType, GridBlockMeta, GridBlockMetaChangeset, RowMetaChangeset, CellMetaChangeset, FieldChangesetParams, FieldType, GridBlockMeta, GridBlockMetaChangeset, RowMetaChangeset,
TypeOptionDataEntry,
}; };
#[tokio::test] #[tokio::test]
@ -55,7 +56,7 @@ async fn grid_create_duplicate_field() {
async fn grid_update_field_with_empty_change() { async fn grid_update_field_with_empty_change() {
let mut test = GridEditorTest::new().await; let mut test = GridEditorTest::new().await;
let (params, field_meta) = create_single_select_field(&test.grid_id); let (params, field_meta) = create_single_select_field(&test.grid_id);
let changeset = FieldChangeset { let changeset = FieldChangesetParams {
field_id: field_meta.id.clone(), field_id: field_meta.id.clone(),
grid_id: test.grid_id.clone(), grid_id: test.grid_id.clone(),
name: None, name: None,
@ -84,9 +85,9 @@ async fn grid_update_field() {
let (single_select_params, single_select_field) = create_single_select_field(&test.grid_id); let (single_select_params, single_select_field) = create_single_select_field(&test.grid_id);
let mut cloned_field = single_select_field.clone(); let mut cloned_field = single_select_field.clone();
let mut single_select_type_options = SingleSelectTypeOption::from(&single_select_field); let mut single_select_type_option = SingleSelectTypeOption::from(&single_select_field);
single_select_type_options.options.push(SelectOption::new("Unknown")); single_select_type_option.options.push(SelectOption::new("Unknown"));
let changeset = FieldChangeset { let changeset = FieldChangesetParams {
field_id: single_select_field.id.clone(), field_id: single_select_field.id.clone(),
grid_id: test.grid_id.clone(), grid_id: test.grid_id.clone(),
name: None, name: None,
@ -95,12 +96,12 @@ async fn grid_update_field() {
frozen: Some(true), frozen: Some(true),
visibility: None, visibility: None,
width: Some(1000), width: Some(1000),
type_option_data: Some(single_select_type_options.clone().into()), type_option_data: Some(single_select_type_option.protobuf_bytes().to_vec()),
}; };
cloned_field.frozen = true; cloned_field.frozen = true;
cloned_field.width = 1000; cloned_field.width = 1000;
cloned_field.type_option_json = single_select_type_options.into(); cloned_field.insert_type_option_entry(&single_select_type_option);
let scripts = vec![ let scripts = vec![
CreateField { CreateField {

View File

@ -3,10 +3,9 @@ use flowy_grid::services::field::*;
use flowy_grid::services::grid_editor::{ClientGridEditor, GridPadBuilder}; use flowy_grid::services::grid_editor::{ClientGridEditor, GridPadBuilder};
use flowy_grid::services::row::CreateRowMetaPayload; use flowy_grid::services::row::CreateRowMetaPayload;
use flowy_grid_data_model::entities::{ use flowy_grid_data_model::entities::{
BuildGridContext, CellMetaChangeset, CreateFieldParams, Field, FieldChangeset, FieldMeta, FieldType, GridBlockMeta, BuildGridContext, CellMetaChangeset, CreateFieldParams, Field, FieldChangesetParams, FieldMeta, FieldType,
GridBlockMetaChangeset, RowMeta, RowMetaChangeset, RowOrder, GridBlockMeta, GridBlockMetaChangeset, RowMeta, RowMetaChangeset, RowOrder, TypeOptionDataEntry,
}; };
use flowy_grid_data_model::parser::CreateFieldParams;
use flowy_revision::REVISION_WRITE_INTERVAL_IN_MILLIS; use flowy_revision::REVISION_WRITE_INTERVAL_IN_MILLIS;
use flowy_sync::client_grid::GridBuilder; use flowy_sync::client_grid::GridBuilder;
use flowy_test::helper::ViewTest; use flowy_test::helper::ViewTest;
@ -22,7 +21,7 @@ pub enum EditorScript {
params: CreateFieldParams, params: CreateFieldParams,
}, },
UpdateField { UpdateField {
changeset: FieldChangeset, changeset: FieldChangesetParams,
}, },
DeleteField { DeleteField {
field_meta: FieldMeta, field_meta: FieldMeta,
@ -255,6 +254,12 @@ pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, FieldMeta) {
let cloned_field_meta = field_meta.clone(); let cloned_field_meta = field_meta.clone();
let type_option_data = field_meta
.get_type_option_entry::<RichTextTypeOption>(None)
.unwrap()
.protobuf_bytes()
.to_vec();
let field = Field { let field = Field {
id: field_meta.id, id: field_meta.id,
name: field_meta.name, name: field_meta.name,
@ -268,7 +273,7 @@ pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, FieldMeta) {
let params = CreateFieldParams { let params = CreateFieldParams {
grid_id: grid_id.to_owned(), grid_id: grid_id.to_owned(),
field, field,
type_option_data: field_meta.type_option_json.as_bytes().to_vec(), type_option_data,
start_field_id: None, start_field_id: None,
}; };
(params, cloned_field_meta) (params, cloned_field_meta)
@ -281,6 +286,11 @@ pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, FieldMet
let field_meta = FieldBuilder::new(single_select).name("Name").visibility(true).build(); let field_meta = FieldBuilder::new(single_select).name("Name").visibility(true).build();
let cloned_field_meta = field_meta.clone(); let cloned_field_meta = field_meta.clone();
let type_option_data = field_meta
.get_type_option_entry::<SingleSelectTypeOption>(None)
.unwrap()
.protobuf_bytes()
.to_vec();
let field = Field { let field = Field {
id: field_meta.id, id: field_meta.id,
@ -295,7 +305,7 @@ pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, FieldMet
let params = CreateFieldParams { let params = CreateFieldParams {
grid_id: grid_id.to_owned(), grid_id: grid_id.to_owned(),
field, field,
type_option_data: field_meta.type_option_json.as_bytes().to_vec(), type_option_data,
start_field_id: None, start_field_id: None,
}; };
(params, cloned_field_meta) (params, cloned_field_meta)

View File

@ -3,9 +3,7 @@ use bytes::Bytes;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error_code::ErrorCode; use flowy_error_code::ErrorCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::any::Any;
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::Deref;
use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString}; use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
pub const DEFAULT_ROW_HEIGHT: i32 = 36; pub const DEFAULT_ROW_HEIGHT: i32 = 36;
@ -79,7 +77,7 @@ pub struct GridBlockMetaSerde {
pub row_metas: Vec<RowMeta>, pub row_metas: Vec<RowMeta>,
} }
#[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)] #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf, Eq, PartialEq)]
pub struct FieldMeta { pub struct FieldMeta {
#[pb(index = 1)] #[pb(index = 1)]
pub id: String, pub id: String,
@ -120,11 +118,27 @@ impl FieldMeta {
} }
} }
pub fn get_type_option_str(&self) -> Option<String> { pub fn insert_type_option_entry<T: TypeOptionDataEntry + ?Sized>(&mut self, entry: &T) {
match self.type_option_by_field_type_id.get(&self.field_type) { self.type_option_by_field_type_id
None => None, .insert(entry.field_type().type_id(), entry.json_str());
Some(s) => Some(s.to_owned()), }
}
pub fn get_type_option_entry<T: TypeOptionDataEntity>(&self, field_type: Option<FieldType>) -> Option<T> {
let field_type = field_type.as_ref().unwrap_or(&self.field_type);
self.type_option_by_field_type_id
.get(&field_type.type_id())
.map(|s| T::from_json_str(s))
}
pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
self.type_option_by_field_type_id.insert(field_type.type_id(), json_str);
}
pub fn get_type_option_str(&self, field_type: Option<FieldType>) -> Option<String> {
let field_type = field_type.as_ref().unwrap_or(&self.field_type);
self.type_option_by_field_type_id
.get(&field_type.type_id())
.map(|s| s.to_owned())
} }
} }
@ -134,38 +148,28 @@ pub trait TypeOptionDataEntry {
fn protobuf_bytes(&self) -> Bytes; fn protobuf_bytes(&self) -> Bytes;
} }
pub trait TypeOptionDataFrom { pub trait TypeOptionDataEntity {
fn from_json_str(s: &str) -> Self; fn from_json_str(s: &str) -> Self;
fn from_protobuf_bytes(bytes: Bytes) -> Self; fn from_protobuf_bytes(bytes: Bytes) -> Self;
} }
#[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)] #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf, PartialEq, Eq)]
pub struct TypeOptionDataByFieldTypeId { pub struct TypeOptionDataByFieldTypeId {
#[pb(index = 1)] #[pb(index = 1)]
pub map: HashMap<String, String>, pub map: HashMap<String, String>,
} }
impl TypeOptionDataByFieldTypeId { impl std::ops::Deref for TypeOptionDataByFieldTypeId {
pub fn insert_entry<T: TypeOptionDataEntry + ?Sized>(&mut self, entry: &T) { type Target = HashMap<String, String>;
self.map.insert(entry.field_type().type_id(), entry.json_str());
}
pub fn insert(&mut self, field_type: &FieldType, json_str: String) { fn deref(&self) -> &Self::Target {
self.map.insert(field_type.type_id(), json_str); &self.map
} }
}
pub fn get_entry<T: TypeOptionDataFrom>(&self, field_type: &FieldType) -> Option<T> { impl std::ops::DerefMut for TypeOptionDataByFieldTypeId {
match self.map.get(&field_type.type_id()) { fn deref_mut(&mut self) -> &mut Self::Target {
None => None, &mut self.map
Some(s) => Some(T::from_json_str(s)),
}
}
pub fn get(&self, field_type: &FieldType) -> Option<String> {
match self.map.get(&field_type.type_id()) {
None => None,
Some(s) => Some(s.to_owned()),
}
} }
} }

View File

@ -4,13 +4,13 @@ use crate::util::{cal_diff, make_delta_from_revisions};
use bytes::Bytes; use bytes::Bytes;
use flowy_grid_data_model::entities::{ use flowy_grid_data_model::entities::{
FieldChangesetParams, FieldMeta, FieldOrder, FieldType, GridBlockMeta, GridBlockMetaChangeset, GridMeta, FieldChangesetParams, FieldMeta, FieldOrder, FieldType, GridBlockMeta, GridBlockMetaChangeset, GridMeta,
RepeatedFieldOrder, TypeOptionDataEntry, RepeatedFieldOrder,
}; };
use lib_infra::uuid; use lib_infra::uuid;
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder}; use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use std::collections::HashMap; use std::collections::HashMap;
use std::string::FromUtf8Error;
use std::sync::Arc; use std::sync::Arc;
pub type GridMetaDelta = PlainTextDelta; pub type GridMetaDelta = PlainTextDelta;
@ -107,11 +107,9 @@ impl GridMetaPad {
Ok(None) Ok(None)
} }
Some(field_meta) => { Some(field_meta) => {
if field_meta.get_type_option_str().is_none() { if field_meta.get_type_option_str(Some(field_type.clone())).is_none() {
let type_option_json = type_option_json_builder(&field_type); let type_option_json = type_option_json_builder(&field_type);
field_meta field_meta.insert_type_option_str(&field_type, type_option_json);
.type_option_by_field_type_id
.insert(&field_type, type_option_json);
} }
field_meta.field_type = field_type; field_meta.field_type = field_type;
@ -162,7 +160,8 @@ impl GridMetaPad {
if let Some(type_option_data) = changeset.type_option_data { if let Some(type_option_data) = changeset.type_option_data {
match deserializer.deserialize(type_option_data) { match deserializer.deserialize(type_option_data) {
Ok(json_str) => { Ok(json_str) => {
field.type_option_by_field_type_id.insert(&field.field_type, json_str); let field_type = field.field_type.clone();
field.insert_type_option_str(&field_type, json_str);
is_changed = Some(()) is_changed = Some(())
} }
Err(err) => { Err(err) => {