1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-08 23:46:12 +02:00

Add Parent field to Database type

Ref: https://developers.notion.com/changelog/database-objects-now-return-parent
This commit is contained in:
David Stotijn 2021-07-20 20:48:35 +02:00
parent 7f6480a766
commit 597978fa0d
4 changed files with 50 additions and 37 deletions

View File

@ -214,6 +214,11 @@ func TestFindDatabaseByID(t *testing.T) {
"type": "files", "type": "files",
"files": {} "files": {}
} }
},
"parent": {
"type": "page_id",
"page_id": "b8595b75-abd1-4cad-8dfe-f935a8ef57cb"
}
} }
}`, }`,
) )
@ -336,6 +341,10 @@ func TestFindDatabaseByID(t *testing.T) {
Type: "files", Type: "files",
}, },
}, },
Parent: notion.Parent{
Type: notion.ParentTypePage,
PageID: "b8595b75-abd1-4cad-8dfe-f935a8ef57cb",
},
}, },
expError: nil, expError: nil,
}, },
@ -622,9 +631,9 @@ func TestQueryDatabase(t *testing.T) {
ID: "7c6b1c95-de50-45ca-94e6-af1d9fd295ab", ID: "7c6b1c95-de50-45ca-94e6-af1d9fd295ab",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-18T17:50:22.371Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-18T17:50:22.371Z"),
LastEditedTime: 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, Type: notion.ParentTypeDatabase,
DatabaseID: notion.StringPtr("39ddfc9d-33c9-404c-89cf-79f01c42dd0c"), DatabaseID: "39ddfc9d-33c9-404c-89cf-79f01c42dd0c",
}, },
Archived: false, Archived: false,
Properties: notion.DatabasePageProperties{ Properties: notion.DatabasePageProperties{
@ -948,9 +957,9 @@ func TestFindPageByID(t *testing.T) {
ID: "606ed832-7d79-46de-bbed-5b4896e7bc02", ID: "606ed832-7d79-46de-bbed-5b4896e7bc02",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"),
LastEditedTime: 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, Type: notion.ParentTypePage,
PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
}, },
Properties: notion.PageProperties{ Properties: notion.PageProperties{
Title: notion.PageTitle{ Title: notion.PageTitle{
@ -1137,9 +1146,9 @@ func TestCreatePage(t *testing.T) {
ID: "276ee233-e426-4ed0-9986-6b22af8550df", ID: "276ee233-e426-4ed0-9986-6b22af8550df",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"),
Parent: notion.PageParent{ Parent: notion.Parent{
Type: notion.ParentTypePage, Type: notion.ParentTypePage,
PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
}, },
Properties: notion.PageProperties{ Properties: notion.PageProperties{
Title: notion.PageTitle{ Title: notion.PageTitle{
@ -1265,9 +1274,9 @@ func TestCreatePage(t *testing.T) {
ID: "276ee233-e426-4ed0-9986-6b22af8550df", ID: "276ee233-e426-4ed0-9986-6b22af8550df",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"),
Parent: notion.PageParent{ Parent: notion.Parent{
Type: notion.ParentTypeDatabase, Type: notion.ParentTypeDatabase,
DatabaseID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), DatabaseID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
}, },
Properties: notion.DatabasePageProperties{ Properties: notion.DatabasePageProperties{
"title": notion.DatabasePageProperty{ "title": notion.DatabasePageProperty{
@ -1512,9 +1521,9 @@ func TestUpdatePageProps(t *testing.T) {
ID: "cb261dc5-6c85-4767-8585-3852382fb466", ID: "cb261dc5-6c85-4767-8585-3852382fb466",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T15:54:31.116Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T15:54:31.116Z"),
Parent: notion.PageParent{ Parent: notion.Parent{
Type: notion.ParentTypePage, Type: notion.ParentTypePage,
PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
}, },
Properties: notion.PageProperties{ Properties: notion.PageProperties{
Title: notion.PageTitle{ Title: notion.PageTitle{
@ -1608,9 +1617,9 @@ func TestUpdatePageProps(t *testing.T) {
ID: "e4f419a7-f01f-4d5b-af58-ff4786a429fe", ID: "e4f419a7-f01f-4d5b-af58-ff4786a429fe",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-17T17:56:00.000Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-17T17:56:00.000Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T16:24:23.007Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T16:24:23.007Z"),
Parent: notion.PageParent{ Parent: notion.Parent{
Type: notion.ParentTypeDatabase, Type: notion.ParentTypeDatabase,
DatabaseID: notion.StringPtr("4cb17949-f08d-4d5c-ab50-fe6ba689d2c8"), DatabaseID: "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8",
}, },
Properties: notion.DatabasePageProperties{ Properties: notion.DatabasePageProperties{
"Name": notion.DatabasePageProperty{ "Name": notion.DatabasePageProperty{
@ -2496,9 +2505,9 @@ func TestSearch(t *testing.T) {
ID: "276ee233-e426-4ed0-9986-6b22af8550df", ID: "276ee233-e426-4ed0-9986-6b22af8550df",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"), CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.068Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"), LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T19:34:05.069Z"),
Parent: notion.PageParent{ Parent: notion.Parent{
Type: notion.ParentTypePage, Type: notion.ParentTypePage,
PageID: notion.StringPtr("b0668f48-8d66-4733-9bdb-2f82215707f7"), PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
}, },
Properties: notion.PageProperties{ Properties: notion.PageProperties{
Title: notion.PageTitle{ Title: notion.PageTitle{

View File

@ -12,6 +12,7 @@ type Database struct {
LastEditedTime time.Time `json:"last_edited_time"` LastEditedTime time.Time `json:"last_edited_time"`
Title []RichText `json:"title"` Title []RichText `json:"title"`
Properties DatabaseProperties `json:"properties"` Properties DatabaseProperties `json:"properties"`
Parent Parent `json:"parent"`
} }
// DatabaseProperties is a mapping of properties defined on a database. // DatabaseProperties is a mapping of properties defined on a database.

24
page.go
View File

@ -14,7 +14,7 @@ type Page struct {
ID string `json:"id"` ID string `json:"id"`
CreatedTime time.Time `json:"created_time"` CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"` LastEditedTime time.Time `json:"last_edited_time"`
Parent PageParent `json:"parent"` Parent Parent `json:"parent"`
Archived bool `json:"archived"` Archived bool `json:"archived"`
// Properties differ between parent type. // Properties differ between parent type.
@ -22,13 +22,6 @@ type Page struct {
Properties interface{} `json:"properties"` 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. // PageProperties are properties of a page whose parent is a page or a workspace.
type PageProperties struct { type PageProperties struct {
Title PageTitle `json:"title"` Title PageTitle `json:"title"`
@ -85,13 +78,6 @@ type UpdatePageParams struct {
Title []RichText 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. // 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. // When type is unknown/unmapped or doesn't have a value, `nil` is returned.
func (prop DatabasePageProperty) Value() interface{} { func (prop DatabasePageProperty) Value() interface{} {
@ -158,17 +144,17 @@ func (p CreatePageParams) Validate() error {
func (p CreatePageParams) MarshalJSON() ([]byte, error) { func (p CreatePageParams) MarshalJSON() ([]byte, error) {
type CreatePageParamsDTO struct { type CreatePageParamsDTO struct {
Parent PageParent `json:"parent"` Parent Parent `json:"parent"`
Properties interface{} `json:"properties"` Properties interface{} `json:"properties"`
Children []Block `json:"children,omitempty"` Children []Block `json:"children,omitempty"`
} }
var parent PageParent var parent Parent
if p.DatabasePageProperties != nil { if p.DatabasePageProperties != nil {
parent.DatabaseID = StringPtr(p.ParentID) parent.DatabaseID = p.ParentID
} else if p.Title != nil { } else if p.Title != nil {
parent.PageID = StringPtr(p.ParentID) parent.PageID = p.ParentID
} }
dto := CreatePageParamsDTO{ dto := CreatePageParamsDTO{

17
parent.go Normal file
View File

@ -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"
)