1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-15 00:05:04 +02:00

Add parent property to blocks (#28)

This commit is contained in:
David Stotijn
2022-08-12 21:03:51 +02:00
committed by GitHub
parent 8c9f519e73
commit a7179ae969
3 changed files with 26 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
// See: https://developers.notion.com/reference/block // See: https://developers.notion.com/reference/block
type Block interface { type Block interface {
ID() string ID() string
Parent() Parent
CreatedTime() time.Time CreatedTime() time.Time
LastEditedTime() time.Time LastEditedTime() time.Time
HasChildren() bool HasChildren() bool
@ -19,6 +20,7 @@ type Block interface {
type blockDTO struct { type blockDTO struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Parent *Parent `json:"parent,omitempty"`
Type BlockType `json:"type,omitempty"` Type BlockType `json:"type,omitempty"`
CreatedTime *time.Time `json:"created_time,omitempty"` CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"` LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
@ -60,6 +62,7 @@ type blockDTO struct {
type baseBlock struct { type baseBlock struct {
id string id string
parent Parent
createdTime time.Time createdTime time.Time
lastEditedTime time.Time lastEditedTime time.Time
hasChildren bool hasChildren bool
@ -87,6 +90,10 @@ func (b baseBlock) Archived() bool {
return b.archived return b.archived
} }
func (b baseBlock) Parent() Parent {
return b.parent
}
type ParagraphBlock struct { type ParagraphBlock struct {
baseBlock baseBlock
@ -831,6 +838,10 @@ func (dto blockDTO) Block() Block {
hasChildren: dto.HasChildren, hasChildren: dto.HasChildren,
} }
if dto.Parent != nil {
baseBlock.parent = *dto.Parent
}
if dto.CreatedTime != nil { if dto.CreatedTime != nil {
baseBlock.createdTime = *dto.CreatedTime baseBlock.createdTime = *dto.CreatedTime
} }

View File

@ -3998,6 +3998,7 @@ func TestFindBlockByID(t *testing.T) {
respStatusCode int respStatusCode int
expBlock notion.Block expBlock notion.Block
expID string expID string
expParent notion.Parent
expCreatedTime time.Time expCreatedTime time.Time
expLastEditedTime time.Time expLastEditedTime time.Time
expHasChildren bool expHasChildren bool
@ -4012,6 +4013,10 @@ func TestFindBlockByID(t *testing.T) {
`{ `{
"object": "block", "object": "block",
"id": "048e165e-352d-4119-8128-e46c3527d95c", "id": "048e165e-352d-4119-8128-e46c3527d95c",
"parent": {
"type": "page_id",
"page_id": "59833787-2cf9-4fdf-8782-e53db20768a5"
},
"created_time": "2021-10-02T06:09:00.000Z", "created_time": "2021-10-02T06:09:00.000Z",
"last_edited_time": "2021-10-02T06:31:00.000Z", "last_edited_time": "2021-10-02T06:31:00.000Z",
"has_children": true, "has_children": true,
@ -4028,6 +4033,10 @@ func TestFindBlockByID(t *testing.T) {
Title: "test title", Title: "test title",
}, },
expID: "048e165e-352d-4119-8128-e46c3527d95c", expID: "048e165e-352d-4119-8128-e46c3527d95c",
expParent: notion.Parent{
Type: notion.ParentTypePage,
PageID: "59833787-2cf9-4fdf-8782-e53db20768a5",
},
expCreatedTime: mustParseTime(time.RFC3339, "2021-10-02T06:09:00Z"), expCreatedTime: mustParseTime(time.RFC3339, "2021-10-02T06:09:00Z"),
expLastEditedTime: mustParseTime(time.RFC3339, "2021-10-02T06:31:00Z"), expLastEditedTime: mustParseTime(time.RFC3339, "2021-10-02T06:31:00Z"),
expHasChildren: true, expHasChildren: true,
@ -4088,6 +4097,10 @@ func TestFindBlockByID(t *testing.T) {
t.Fatalf("id not equal (expected: %v, got: %v)", tt.expID, block.ID()) t.Fatalf("id not equal (expected: %v, got: %v)", tt.expID, block.ID())
} }
if tt.expParent != block.Parent() {
t.Fatalf("parent not equal (expected: %+v, got: %+v)", tt.expParent, block.Parent())
}
if tt.expCreatedTime != block.CreatedTime() { if tt.expCreatedTime != block.CreatedTime() {
t.Fatalf("createdTime not equal (expected: %v, got: %v)", tt.expCreatedTime, block.CreatedTime()) t.Fatalf("createdTime not equal (expected: %v, got: %v)", tt.expCreatedTime, block.CreatedTime())
} }

View File

@ -3,6 +3,7 @@ package notion
type Parent struct { type Parent struct {
Type ParentType `json:"type,omitempty"` Type ParentType `json:"type,omitempty"`
BlockID string `json:"block_id,omitempty"`
PageID string `json:"page_id,omitempty"` PageID string `json:"page_id,omitempty"`
DatabaseID string `json:"database_id,omitempty"` DatabaseID string `json:"database_id,omitempty"`
Workspace bool `json:"workspace,omitempty"` Workspace bool `json:"workspace,omitempty"`