Skip to content

Commit 67e350e

Browse files
authored
refactor: provide default type option transform (#1579)
1 parent 85e489b commit 67e350e

File tree

15 files changed

+253
-144
lines changed

15 files changed

+253
-144
lines changed

frontend/rust-lib/flowy-grid/src/services/field/type_option_builder.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@ pub trait TypeOptionBuilder {
99

1010
/// Returns a serializer that can be used to serialize the type-option data
1111
fn serializer(&self) -> &dyn TypeOptionDataSerializer;
12-
13-
/// Transform the data from passed-in type-option to current type-option
14-
///
15-
/// The current type-option data may be changed if it supports transform
16-
/// the data from the other kind of type-option data.
17-
///
18-
/// For example, when switching from `checkbox` type-option to `single-select`
19-
/// type-option, adding the `Yes` option if the `single-select` type-option doesn't contain it.
20-
/// But the cell content is a string, `Yes`, it's need to do the cell content transform.
21-
/// The `Yes` string will be transformed to the `Yes` option id.
22-
///
23-
///
24-
/// # Arguments
25-
///
26-
/// * `field_type`: represents as the field type of the passed-in type-option data
27-
/// * `type_option_data`: passed-in type-option data
28-
//
29-
fn transform(&mut self, field_type: &FieldType, type_option_data: String);
3012
}
3113

3214
pub fn default_type_option_builder_from_type(field_type: &FieldType) -> Box<dyn TypeOptionBuilder> {

frontend/rust-lib/flowy-grid/src/services/field/type_options/checkbox_type_option/checkbox_type_option.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::impl_type_option;
33
use crate::services::cell::{AnyCellChangeset, CellDataChangeset, CellDataDecoder, FromCellString};
44
use crate::services::field::{
55
BoxTypeOptionBuilder, CheckboxCellData, TypeOption, TypeOptionBuilder, TypeOptionCellData, TypeOptionConfiguration,
6+
TypeOptionTransform,
67
};
78
use bytes::Bytes;
89
use flowy_derive::ProtoBuf;
@@ -31,10 +32,6 @@ impl TypeOptionBuilder for CheckboxTypeOptionBuilder {
3132
fn serializer(&self) -> &dyn TypeOptionDataSerializer {
3233
&self.0
3334
}
34-
35-
fn transform(&mut self, _field_type: &FieldType, _type_option_data: String) {
36-
// Do nothing
37-
}
3835
}
3936

4037
#[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
@@ -47,15 +44,17 @@ impl_type_option!(CheckboxTypeOptionPB, FieldType::Checkbox);
4744
impl TypeOption for CheckboxTypeOptionPB {
4845
type CellData = CheckboxCellData;
4946
type CellChangeset = CheckboxCellChangeset;
50-
type CellPBType = CheckboxCellData;
47+
type CellProtobufType = CheckboxCellData;
5148
}
5249

50+
impl TypeOptionTransform for CheckboxTypeOptionPB {}
51+
5352
impl TypeOptionConfiguration for CheckboxTypeOptionPB {
5453
type CellFilterConfiguration = CheckboxFilterPB;
5554
}
5655

5756
impl TypeOptionCellData for CheckboxTypeOptionPB {
58-
fn convert_into_pb_type(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellPBType {
57+
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
5958
cell_data
6059
}
6160

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option/date_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod tests {
165165
let decoded_data = type_option
166166
.try_decode_cell_data(encoded_data, &FieldType::DateTime, field_rev)
167167
.unwrap();
168-
let decoded_data = type_option.convert_into_pb_type(decoded_data);
168+
let decoded_data = type_option.convert_to_protobuf(decoded_data);
169169
if type_option.include_time {
170170
format!("{} {}", decoded_data.date, decoded_data.time)
171171
.trim_end()

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option/date_type_option.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::impl_type_option;
33
use crate::services::cell::{AnyCellChangeset, CellDataChangeset, CellDataDecoder, FromCellString};
44
use crate::services::field::{
55
BoxTypeOptionBuilder, DateCellChangeset, DateCellData, DateCellDataPB, DateFormat, TimeFormat, TypeOption,
6-
TypeOptionBuilder, TypeOptionCellData, TypeOptionConfiguration,
6+
TypeOptionBuilder, TypeOptionCellData, TypeOptionConfiguration, TypeOptionTransform,
77
};
88
use bytes::Bytes;
99
use chrono::format::strftime::StrftimeItems;
@@ -30,15 +30,15 @@ impl_type_option!(DateTypeOptionPB, FieldType::DateTime);
3030
impl TypeOption for DateTypeOptionPB {
3131
type CellData = DateCellData;
3232
type CellChangeset = DateCellChangeset;
33-
type CellPBType = DateCellDataPB;
33+
type CellProtobufType = DateCellDataPB;
3434
}
3535

3636
impl TypeOptionConfiguration for DateTypeOptionPB {
3737
type CellFilterConfiguration = DateFilterPB;
3838
}
3939

4040
impl TypeOptionCellData for DateTypeOptionPB {
41-
fn convert_into_pb_type(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellPBType {
41+
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
4242
self.today_desc_from_timestamp(cell_data)
4343
}
4444

@@ -128,6 +128,8 @@ impl DateTypeOptionPB {
128128
}
129129
}
130130

131+
impl TypeOptionTransform for DateTypeOptionPB {}
132+
131133
impl CellDataDecoder for DateTypeOptionPB {
132134
fn try_decode_cell_data(
133135
&self,
@@ -207,7 +209,4 @@ impl TypeOptionBuilder for DateTypeOptionBuilder {
207209
fn serializer(&self) -> &dyn TypeOptionDataSerializer {
208210
&self.0
209211
}
210-
fn transform(&mut self, _field_type: &FieldType, _type_option_data: String) {
211-
// Do nothing
212-
}
213212
}

frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::services::cell::{AnyCellChangeset, CellComparable, CellDataChangeset,
44
use crate::services::field::type_options::number_type_option::format::*;
55
use crate::services::field::{
66
BoxTypeOptionBuilder, NumberCellData, StrCellData, TypeOption, TypeOptionBuilder, TypeOptionCellData,
7-
TypeOptionConfiguration,
7+
TypeOptionConfiguration, TypeOptionTransform,
88
};
99
use bytes::Bytes;
1010
use flowy_derive::ProtoBuf;
@@ -51,9 +51,6 @@ impl TypeOptionBuilder for NumberTypeOptionBuilder {
5151
fn serializer(&self) -> &dyn TypeOptionDataSerializer {
5252
&self.0
5353
}
54-
fn transform(&mut self, _field_type: &FieldType, _type_option_data: String) {
55-
// Do nothing
56-
}
5754
}
5855

5956
// Number
@@ -79,15 +76,15 @@ impl_type_option!(NumberTypeOptionPB, FieldType::Number);
7976
impl TypeOption for NumberTypeOptionPB {
8077
type CellData = StrCellData;
8178
type CellChangeset = NumberCellChangeset;
82-
type CellPBType = StrCellData;
79+
type CellProtobufType = StrCellData;
8380
}
8481

8582
impl TypeOptionConfiguration for NumberTypeOptionPB {
8683
type CellFilterConfiguration = NumberFilterPB;
8784
}
8885

8986
impl TypeOptionCellData for NumberTypeOptionPB {
90-
fn convert_into_pb_type(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellPBType {
87+
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
9188
cell_data
9289
}
9390

@@ -128,6 +125,8 @@ pub(crate) fn strip_currency_symbol<T: ToString>(s: T) -> String {
128125
s
129126
}
130127

128+
impl TypeOptionTransform for NumberTypeOptionPB {}
129+
131130
impl CellDataDecoder for NumberTypeOptionPB {
132131
fn try_decode_cell_data(
133132
&self,

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/checklist_type_option.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::entities::{ChecklistFilterPB, FieldType};
22
use crate::impl_type_option;
33
use crate::services::cell::{AnyCellChangeset, CellDataChangeset, FromCellString, TypeCellData};
4-
use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformer;
4+
55
use crate::services::field::{
66
BoxTypeOptionBuilder, SelectOptionCellChangeset, SelectOptionCellDataPB, SelectOptionIds, SelectOptionPB,
77
SelectTypeOptionSharedAction, TypeOption, TypeOptionBuilder, TypeOptionCellData, TypeOptionConfiguration,
@@ -26,15 +26,15 @@ impl_type_option!(ChecklistTypeOptionPB, FieldType::Checklist);
2626
impl TypeOption for ChecklistTypeOptionPB {
2727
type CellData = SelectOptionIds;
2828
type CellChangeset = SelectOptionCellChangeset;
29-
type CellPBType = SelectOptionCellDataPB;
29+
type CellProtobufType = SelectOptionCellDataPB;
3030
}
3131

3232
impl TypeOptionConfiguration for ChecklistTypeOptionPB {
3333
type CellFilterConfiguration = ChecklistFilterPB;
3434
}
3535

3636
impl TypeOptionCellData for ChecklistTypeOptionPB {
37-
fn convert_into_pb_type(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellPBType {
37+
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
3838
self.get_selected_options(cell_data)
3939
}
4040

@@ -113,8 +113,4 @@ impl TypeOptionBuilder for ChecklistTypeOptionBuilder {
113113
fn serializer(&self) -> &dyn TypeOptionDataSerializer {
114114
&self.0
115115
}
116-
117-
fn transform(&mut self, field_type: &FieldType, type_option_data: String) {
118-
SelectOptionTypeOptionTransformer::transform_type_option(&mut self.0, field_type, type_option_data)
119-
}
120116
}

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::entities::{FieldType, SelectOptionFilterPB};
22
use crate::impl_type_option;
33
use crate::services::cell::{AnyCellChangeset, CellDataChangeset, FromCellString, TypeCellData};
4-
use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformer;
4+
55
use crate::services::field::{
66
BoxTypeOptionBuilder, SelectOptionCellChangeset, SelectOptionCellDataPB, SelectOptionIds, SelectOptionPB,
77
SelectTypeOptionSharedAction, TypeOption, TypeOptionBuilder, TypeOptionCellData, TypeOptionConfiguration,
@@ -26,15 +26,15 @@ impl_type_option!(MultiSelectTypeOptionPB, FieldType::MultiSelect);
2626
impl TypeOption for MultiSelectTypeOptionPB {
2727
type CellData = SelectOptionIds;
2828
type CellChangeset = SelectOptionCellChangeset;
29-
type CellPBType = SelectOptionCellDataPB;
29+
type CellProtobufType = SelectOptionCellDataPB;
3030
}
3131

3232
impl TypeOptionConfiguration for MultiSelectTypeOptionPB {
3333
type CellFilterConfiguration = SelectOptionFilterPB;
3434
}
3535

3636
impl TypeOptionCellData for MultiSelectTypeOptionPB {
37-
fn convert_into_pb_type(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellPBType {
37+
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
3838
self.get_selected_options(cell_data)
3939
}
4040

@@ -119,31 +119,28 @@ impl TypeOptionBuilder for MultiSelectTypeOptionBuilder {
119119
fn serializer(&self) -> &dyn TypeOptionDataSerializer {
120120
&self.0
121121
}
122-
123-
fn transform(&mut self, field_type: &FieldType, type_option_data: String) {
124-
SelectOptionTypeOptionTransformer::transform_type_option(&mut self.0, field_type, type_option_data)
125-
}
126122
}
123+
127124
#[cfg(test)]
128125
mod tests {
129126
use crate::entities::FieldType;
130127
use crate::services::cell::CellDataChangeset;
131128
use crate::services::field::type_options::selection_type_option::*;
132-
use crate::services::field::{CheckboxTypeOptionBuilder, FieldBuilder, TypeOptionBuilder};
129+
use crate::services::field::{CheckboxTypeOptionBuilder, FieldBuilder, TypeOptionBuilder, TypeOptionTransform};
133130
use crate::services::field::{MultiSelectTypeOptionBuilder, MultiSelectTypeOptionPB};
134131

135132
#[test]
136133
fn multi_select_transform_with_checkbox_type_option_test() {
137134
let checkbox_type_option_builder = CheckboxTypeOptionBuilder::default();
138135
let checkbox_type_option_data = checkbox_type_option_builder.serializer().json_str();
139136

140-
let mut multi_select = MultiSelectTypeOptionBuilder::default();
141-
multi_select.transform(&FieldType::Checkbox, checkbox_type_option_data.clone());
142-
debug_assert_eq!(multi_select.0.options.len(), 2);
137+
let mut multi_select = MultiSelectTypeOptionBuilder::default().0;
138+
multi_select.transform_type_option(FieldType::Checkbox, checkbox_type_option_data.clone());
139+
debug_assert_eq!(multi_select.options.len(), 2);
143140

144141
// Already contain the yes/no option. It doesn't need to insert new options
145-
multi_select.transform(&FieldType::Checkbox, checkbox_type_option_data);
146-
debug_assert_eq!(multi_select.0.options.len(), 2);
142+
multi_select.transform_type_option(FieldType::Checkbox, checkbox_type_option_data);
143+
debug_assert_eq!(multi_select.options.len(), 2);
147144
}
148145

149146
#[test]
@@ -158,13 +155,13 @@ mod tests {
158155

159156
let singleselect_type_option_data = singleselect_type_option_builder.serializer().json_str();
160157

161-
let mut multi_select = MultiSelectTypeOptionBuilder::default();
162-
multi_select.transform(&FieldType::MultiSelect, singleselect_type_option_data.clone());
163-
debug_assert_eq!(multi_select.0.options.len(), 2);
158+
let mut multi_select = MultiSelectTypeOptionBuilder::default().0;
159+
multi_select.transform_type_option(FieldType::MultiSelect, singleselect_type_option_data.clone());
160+
debug_assert_eq!(multi_select.options.len(), 2);
164161

165162
// Already contain the yes/no option. It doesn't need to insert new options
166-
multi_select.transform(&FieldType::MultiSelect, singleselect_type_option_data);
167-
debug_assert_eq!(multi_select.0.options.len(), 2);
163+
multi_select.transform_type_option(FieldType::MultiSelect, singleselect_type_option_data);
164+
debug_assert_eq!(multi_select.options.len(), 2);
168165
}
169166

170167
// #[test]

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::services::cell::{
44
CellDataDecoder, CellProtobufBlobParser, DecodedCellData, FromCellChangeset, FromCellString,
55
};
66

7-
use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformer;
7+
use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformHelper;
88
use crate::services::field::{
99
ChecklistTypeOptionPB, MultiSelectTypeOptionPB, SingleSelectTypeOptionPB, TypeOption, TypeOptionCellData,
10+
TypeOptionTransform,
1011
};
1112
use bytes::Bytes;
1213
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
@@ -128,23 +129,42 @@ pub trait SelectTypeOptionSharedAction: TypeOptionDataSerializer + Send + Sync {
128129
fn mut_options(&mut self) -> &mut Vec<SelectOptionPB>;
129130
}
130131

132+
impl<T> TypeOptionTransform for T
133+
where
134+
T: SelectTypeOptionSharedAction + TypeOption<CellData = SelectOptionIds> + TypeOptionDataSerializer,
135+
{
136+
fn transformable(&self) -> bool {
137+
true
138+
}
139+
140+
fn transform_type_option(&mut self, old_type_option_field_type: FieldType, old_type_option_data: String) {
141+
SelectOptionTypeOptionTransformHelper::transform_type_option(
142+
self,
143+
&old_type_option_field_type,
144+
old_type_option_data,
145+
);
146+
}
147+
148+
fn transform_type_option_cell_data(
149+
&self,
150+
cell_data: <Self as TypeOption>::CellData,
151+
decoded_field_type: &FieldType,
152+
) -> <Self as TypeOption>::CellData {
153+
SelectOptionTypeOptionTransformHelper::transform_type_option_cell_data(self, cell_data, decoded_field_type)
154+
}
155+
}
156+
131157
impl<T> CellDataDecoder for T
132158
where
133159
T: SelectTypeOptionSharedAction + TypeOption<CellData = SelectOptionIds> + TypeOptionCellData,
134160
{
135161
fn try_decode_cell_data(
136162
&self,
137163
cell_data: String,
138-
decoded_field_type: &FieldType,
139-
field_rev: &FieldRevision,
164+
_decoded_field_type: &FieldType,
165+
_field_rev: &FieldRevision,
140166
) -> FlowyResult<<Self as TypeOption>::CellData> {
141-
let cell_data = self.decode_type_option_cell_data(cell_data)?;
142-
Ok(SelectOptionTypeOptionTransformer::transform_type_option_cell_data(
143-
self,
144-
cell_data,
145-
decoded_field_type,
146-
field_rev,
147-
))
167+
self.decode_type_option_cell_data(cell_data)
148168
}
149169

150170
fn decode_cell_data_to_str(

0 commit comments

Comments
 (0)