Skip to content

Commit

Permalink
fix(#32): Fix rollup value parsing issues (#33)
Browse files Browse the repository at this point in the history
Fixes #32: Rollup values were not being parsed correctly.
  • Loading branch information
jakeswenson committed Dec 28, 2021
1 parent 1f66869 commit 397a38b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 9 deletions.
83 changes: 75 additions & 8 deletions src/models/properties.rs
Expand Up @@ -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<Number>),
Date(#[serde(rename = "date")] Option<DateTime<Utc>>),
// 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<PropertyValue>),
Number { number: Option<Number> },
Date { date: Option<DateTime<Utc>> },
Array { array: Vec<RollupPropertyValue> },
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
Expand Down Expand Up @@ -290,9 +287,10 @@ pub enum PropertyValue {
id: PropertyId,
relation: Option<Vec<RelationValue>>,
},
/// <https://developers.notion.com/reference/page#rollup-property-values>
Rollup {
id: PropertyId,
relation: Option<Rollup>,
rollup: Option<RollupValue>,
},
People {
id: PropertyId,
Expand Down Expand Up @@ -335,3 +333,72 @@ pub enum PropertyValue {
last_edited_by: User,
},
}

/// <https://developers.notion.com/reference/page#rollup-property-value-element>
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum RollupPropertyValue {
/// <https://developers.notion.com/reference/page#rich-text-property-values>
#[serde(rename = "rich_text")]
Text {
rich_text: Vec<RichText>,
},
/// <https://developers.notion.com/reference/page#number-property-values>
Number {
number: Option<Number>,
},
/// <https://developers.notion.com/reference/page#select-property-values>
Select {
select: Option<SelectedValue>,
},
MultiSelect {
multi_select: Option<Vec<SelectedValue>>,
},
Date {
date: Option<DateValue>,
},
/// <https://developers.notion.com/reference/page#formula-property-values>
Formula {
formula: FormulaResultValue,
},
/// <https://developers.notion.com/reference/page#relation-property-values>
/// It is actually an array of relations
Relation {
relation: Option<Vec<RelationValue>>,
},
/// <https://developers.notion.com/reference/page#rollup-property-values>
Rollup {
rollup: Option<RollupValue>,
},
People {
people: Vec<User>,
},
Files {
files: Option<Vec<FileReference>>,
},
Checkbox {
checkbox: bool,
},
Url {
url: Option<String>,
},
Email {
email: Option<String>,
},
PhoneNumber {
phone_number: String,
},
CreatedTime {
created_time: DateTime<Utc>,
},
CreatedBy {
created_by: User,
},
LastEditedTime {
last_edited_time: DateTime<Utc>,
},
LastEditedBy {
last_edited_by: User,
},
}
24 changes: 23 additions & 1 deletion src/models/properties/tests.rs
@@ -1,4 +1,4 @@
use super::{DateOrDateTime, PropertyValue};
use super::{DateOrDateTime, PropertyValue, RollupPropertyValue, RollupValue};
use chrono::NaiveDate;

#[test]
Expand Down Expand Up @@ -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 { .. }))
}
}
32 changes: 32 additions & 0 deletions 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"
}
}

0 comments on commit 397a38b

Please sign in to comment.