mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: import cvs from file (#2667)
* refactor: import file * chore: fix tarui build
This commit is contained in:
@ -12,7 +12,6 @@ mod view_entities;
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod share_entities;
|
||||
mod type_option_entities;
|
||||
|
||||
pub use calendar_entities::*;
|
||||
@ -23,7 +22,6 @@ pub use filter_entities::*;
|
||||
pub use group_entities::*;
|
||||
pub use row_entities::*;
|
||||
pub use setting_entities::*;
|
||||
pub use share_entities::*;
|
||||
pub use sort_entities::*;
|
||||
pub use type_option_entities::*;
|
||||
pub use view_entities::*;
|
||||
|
@ -1,24 +0,0 @@
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
|
||||
#[derive(Clone, Debug, ProtoBuf_Enum)]
|
||||
pub enum ImportTypePB {
|
||||
CSV = 0,
|
||||
}
|
||||
|
||||
impl Default for ImportTypePB {
|
||||
fn default() -> Self {
|
||||
Self::CSV
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, ProtoBuf, Default)]
|
||||
pub struct DatabaseImportPB {
|
||||
#[pb(index = 1, one_of)]
|
||||
pub data: Option<String>,
|
||||
|
||||
#[pb(index = 2, one_of)]
|
||||
pub uri: Option<String>,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub import_type: ImportTypePB,
|
||||
}
|
@ -5,7 +5,7 @@ use collab_database::rows::RowId;
|
||||
use collab_database::views::DatabaseLayout;
|
||||
use lib_infra::util::timestamp;
|
||||
|
||||
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
|
||||
use flowy_error::{FlowyError, FlowyResult};
|
||||
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
||||
|
||||
use crate::entities::*;
|
||||
@ -17,7 +17,6 @@ use crate::services::field::{
|
||||
type_option_data_from_pb_or_default, DateCellChangeset, SelectOptionCellChangeset,
|
||||
};
|
||||
use crate::services::group::{GroupChangeset, GroupSettingChangeset};
|
||||
use crate::services::share::csv::CSVFormat;
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn get_database_data_handler(
|
||||
@ -678,27 +677,3 @@ pub(crate) async fn get_calendar_event_handler(
|
||||
Some(event) => data_result_ok(event),
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn import_data_handler(
|
||||
data: AFPluginData<DatabaseImportPB>,
|
||||
manager: AFPluginState<Arc<DatabaseManager2>>,
|
||||
) -> FlowyResult<()> {
|
||||
let params = data.into_inner();
|
||||
|
||||
match params.import_type {
|
||||
ImportTypePB::CSV => {
|
||||
if let Some(data) = params.data {
|
||||
manager.import_csv(data, CSVFormat::META).await?;
|
||||
} else if let Some(uri) = params.uri {
|
||||
manager.import_csv_from_uri(uri, CSVFormat::META).await?;
|
||||
} else {
|
||||
return Err(FlowyError::new(
|
||||
ErrorCode::InvalidData,
|
||||
"No data or uri provided",
|
||||
));
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -9,10 +9,10 @@ use crate::event_handler::*;
|
||||
use crate::manager::DatabaseManager2;
|
||||
|
||||
pub fn init(database_manager: Arc<DatabaseManager2>) -> AFPlugin {
|
||||
let mut plugin = AFPlugin::new()
|
||||
let plugin = AFPlugin::new()
|
||||
.name(env!("CARGO_PKG_NAME"))
|
||||
.state(database_manager);
|
||||
plugin = plugin
|
||||
plugin
|
||||
.event(DatabaseEvent::GetDatabase, get_database_data_handler)
|
||||
.event(DatabaseEvent::GetDatabaseSetting, get_database_setting_handler)
|
||||
.event(DatabaseEvent::UpdateDatabaseSetting, update_database_setting_handler)
|
||||
@ -64,10 +64,7 @@ pub fn init(database_manager: Arc<DatabaseManager2>) -> AFPlugin {
|
||||
// Layout setting
|
||||
.event(DatabaseEvent::SetLayoutSetting, set_layout_setting_handler)
|
||||
.event(DatabaseEvent::GetLayoutSetting, get_layout_setting_handler)
|
||||
// import
|
||||
.event(DatabaseEvent::ImportCSV, import_data_handler);
|
||||
|
||||
plugin
|
||||
// import
|
||||
}
|
||||
|
||||
/// [DatabaseEvent] defines events that are used to interact with the Grid. You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/protobuf)
|
||||
@ -278,7 +275,4 @@ pub enum DatabaseEvent {
|
||||
|
||||
#[event(input = "MoveCalendarEventPB")]
|
||||
MoveCalendarEvent = 125,
|
||||
|
||||
#[event(input = "DatabaseImportPB")]
|
||||
ImportCSV = 130,
|
||||
}
|
||||
|
@ -195,11 +195,17 @@ impl DatabaseManager2 {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn import_csv(&self, content: String, format: CSVFormat) -> FlowyResult<ImportResult> {
|
||||
let params =
|
||||
tokio::task::spawn_blocking(move || CSVImporter.import_csv_from_string(content, format))
|
||||
.await
|
||||
.map_err(internal_error)??;
|
||||
pub async fn import_csv(
|
||||
&self,
|
||||
view_id: String,
|
||||
content: String,
|
||||
format: CSVFormat,
|
||||
) -> FlowyResult<ImportResult> {
|
||||
let params = tokio::task::spawn_blocking(move || {
|
||||
CSVImporter.import_csv_from_string(view_id, content, format)
|
||||
})
|
||||
.await
|
||||
.map_err(internal_error)??;
|
||||
let result = ImportResult {
|
||||
database_id: params.database_id.clone(),
|
||||
view_id: params.view_id.clone(),
|
||||
@ -208,7 +214,12 @@ impl DatabaseManager2 {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn import_csv_from_uri(&self, _uri: String, _format: CSVFormat) -> FlowyResult<()> {
|
||||
// will implement soon
|
||||
pub async fn import_csv_from_file(
|
||||
&self,
|
||||
_file_path: String,
|
||||
_format: CSVFormat,
|
||||
) -> FlowyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ use crate::entities::FieldType;
|
||||
|
||||
use crate::services::field::{default_type_option_data_from_type, CELL_DATA};
|
||||
use crate::services::share::csv::CSVFormat;
|
||||
use collab_database::database::{gen_database_id, gen_database_view_id, gen_field_id, gen_row_id};
|
||||
use collab_database::database::{gen_database_id, gen_field_id, gen_row_id};
|
||||
use collab_database::fields::Field;
|
||||
use collab_database::rows::{new_cell_builder, Cell, CreateRowParams};
|
||||
use collab_database::views::{CreateDatabaseParams, DatabaseLayout};
|
||||
@ -15,6 +15,7 @@ pub struct CSVImporter;
|
||||
impl CSVImporter {
|
||||
pub fn import_csv_from_file(
|
||||
&self,
|
||||
view_id: &str,
|
||||
path: &str,
|
||||
style: CSVFormat,
|
||||
) -> FlowyResult<CreateDatabaseParams> {
|
||||
@ -22,17 +23,18 @@ impl CSVImporter {
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)?;
|
||||
let fields_with_rows = self.get_fields_and_rows(content)?;
|
||||
let database_data = database_from_fields_and_rows(fields_with_rows, &style);
|
||||
let database_data = database_from_fields_and_rows(view_id, fields_with_rows, &style);
|
||||
Ok(database_data)
|
||||
}
|
||||
|
||||
pub fn import_csv_from_string(
|
||||
&self,
|
||||
view_id: String,
|
||||
content: String,
|
||||
format: CSVFormat,
|
||||
) -> FlowyResult<CreateDatabaseParams> {
|
||||
let fields_with_rows = self.get_fields_and_rows(content)?;
|
||||
let database_data = database_from_fields_and_rows(fields_with_rows, &format);
|
||||
let database_data = database_from_fields_and_rows(&view_id, fields_with_rows, &format);
|
||||
Ok(database_data)
|
||||
}
|
||||
|
||||
@ -68,11 +70,11 @@ impl CSVImporter {
|
||||
}
|
||||
|
||||
fn database_from_fields_and_rows(
|
||||
view_id: &str,
|
||||
fields_and_rows: FieldsRows,
|
||||
format: &CSVFormat,
|
||||
) -> CreateDatabaseParams {
|
||||
let (fields, rows) = fields_and_rows.split();
|
||||
let view_id = gen_database_view_id();
|
||||
let database_id = gen_database_id();
|
||||
|
||||
let fields = fields
|
||||
@ -125,7 +127,7 @@ fn database_from_fields_and_rows(
|
||||
|
||||
CreateDatabaseParams {
|
||||
database_id,
|
||||
view_id,
|
||||
view_id: view_id.to_string(),
|
||||
name: "".to_string(),
|
||||
layout: DatabaseLayout::Grid,
|
||||
layout_settings: Default::default(),
|
||||
@ -167,6 +169,7 @@ pub struct ImportResult {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::services::share::csv::{CSVFormat, CSVImporter};
|
||||
use collab_database::database::gen_database_view_id;
|
||||
|
||||
#[test]
|
||||
fn test_import_csv_from_str() {
|
||||
@ -176,7 +179,7 @@ mod tests {
|
||||
,,,,Yes,"#;
|
||||
let importer = CSVImporter;
|
||||
let result = importer
|
||||
.import_csv_from_string(s.to_string(), CSVFormat::Original)
|
||||
.import_csv_from_string(gen_database_view_id(), s.to_string(), CSVFormat::Original)
|
||||
.unwrap();
|
||||
assert_eq!(result.created_rows.len(), 3);
|
||||
assert_eq!(result.fields.len(), 6);
|
||||
|
@ -1,3 +1,4 @@
|
||||
use collab_database::database::gen_database_view_id;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -262,7 +263,7 @@ impl DatabaseEditorTest {
|
||||
self
|
||||
.sdk
|
||||
.database_manager
|
||||
.import_csv(s, format)
|
||||
.import_csv(gen_database_view_id(), s, format)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
Reference in New Issue
Block a user