Skip to content

Commit 608ccec

Browse files
committed
feat: checkbox
1 parent 22c4418 commit 608ccec

File tree

1 file changed

+61
-13
lines changed

1 file changed

+61
-13
lines changed

collab-database/src/fields/type_option/checkbox_type_option.rs

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use crate::entity::FieldType;
24
use crate::fields::{
35
TypeOptionCellReader, TypeOptionCellWriter, TypeOptionData, TypeOptionDataBuilder,
@@ -19,11 +21,10 @@ impl CheckboxTypeOption {
1921

2022
impl TypeOptionCellReader for CheckboxTypeOption {
2123
fn json_cell(&self, cell: &Cell) -> Value {
22-
let value = match cell.get_as::<String>(CELL_DATA) {
23-
None => "".to_string(),
24-
Some(s) => Self::convert_raw_cell_data(self, &s),
25-
};
26-
Value::String(value)
24+
match cell.get_as::<Arc<str>>(CELL_DATA) {
25+
None => false.into(),
26+
Some(s) => bool_from_str(&s).into(),
27+
}
2728
}
2829

2930
fn numeric_cell(&self, cell: &Cell) -> Option<f64> {
@@ -43,14 +44,13 @@ impl TypeOptionCellReader for CheckboxTypeOption {
4344
impl TypeOptionCellWriter for CheckboxTypeOption {
4445
fn convert_json_to_cell(&self, value: Value) -> Cell {
4546
let mut cell = new_cell_builder(FieldType::Checkbox);
46-
if let Some(data) = match value {
47-
Value::String(s) => Some(s),
48-
Value::Bool(b) => Some(b.to_string()),
49-
Value::Number(n) => Some(n.to_string()),
50-
_ => None,
51-
} {
52-
cell.insert(CELL_DATA.into(), bool_from_str(&data).to_string().into());
53-
}
47+
let checked = match value {
48+
Value::String(s) => bool_from_str(&s),
49+
Value::Bool(b) => b,
50+
Value::Number(n) => n.as_i64().unwrap_or(0) > 0,
51+
_ => false,
52+
};
53+
cell.insert(CELL_DATA.into(), checked.to_string().into());
5454
cell
5555
}
5656
}
@@ -156,4 +156,52 @@ mod tests {
156156
assert!(!bool_from_str("invalid"));
157157
assert!(!bool_from_str(""));
158158
}
159+
160+
#[test]
161+
fn checkbox_cell_to_serde() {
162+
let checkbox_type_option = CheckboxTypeOption::new();
163+
let cell_writer: Box<dyn TypeOptionCellReader> = Box::new(checkbox_type_option);
164+
{
165+
let mut cell: Cell = new_cell_builder(FieldType::RichText);
166+
cell.insert(CELL_DATA.into(), "Yes".into());
167+
let serde_val = cell_writer.json_cell(&cell);
168+
assert_eq!(serde_val, Value::Bool(true));
169+
}
170+
}
171+
172+
#[test]
173+
fn number_serde_to_cell() {
174+
let checkbox_type_option = CheckboxTypeOption::default();
175+
let cell_writer: Box<dyn TypeOptionCellWriter> = Box::new(checkbox_type_option);
176+
{
177+
// empty string
178+
let cell: Cell = cell_writer.convert_json_to_cell(Value::String("".to_string()));
179+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
180+
assert_eq!(data, "false");
181+
}
182+
{
183+
// "yes" in any case
184+
let cell: Cell = cell_writer.convert_json_to_cell(Value::String("yEs".to_string()));
185+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
186+
assert_eq!(data, "true");
187+
}
188+
{
189+
// bool
190+
let cell: Cell = cell_writer.convert_json_to_cell(Value::Bool(true));
191+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
192+
assert_eq!(data, "true");
193+
}
194+
{
195+
// js number
196+
let cell: Cell = cell_writer.convert_json_to_cell(Value::Number(1.into()));
197+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
198+
assert_eq!(data, "true");
199+
}
200+
{
201+
// js null
202+
let cell: Cell = cell_writer.convert_json_to_cell(Value::Null);
203+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
204+
assert_eq!(data, "false");
205+
}
206+
}
159207
}

0 commit comments

Comments
 (0)