|
@@ -1,5 +1,5 @@
|
|
|
use crate::revision::filter_rev::GridFilterRevision;
|
|
|
-use crate::revision::group_rev::GridGroupRevision;
|
|
|
+use crate::revision::grid_group::GridGroupRevision;
|
|
|
use crate::revision::{FieldRevision, FieldTypeRevision};
|
|
|
use indexmap::IndexMap;
|
|
|
use nanoid::nanoid;
|
|
@@ -21,28 +21,28 @@ pub fn gen_grid_sort_id() -> String {
|
|
|
nanoid!(6)
|
|
|
}
|
|
|
|
|
|
+pub type GridFilters = SettingContainer<GridFilterRevision>;
|
|
|
+pub type GridFilterRevisionMap = GridObjectRevisionMap<GridFilterRevision>;
|
|
|
+pub type FiltersByFieldId = HashMap<String, Vec<Arc<GridFilterRevision>>>;
|
|
|
+//
|
|
|
+pub type GridGroups = SettingContainer<GridGroupRevision>;
|
|
|
+pub type GridGroupRevisionMap = GridObjectRevisionMap<GridGroupRevision>;
|
|
|
+pub type GroupsByFieldId = HashMap<String, Vec<Arc<GridGroupRevision>>>;
|
|
|
+//
|
|
|
+pub type GridSorts = SettingContainer<GridSortRevision>;
|
|
|
+pub type GridSortRevisionMap = GridObjectRevisionMap<GridSortRevision>;
|
|
|
+pub type SortsByFieldId = HashMap<String, Vec<Arc<GridSortRevision>>>;
|
|
|
+
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
|
|
pub struct GridSettingRevision {
|
|
|
pub layout: GridLayoutRevision,
|
|
|
|
|
|
- /// Each layout contains multiple key/value.
|
|
|
- /// Key: field_id
|
|
|
- /// Value: this value contains key/value.
|
|
|
- /// Key: FieldType,
|
|
|
- /// Value: the corresponding filters.
|
|
|
- #[serde(with = "indexmap::serde_seq")]
|
|
|
- filters: IndexMap<GridLayoutRevision, IndexMap<String, GridFilterRevisionMap>>,
|
|
|
+ pub filters: GridFilters,
|
|
|
|
|
|
- /// Each layout contains multiple key/value.
|
|
|
- /// Key: field_id
|
|
|
- /// Value: this value contains key/value.
|
|
|
- /// Key: FieldType,
|
|
|
- /// Value: the corresponding groups.
|
|
|
- #[serde(skip, with = "indexmap::serde_seq")]
|
|
|
- pub groups: IndexMap<GridLayoutRevision, IndexMap<String, GridGroupRevisionMap>>,
|
|
|
+ pub groups: GridGroups,
|
|
|
|
|
|
- #[serde(skip, with = "indexmap::serde_seq")]
|
|
|
- pub sorts: IndexMap<GridLayoutRevision, Vec<GridSortRevision>>,
|
|
|
+ #[serde(skip)]
|
|
|
+ pub sorts: GridSorts,
|
|
|
}
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
|
|
@@ -65,12 +65,18 @@ impl std::default::Default for GridLayoutRevision {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub type FiltersByFieldId = HashMap<String, Vec<Arc<GridFilterRevision>>>;
|
|
|
-pub type GroupsByFieldId = HashMap<String, Vec<Arc<GridGroupRevision>>>;
|
|
|
-pub type SortsByFieldId = HashMap<String, Vec<Arc<GridSortRevision>>>;
|
|
|
impl GridSettingRevision {
|
|
|
- pub fn get_all_group(&self) -> Option<GroupsByFieldId> {
|
|
|
- None
|
|
|
+ pub fn get_all_groups(&self, field_revs: &[Arc<FieldRevision>]) -> Option<GroupsByFieldId> {
|
|
|
+ self.groups.get_all_objects(&self.layout, field_revs)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn get_groups(
|
|
|
+ &self,
|
|
|
+ layout: &GridLayoutRevision,
|
|
|
+ field_id: &str,
|
|
|
+ field_type_rev: &FieldTypeRevision,
|
|
|
+ ) -> Option<Vec<Arc<GridGroupRevision>>> {
|
|
|
+ self.groups.get_objects(layout, field_id, field_type_rev)
|
|
|
}
|
|
|
|
|
|
pub fn get_mut_groups(
|
|
@@ -79,10 +85,7 @@ impl GridSettingRevision {
|
|
|
field_id: &str,
|
|
|
field_type: &FieldTypeRevision,
|
|
|
) -> Option<&mut Vec<Arc<GridGroupRevision>>> {
|
|
|
- self.groups
|
|
|
- .get_mut(layout)
|
|
|
- .and_then(|group_rev_map_by_field_id| group_rev_map_by_field_id.get_mut(field_id))
|
|
|
- .and_then(|group_rev_map| group_rev_map.get_mut(field_type))
|
|
|
+ self.groups.get_mut_objects(layout, field_id, field_type)
|
|
|
}
|
|
|
|
|
|
pub fn insert_group(
|
|
@@ -90,102 +93,143 @@ impl GridSettingRevision {
|
|
|
layout: &GridLayoutRevision,
|
|
|
field_id: &str,
|
|
|
field_type: &FieldTypeRevision,
|
|
|
- filter_rev: GridGroupRevision,
|
|
|
+ group_rev: GridGroupRevision,
|
|
|
) {
|
|
|
- let filter_rev_map_by_field_id = self.groups.entry(layout.clone()).or_insert_with(IndexMap::new);
|
|
|
- let filter_rev_map = filter_rev_map_by_field_id
|
|
|
- .entry(field_id.to_string())
|
|
|
- .or_insert_with(GridGroupRevisionMap::new);
|
|
|
+ self.groups.insert_object(layout, field_id, field_type, group_rev);
|
|
|
+ }
|
|
|
|
|
|
- filter_rev_map
|
|
|
- .entry(field_type.to_owned())
|
|
|
- .or_insert_with(Vec::new)
|
|
|
- .push(Arc::new(filter_rev))
|
|
|
+ pub fn get_all_filters(&self, field_revs: &[Arc<FieldRevision>]) -> Option<FiltersByFieldId> {
|
|
|
+ self.filters.get_all_objects(&self.layout, field_revs)
|
|
|
}
|
|
|
|
|
|
- pub fn get_all_sort(&self) -> Option<SortsByFieldId> {
|
|
|
- None
|
|
|
+ pub fn get_filters(
|
|
|
+ &self,
|
|
|
+ layout: &GridLayoutRevision,
|
|
|
+ field_id: &str,
|
|
|
+ field_type_rev: &FieldTypeRevision,
|
|
|
+ ) -> Option<Vec<Arc<GridFilterRevision>>> {
|
|
|
+ self.filters.get_objects(layout, field_id, field_type_rev)
|
|
|
}
|
|
|
|
|
|
- /// Return the Filters of the current layout
|
|
|
- pub fn get_all_filter(&self, field_revs: &[Arc<FieldRevision>]) -> Option<FiltersByFieldId> {
|
|
|
- let layout = &self.layout;
|
|
|
- // Acquire the read lock of the filters.
|
|
|
- let filter_rev_map_by_field_id = self.filters.get(layout)?;
|
|
|
- // Get the filters according to the FieldType, so we need iterate the field_revs.
|
|
|
- let filters_by_field_id = field_revs
|
|
|
- .iter()
|
|
|
- .flat_map(|field_rev| {
|
|
|
- let field_type = &field_rev.field_type_rev;
|
|
|
- let field_id = &field_rev.id;
|
|
|
+ pub fn get_mut_filters(
|
|
|
+ &mut self,
|
|
|
+ layout: &GridLayoutRevision,
|
|
|
+ field_id: &str,
|
|
|
+ field_type: &FieldTypeRevision,
|
|
|
+ ) -> Option<&mut Vec<Arc<GridFilterRevision>>> {
|
|
|
+ self.filters.get_mut_objects(layout, field_id, field_type)
|
|
|
+ }
|
|
|
|
|
|
- let filter_rev_map: &GridFilterRevisionMap = filter_rev_map_by_field_id.get(field_id)?;
|
|
|
- let filters: Vec<Arc<GridFilterRevision>> = filter_rev_map.get(field_type)?.clone();
|
|
|
- Some((field_rev.id.clone(), filters))
|
|
|
- })
|
|
|
- .collect::<FiltersByFieldId>();
|
|
|
- Some(filters_by_field_id)
|
|
|
+ pub fn insert_filter(
|
|
|
+ &mut self,
|
|
|
+ layout: &GridLayoutRevision,
|
|
|
+ field_id: &str,
|
|
|
+ field_type: &FieldTypeRevision,
|
|
|
+ filter_rev: GridFilterRevision,
|
|
|
+ ) {
|
|
|
+ self.filters.insert_object(layout, field_id, field_type, filter_rev);
|
|
|
}
|
|
|
|
|
|
- #[allow(dead_code)]
|
|
|
- fn get_filter_rev_map(&self, layout: &GridLayoutRevision, field_id: &str) -> Option<&GridFilterRevisionMap> {
|
|
|
- let filter_rev_map_by_field_id = self.filters.get(layout)?;
|
|
|
- filter_rev_map_by_field_id.get(field_id)
|
|
|
+ pub fn get_all_sort(&self) -> Option<SortsByFieldId> {
|
|
|
+ None
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- pub fn get_mut_filters(
|
|
|
+#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
|
|
+pub struct GridSortRevision {
|
|
|
+ pub id: String,
|
|
|
+ pub field_id: Option<String>,
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
|
|
+#[serde(transparent)]
|
|
|
+pub struct SettingContainer<T>
|
|
|
+where
|
|
|
+ T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
|
|
+{
|
|
|
+ /// Each layout contains multiple key/value.
|
|
|
+ /// Key: field_id
|
|
|
+ /// Value: this value contains key/value.
|
|
|
+ /// Key: FieldType,
|
|
|
+ /// Value: the corresponding objects.
|
|
|
+ #[serde(with = "indexmap::serde_seq")]
|
|
|
+ inner: IndexMap<GridLayoutRevision, IndexMap<String, GridObjectRevisionMap<T>>>,
|
|
|
+}
|
|
|
+
|
|
|
+impl<T> SettingContainer<T>
|
|
|
+where
|
|
|
+ T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
|
|
+{
|
|
|
+ pub fn get_mut_objects(
|
|
|
&mut self,
|
|
|
layout: &GridLayoutRevision,
|
|
|
field_id: &str,
|
|
|
field_type: &FieldTypeRevision,
|
|
|
- ) -> Option<&mut Vec<Arc<GridFilterRevision>>> {
|
|
|
- self.filters
|
|
|
+ ) -> Option<&mut Vec<Arc<T>>> {
|
|
|
+ let value = self
|
|
|
+ .inner
|
|
|
.get_mut(layout)
|
|
|
- .and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get_mut(field_id))
|
|
|
- .and_then(|filter_rev_map| filter_rev_map.get_mut(field_type))
|
|
|
+ .and_then(|object_rev_map_by_field_id| object_rev_map_by_field_id.get_mut(field_id))
|
|
|
+ .and_then(|object_rev_map| object_rev_map.get_mut(field_type));
|
|
|
+ if value.is_none() {
|
|
|
+ tracing::warn!("Can't find the {:?} with", std::any::type_name::<T>());
|
|
|
+ }
|
|
|
+ value
|
|
|
}
|
|
|
-
|
|
|
- pub fn get_filters(
|
|
|
+ pub fn get_objects(
|
|
|
&self,
|
|
|
layout: &GridLayoutRevision,
|
|
|
field_id: &str,
|
|
|
field_type_rev: &FieldTypeRevision,
|
|
|
- ) -> Option<Vec<Arc<GridFilterRevision>>> {
|
|
|
- self.filters
|
|
|
+ ) -> Option<Vec<Arc<T>>> {
|
|
|
+ self.inner
|
|
|
.get(layout)
|
|
|
- .and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get(field_id))
|
|
|
- .and_then(|filter_rev_map| filter_rev_map.get(field_type_rev))
|
|
|
+ .and_then(|object_rev_map_by_field_id| object_rev_map_by_field_id.get(field_id))
|
|
|
+ .and_then(|object_rev_map| object_rev_map.get(field_type_rev))
|
|
|
.cloned()
|
|
|
}
|
|
|
|
|
|
- pub fn insert_filter(
|
|
|
+ pub fn get_all_objects(
|
|
|
+ &self,
|
|
|
+ layout: &GridLayoutRevision,
|
|
|
+ field_revs: &[Arc<FieldRevision>],
|
|
|
+ ) -> Option<HashMap<String, Vec<Arc<T>>>> {
|
|
|
+ // Acquire the read lock.
|
|
|
+ let object_rev_map_by_field_id = self.inner.get(layout)?;
|
|
|
+ // Get the objects according to the FieldType, so we need iterate the field_revs.
|
|
|
+ let objects_by_field_id = field_revs
|
|
|
+ .iter()
|
|
|
+ .flat_map(|field_rev| {
|
|
|
+ let field_type = &field_rev.field_type_rev;
|
|
|
+ let field_id = &field_rev.id;
|
|
|
+
|
|
|
+ let object_rev_map = object_rev_map_by_field_id.get(field_id)?;
|
|
|
+ let objects: Vec<Arc<T>> = object_rev_map.get(field_type)?.clone();
|
|
|
+ Some((field_rev.id.clone(), objects))
|
|
|
+ })
|
|
|
+ .collect::<HashMap<String, Vec<Arc<T>>>>();
|
|
|
+ Some(objects_by_field_id)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn insert_object(
|
|
|
&mut self,
|
|
|
layout: &GridLayoutRevision,
|
|
|
field_id: &str,
|
|
|
field_type: &FieldTypeRevision,
|
|
|
- filter_rev: GridFilterRevision,
|
|
|
+ object: T,
|
|
|
) {
|
|
|
- let filter_rev_map_by_field_id = self.filters.entry(layout.clone()).or_insert_with(IndexMap::new);
|
|
|
- let filter_rev_map = filter_rev_map_by_field_id
|
|
|
+ let object_rev_map_by_field_id = self.inner.entry(layout.clone()).or_insert_with(IndexMap::new);
|
|
|
+ let object_rev_map = object_rev_map_by_field_id
|
|
|
.entry(field_id.to_string())
|
|
|
- .or_insert_with(GridFilterRevisionMap::new);
|
|
|
+ .or_insert_with(GridObjectRevisionMap::<T>::new);
|
|
|
|
|
|
- filter_rev_map
|
|
|
+ object_rev_map
|
|
|
.entry(field_type.to_owned())
|
|
|
.or_insert_with(Vec::new)
|
|
|
- .push(Arc::new(filter_rev))
|
|
|
+ .push(Arc::new(object))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
|
|
-pub struct GridSortRevision {
|
|
|
- pub id: String,
|
|
|
- pub field_id: Option<String>,
|
|
|
-}
|
|
|
-
|
|
|
-pub type GridFilterRevisionMap = GridObjectRevisionMap<GridFilterRevision>;
|
|
|
-pub type GridGroupRevisionMap = GridObjectRevisionMap<GridGroupRevision>;
|
|
|
-
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
|
|
#[serde(transparent)]
|
|
|
pub struct GridObjectRevisionMap<T>
|