1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-06 23:36:14 +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",
"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{

View File

@ -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.

32
page.go
View File

@ -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{

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