mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: fix transform field type error
This commit is contained in:
parent
43095cb0dd
commit
9d629412c8
@ -30,7 +30,7 @@ macro_rules! impl_type_option {
|
||||
($target: ident, $field_type:expr) => {
|
||||
impl std::convert::From<&FieldMeta> for $target {
|
||||
fn from(field_meta: &FieldMeta) -> $target {
|
||||
match field_meta.get_type_option_entry::<$target>(Some($field_type)) {
|
||||
match field_meta.get_type_option_entry::<$target>(&$field_type) {
|
||||
None => $target::default(),
|
||||
Some(target) => target,
|
||||
}
|
||||
@ -63,7 +63,7 @@ macro_rules! impl_type_option {
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeOptionDataEntity for $target {
|
||||
impl TypeOptionDataDeserializer for $target {
|
||||
fn from_json_str(s: &str) -> $target {
|
||||
match serde_json::from_str(s) {
|
||||
Ok(obj) => obj,
|
||||
|
@ -4,7 +4,10 @@ use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellD
|
||||
use bytes::Bytes;
|
||||
use flowy_derive::ProtoBuf;
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
|
||||
use flowy_grid_data_model::entities::{
|
||||
CellMeta, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
|
@ -5,7 +5,10 @@ use chrono::format::strftime::StrftimeItems;
|
||||
use chrono::NaiveDateTime;
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
|
||||
use flowy_grid_data_model::entities::{
|
||||
CellMeta, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
|
@ -2,7 +2,10 @@ use crate::impl_type_option;
|
||||
use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellData};
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
|
||||
use flowy_grid_data_model::entities::{
|
||||
CellMeta, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
|
||||
};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use rust_decimal::Decimal;
|
||||
|
@ -7,7 +7,7 @@ use bytes::Bytes;
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::{ErrorCode, FlowyError};
|
||||
use flowy_grid_data_model::entities::{
|
||||
CellMeta, CellMetaChangeset, FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry,
|
||||
CellMeta, CellMetaChangeset, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
|
||||
};
|
||||
use flowy_grid_data_model::parser::NotEmptyUuid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -4,7 +4,9 @@ use crate::services::row::{decode_cell_data, CellDataChangeset, CellDataOperatio
|
||||
use bytes::Bytes;
|
||||
use flowy_derive::ProtoBuf;
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType, TypeOptionDataEntity, TypeOptionDataEntry};
|
||||
use flowy_grid_data_model::entities::{
|
||||
CellMeta, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
@ -38,7 +40,7 @@ impl CellDataOperation for RichTextTypeOption {
|
||||
|| type_option_cell_data.is_multi_select()
|
||||
|| type_option_cell_data.is_number()
|
||||
{
|
||||
decode_cell_data(data, field_meta).unwrap_or_else(|_| "".to_owned())
|
||||
decode_cell_data(data, &field_meta, &type_option_cell_data.field_type).unwrap_or_else(|| "".to_owned())
|
||||
} else {
|
||||
type_option_cell_data.data
|
||||
}
|
||||
|
@ -106,17 +106,43 @@ pub fn apply_cell_data_changeset<T: Into<CellDataChangeset>>(
|
||||
FieldType::Checkbox => CheckboxTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
|
||||
}
|
||||
}
|
||||
//
|
||||
// #[tracing::instrument(level = "trace", skip(field_meta, data), fields(content), err)]
|
||||
// pub fn decode_cell_data(data: String, field_meta: &FieldMeta, field_type: &FieldType) -> Result<String, FlowyError> {
|
||||
// let s = match field_meta.field_type {
|
||||
// FieldType::RichText => RichTextTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// FieldType::Number => NumberTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// FieldType::DateTime => DateTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// FieldType::Checkbox => CheckboxTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
// };
|
||||
// tracing::Span::current().record("content", &format!("{:?}: {}", field_meta.field_type, s).as_str());
|
||||
// Ok(s)
|
||||
// }
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(field_meta, data), fields(content), err)]
|
||||
pub fn decode_cell_data(data: String, field_meta: &FieldMeta) -> Result<String, FlowyError> {
|
||||
let s = match field_meta.field_type {
|
||||
FieldType::RichText => RichTextTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
FieldType::Number => NumberTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
FieldType::DateTime => DateTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
FieldType::Checkbox => CheckboxTypeOption::from(field_meta).decode_cell_data(data, field_meta),
|
||||
#[tracing::instrument(level = "trace", skip(field_meta, data), fields(content))]
|
||||
pub fn decode_cell_data(data: String, field_meta: &FieldMeta, field_type: &FieldType) -> Option<String> {
|
||||
let s = match field_type {
|
||||
FieldType::RichText => field_meta
|
||||
.get_type_option_entry::<RichTextTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
FieldType::Number => field_meta
|
||||
.get_type_option_entry::<NumberTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
FieldType::DateTime => field_meta
|
||||
.get_type_option_entry::<DateTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
FieldType::SingleSelect => field_meta
|
||||
.get_type_option_entry::<SingleSelectTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
FieldType::MultiSelect => field_meta
|
||||
.get_type_option_entry::<MultiSelectTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
FieldType::Checkbox => field_meta
|
||||
.get_type_option_entry::<CheckboxTypeOption>(field_type)?
|
||||
.decode_cell_data(data, field_meta),
|
||||
};
|
||||
tracing::Span::current().record("content", &format!("{:?}: {}", field_meta.field_type, s).as_str());
|
||||
Ok(s)
|
||||
Some(s)
|
||||
}
|
||||
|
@ -34,28 +34,16 @@ pub fn make_cell_by_field_id(
|
||||
cell_meta: CellMeta,
|
||||
) -> Option<(String, Cell)> {
|
||||
let field_meta = field_map.get(&field_id)?;
|
||||
match decode_cell_data(cell_meta.data, field_meta) {
|
||||
Ok(content) => {
|
||||
let cell = Cell::new(&field_id, content);
|
||||
Some((field_id, cell))
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("{}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
let content = decode_cell_data(cell_meta.data, field_meta, &field_meta.field_type)?;
|
||||
let cell = Cell::new(&field_id, content);
|
||||
Some((field_id, cell))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn make_cell(field_id: &str, field_meta: &FieldMeta, row_meta: &RowMeta) -> Option<Cell> {
|
||||
let cell_meta = row_meta.cells.get(field_id)?.clone();
|
||||
match decode_cell_data(cell_meta.data, field_meta) {
|
||||
Ok(content) => Some(Cell::new(field_id, content)),
|
||||
Err(e) => {
|
||||
tracing::error!("{}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
let content = decode_cell_data(cell_meta.data, field_meta, &field_meta.field_type)?;
|
||||
Some(Cell::new(field_id, content))
|
||||
}
|
||||
|
||||
pub(crate) fn make_row_orders_from_row_metas(row_metas: &[Arc<RowMeta>]) -> Vec<RowOrder> {
|
||||
|
@ -287,7 +287,7 @@ async fn grid_row_add_date_cell_test() {
|
||||
let date_field = date_field.unwrap();
|
||||
let cell_data = context.cell_by_field_id.get(&date_field.id).unwrap().clone();
|
||||
assert_eq!(
|
||||
decode_cell_data(cell_data.data.clone(), &date_field).unwrap(),
|
||||
decode_cell_data(cell_data.data.clone(), &date_field, &date_field.field_type).unwrap(),
|
||||
"2022/03/16 08:31",
|
||||
);
|
||||
let scripts = vec![CreateRow { context }];
|
||||
|
@ -258,7 +258,7 @@ pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, FieldMeta) {
|
||||
let cloned_field_meta = field_meta.clone();
|
||||
|
||||
let type_option_data = field_meta
|
||||
.get_type_option_entry::<RichTextTypeOption>(None)
|
||||
.get_type_option_entry::<RichTextTypeOption>(&field_meta.field_type)
|
||||
.unwrap()
|
||||
.protobuf_bytes()
|
||||
.to_vec();
|
||||
@ -290,7 +290,7 @@ pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, FieldMet
|
||||
let field_meta = FieldBuilder::new(single_select).name("Name").visibility(true).build();
|
||||
let cloned_field_meta = field_meta.clone();
|
||||
let type_option_data = field_meta
|
||||
.get_type_option_entry::<SingleSelectTypeOption>(None)
|
||||
.get_type_option_entry::<SingleSelectTypeOption>(&field_meta.field_type)
|
||||
.unwrap()
|
||||
.protobuf_bytes()
|
||||
.to_vec();
|
||||
|
@ -122,12 +122,14 @@ impl FieldMeta {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_type_option_entry<T: TypeOptionDataEntry + ?Sized>(&mut self, entry: &T) {
|
||||
pub fn insert_type_option_entry<T>(&mut self, entry: &T)
|
||||
where
|
||||
T: TypeOptionDataEntry + ?Sized,
|
||||
{
|
||||
self.type_options.insert(entry.field_type().type_id(), entry.json_str());
|
||||
}
|
||||
|
||||
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);
|
||||
pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type: &FieldType) -> Option<T> {
|
||||
self.type_options
|
||||
.get(&field_type.type_id())
|
||||
.map(|s| T::from_json_str(s))
|
||||
@ -149,7 +151,7 @@ pub trait TypeOptionDataEntry {
|
||||
fn protobuf_bytes(&self) -> Bytes;
|
||||
}
|
||||
|
||||
pub trait TypeOptionDataEntity {
|
||||
pub trait TypeOptionDataDeserializer {
|
||||
fn from_json_str(s: &str) -> Self;
|
||||
fn from_protobuf_bytes(bytes: Bytes) -> Self;
|
||||
}
|
||||
|
@ -165,8 +165,8 @@ impl GridBlockMetaPad {
|
||||
Some(delta) => {
|
||||
tracing::debug!("[GridBlockMeta] Composing delta {}", delta.to_delta_str());
|
||||
tracing::debug!(
|
||||
"[GridBlockMeta] Composing str {}",
|
||||
delta.to_str().unwrap_or("".to_string())
|
||||
"[GridBlockMeta] current delta: {}",
|
||||
self.delta.to_str().unwrap_or("".to_string())
|
||||
);
|
||||
self.delta = self.delta.compose(&delta)?;
|
||||
Ok(Some(GridBlockMetaChange { delta, md5: self.md5() }))
|
||||
|
Loading…
Reference in New Issue
Block a user