From 397a38bb4d1dc5a2eef186906ee6570837efb2f5 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Mon, 27 Dec 2021 19:37:34 -0800 Subject: fix(#32): Fix rollup value parsing issues (#33) Fixes #32: Rollup values were not being parsed correctly. --- src/models/properties.rs | 83 +++++++++++++++++++++--- src/models/properties/tests.rs | 24 ++++++- src/models/properties/tests/rollup_property.json | 32 +++++++++ 3 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 src/models/properties/tests/rollup_property.json (limited to 'src') diff --git a/src/models/properties.rs b/src/models/properties.rs index d04873e..0a2ed97 100644 --- a/src/models/properties.rs +++ b/src/models/properties.rs @@ -229,14 +229,11 @@ pub struct RelationValue { } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] -#[serde(tag = "type")] -#[serde(rename_all = "snake_case")] +#[serde(tag = "type", rename_all = "snake_case")] pub enum RollupValue { - Number(#[serde(rename = "number")] Option), - Date(#[serde(rename = "date")] Option>), - // Todo: these property values don't have id properties... - // so this likely wont deserialize. would like to minimize duplicated code... - Array(#[serde(rename = "array")] Vec), + Number { number: Option }, + Date { date: Option> }, + Array { array: Vec }, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] @@ -290,9 +287,10 @@ pub enum PropertyValue { id: PropertyId, relation: Option>, }, + /// Rollup { id: PropertyId, - relation: Option, + rollup: Option, }, People { id: PropertyId, @@ -335,3 +333,72 @@ pub enum PropertyValue { last_edited_by: User, }, } + +/// +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum RollupPropertyValue { + /// + #[serde(rename = "rich_text")] + Text { + rich_text: Vec, + }, + /// + Number { + number: Option, + }, + /// + Select { + select: Option, + }, + MultiSelect { + multi_select: Option>, + }, + Date { + date: Option, + }, + /// + Formula { + formula: FormulaResultValue, + }, + /// + /// It is actually an array of relations + Relation { + relation: Option>, + }, + /// + Rollup { + rollup: Option, + }, + People { + people: Vec, + }, + Files { + files: Option>, + }, + Checkbox { + checkbox: bool, + }, + Url { + url: Option, + }, + Email { + email: Option, + }, + PhoneNumber { + phone_number: String, + }, + CreatedTime { + created_time: DateTime, + }, + CreatedBy { + created_by: User, + }, + LastEditedTime { + last_edited_time: DateTime, + }, + LastEditedBy { + last_edited_by: User, + }, +} diff --git a/src/models/properties/tests.rs b/src/models/properties/tests.rs index 8fe5b42..022f080 100644 --- a/src/models/properties/tests.rs +++ b/src/models/properties/tests.rs @@ -1,4 +1,4 @@ -use super::{DateOrDateTime, PropertyValue}; +use super::{DateOrDateTime, PropertyValue, RollupPropertyValue, RollupValue}; use chrono::NaiveDate; #[test] @@ -32,3 +32,25 @@ fn parse_text_property_with_link() { let _property: PropertyValue = serde_json::from_str(include_str!("tests/text_with_link.json")).unwrap(); } + +#[test] +fn parse_rollup_property() { + let property: PropertyValue = + serde_json::from_str(include_str!("tests/rollup_property.json")).unwrap(); + + assert!(matches!( + property, + PropertyValue::Rollup { + rollup: Some(RollupValue::Array { .. }), + .. + } + )); + + if let PropertyValue::Rollup { + rollup: Some(RollupValue::Array { array }), + .. + } = property + { + assert!(matches!(array[0], RollupPropertyValue::Text { .. })) + } +} diff --git a/src/models/properties/tests/rollup_property.json b/src/models/properties/tests/rollup_property.json new file mode 100644 index 0000000..c80d33b --- /dev/null +++ b/src/models/properties/tests/rollup_property.json @@ -0,0 +1,32 @@ +{ + "id": "R%7Cm%3F", + "type": "rollup", + "rollup": { + "type": "array", + "array": [ + { + "type": "rich_text", + "rich_text": [ + { + "type": "text", + "text": { + "content": "personal", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "personal", + "href": null + } + ] + } + ], + "function": "show_original" + } +} -- cgit v1.2.3