From 597978fa0d1c7957366b598f7178ad6432b69ca2 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 20 Jul 2021 20:48:35 +0200 Subject: [PATCH] Add `Parent` field to `Database` type Ref: https://developers.notion.com/changelog/database-objects-now-return-parent --- client_test.go | 37 +++++++++++++++++++++++-------------- database.go | 1 + page.go | 32 +++++++++----------------------- parent.go | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 parent.go diff --git a/client_test.go b/client_test.go index 65e1487..5584b91 100644 --- a/client_test.go +++ b/client_test.go @@ -214,6 +214,11 @@ func TestFindDatabaseByID(t *testing.T) { "type": "files", "files": {} } + }, + "parent": { + "type": "page_id", + "page_id": "b8595b75-abd1-4cad-8dfe-f935a8ef57cb" + } } }`, ) @@ -336,6 +341,10 @@ func TestFindDatabaseByID(t *testing.T) { Type: "files", }, }, + Parent: notion.Parent{ + Type: notion.ParentTypePage, + PageID: "b8595b75-abd1-4cad-8dfe-f935a8ef57cb", + }, }, expError: nil, }, @@ -622,9 +631,9 @@ func TestQueryDatabase(t *testing.T) { ID: "7c6b1c95-de50-45ca-94e6-af1d9fd295ab", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-18T17:50:22.371Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-18T17:50:22.371Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypeDatabase, - DatabaseID: notion.StringPtr("39ddfc9d-33c9-404c-89cf-79f01c42dd0c"), + DatabaseID: "39ddfc9d-33c9-404c-89cf-79f01c42dd0c", }, Archived: false, Properties: notion.DatabasePageProperties{ @@ -948,9 +957,9 @@ func TestFindPageByID(t *testing.T) { ID: "606ed832-7d79-46de-bbed-5b4896e7bc02", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypePage, - PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, Properties: notion.PageProperties{ Title: notion.PageTitle{ @@ -1137,9 +1146,9 @@ func TestCreatePage(t *testing.T) { ID: "276ee233-e426-4ed0-9986-6b22af8550df", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypePage, - PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, Properties: notion.PageProperties{ Title: notion.PageTitle{ @@ -1265,9 +1274,9 @@ func TestCreatePage(t *testing.T) { ID: "276ee233-e426-4ed0-9986-6b22af8550df", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypeDatabase, - DatabaseID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), + DatabaseID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, Properties: notion.DatabasePageProperties{ "title": notion.DatabasePageProperty{ @@ -1512,9 +1521,9 @@ func TestUpdatePageProps(t *testing.T) { ID: "cb261dc5-6c85-4767-8585-3852382fb466", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T15:54:31.116Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypePage, - PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, Properties: notion.PageProperties{ Title: notion.PageTitle{ @@ -1608,9 +1617,9 @@ func TestUpdatePageProps(t *testing.T) { ID: "e4f419a7-f01f-4d5b-af58-ff4786a429fe", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-17T17:56:00.000Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T16:24:23.007Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypeDatabase, - DatabaseID: notion.StringPtr("4cb17949-f08d-4d5c-ab50-fe6ba689d2c8"), + DatabaseID: "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8", }, Properties: notion.DatabasePageProperties{ "Name": notion.DatabasePageProperty{ @@ -2496,9 +2505,9 @@ func TestSearch(t *testing.T) { ID: "276ee233-e426-4ed0-9986-6b22af8550df", CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), - Parent: notion.PageParent{ + Parent: notion.Parent{ Type: notion.ParentTypePage, - PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, Properties: notion.PageProperties{ Title: notion.PageTitle{ diff --git a/database.go b/database.go index 0ae2231..471beca 100644 --- a/database.go +++ b/database.go @@ -12,6 +12,7 @@ type Database struct { LastEditedTime time.Time `json:"last_edited_time"` Title []RichText `json:"title"` Properties DatabaseProperties `json:"properties"` + Parent Parent `json:"parent"` } // DatabaseProperties is a mapping of properties defined on a database. diff --git a/page.go b/page.go index 7eb7cf5..68a5dd6 100644 --- a/page.go +++ b/page.go @@ -11,24 +11,17 @@ import ( // another page, or a database. // See: https://developers.notion.com/reference/page type Page struct { - ID string `json:"id"` - CreatedTime time.Time `json:"created_time"` - LastEditedTime time.Time `json:"last_edited_time"` - Parent PageParent `json:"parent"` - Archived bool `json:"archived"` + ID string `json:"id"` + CreatedTime time.Time `json:"created_time"` + LastEditedTime time.Time `json:"last_edited_time"` + Parent Parent `json:"parent"` + Archived bool `json:"archived"` // Properties differ between parent type. // See the `UnmarshalJSON` method. Properties interface{} `json:"properties"` } -type PageParent struct { - Type ParentType `json:"type,omitempty"` - - PageID *string `json:"page_id,omitempty"` - DatabaseID *string `json:"database_id,omitempty"` -} - // PageProperties are properties of a page whose parent is a page or a workspace. type PageProperties struct { Title PageTitle `json:"title"` @@ -85,13 +78,6 @@ type UpdatePageParams struct { Title []RichText } -type ParentType string - -const ( - ParentTypeDatabase ParentType = "database_id" - ParentTypePage ParentType = "page_id" -) - // Value returns the underlying database page property value, based on its `type` field. // When type is unknown/unmapped or doesn't have a value, `nil` is returned. func (prop DatabasePageProperty) Value() interface{} { @@ -158,17 +144,17 @@ func (p CreatePageParams) Validate() error { func (p CreatePageParams) MarshalJSON() ([]byte, error) { type CreatePageParamsDTO struct { - Parent PageParent `json:"parent"` + Parent Parent `json:"parent"` Properties interface{} `json:"properties"` Children []Block `json:"children,omitempty"` } - var parent PageParent + var parent Parent if p.DatabasePageProperties != nil { - parent.DatabaseID = StringPtr(p.ParentID) + parent.DatabaseID = p.ParentID } else if p.Title != nil { - parent.PageID = StringPtr(p.ParentID) + parent.PageID = p.ParentID } dto := CreatePageParamsDTO{ diff --git a/parent.go b/parent.go new file mode 100644 index 0000000..3f1de2d --- /dev/null +++ b/parent.go @@ -0,0 +1,17 @@ +package notion + +type Parent struct { + Type ParentType `json:"type,omitempty"` + + PageID string `json:"page_id,omitempty"` + DatabaseID string `json:"database_id,omitempty"` + Workspace bool `json:"workspace,omitempty"` +} + +type ParentType string + +const ( + ParentTypeDatabase ParentType = "database_id" + ParentTypePage ParentType = "page_id" + ParentTypeWorkspace ParentType = "workspace" +)