Repository: jomei/notionapi
Pull request #70 (2022-04-26 19:54:30)

Add methods to Block to retrieve CreatedBy and LastEditedBy
This adds getters for the CreatedBy and LastEditedBy fields of the Block
interface and the BasicBlock struct.

Missed this during the previous PR.


Patch:
diff --git a/block.go b/block.go
index 6cee215..7c0e3ff 100644
--- a/block.go
+++ b/block.go
@@ -158,6 +158,8 @@ type Block interface {
 	GetObject() ObjectType
 	GetCreatedTime() *time.Time
 	GetLastEditedTime() *time.Time
+	GetCreatedBy() *User
+	GetLastEditedBy() *User
 	GetHasChildren() bool
 	GetArchived() bool
 }
@@ -217,6 +219,14 @@ func (b BasicBlock) GetLastEditedTime() *time.Time {
 	return b.LastEditedTime
 }
 
+func (b BasicBlock) GetCreatedBy() *User {
+	return b.CreatedBy
+}
+
+func (b BasicBlock) GetLastEditedBy() *User {
+	return b.LastEditedBy
+}
+
 func (b BasicBlock) GetHasChildren() bool {
 	return b.HasChildren
 }
@@ -225,6 +235,8 @@ func (b BasicBlock) GetArchived() bool {
 	return b.Archived
 }
 
+var _ Block = (*BasicBlock)(nil)
+
 type ParagraphBlock struct {
 	BasicBlock
 	Paragraph Paragraph `json:"paragraph"`
@@ -243,7 +255,7 @@ type Heading1Block struct {
 }
 
 type Heading struct {
-	Text     []RichText `json:"text"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 }
 
@@ -263,7 +275,9 @@ type CalloutBlock struct {
 }
 
 type Callout struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Icon     *Icon      `json:"icon,omitempty"`
 	Children Blocks     `json:"children,omitempty"`
 }
@@ -274,7 +288,9 @@ type QuoteBlock struct {
 }
 
 type Quote struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 }
 
@@ -305,7 +321,9 @@ type BulletedListItemBlock struct {
 }
 
 type ListItem struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 }
 
@@ -320,20 +338,26 @@ type ToDoBlock struct {
 }
 
 type ToDo struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 	Checked  bool       `json:"checked,omitempty"`
 }
 
 type ToggleBlock struct {
 	BasicBlock
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 	Toggle   Toggle     `json:"toggle"`
 }
 
 type Toggle struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 }
 
@@ -383,7 +407,9 @@ type CodeBlock struct {
 }
 
 type Code struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Language string     `json:"language"`
 }
 
@@ -524,7 +550,9 @@ type TemplateBlock struct {
 }
 
 type Template struct {
-	Text     []RichText `json:"text"`
+	// DEPRECATED use RichText instead
+	Text     []RichText `json:"text,omitempty"`
+	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
 }
 


Test patch:
diff --git a/block_test.go b/block_test.go
index b7baf2b..d4ec63e 100644
--- a/block_test.go
+++ b/block_test.go
@@ -78,7 +78,7 @@ func TestBlockClient(t *testing.T) {
 								Type:   notionapi.BlockTypeHeading2,
 							},
 							Heading2: struct {
-								Text     []notionapi.RichText `json:"text"`
+								RichText []notionapi.RichText `json:"rich_text"`
 								Children notionapi.Blocks     `json:"children,omitempty"`
 							}{[]notionapi.RichText{
 								{
@@ -318,7 +318,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 							LastEditedBy:   user,
 						},
 						Callout: notionapi.Callout{
-							Text: []notionapi.RichText{
+							RichText: []notionapi.RichText{
 								{
 									Type: "text",
 									Text: notionapi.Text{
@@ -357,7 +357,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 							LastEditedBy:   user,
 						},
 						Heading1: notionapi.Heading{
-							Text: []notionapi.RichText{
+							RichText: []notionapi.RichText{
 								{
 									Type: "text",
 									Text: notionapi.Text{
@@ -410,7 +410,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 							LastEditedBy:   user,
 						},
 						Heading3: notionapi.Heading{
-							Text: []notionapi.RichText{
+							RichText: []notionapi.RichText{
 								{
 									Type: "text",
 									Text: notionapi.Text{
diff --git a/page_test.go b/page_test.go
index 6adb863..256e961 100644
--- a/page_test.go
+++ b/page_test.go
@@ -426,10 +426,10 @@ func TestPageCreateRequest_MarshallJSON(t *testing.T) {
 							Type:   notionapi.BlockTypeHeading2,
 						},
 						Heading2: struct {
-							Text     []notionapi.RichText `json:"text"`
+							RichText []notionapi.RichText `json:"rich_text"`
 							Children notionapi.Blocks     `json:"children,omitempty"`
 						}{
-							Text: []notionapi.RichText{
+							RichText: []notionapi.RichText{
 								{
 									Type: notionapi.ObjectTypeText,
 									Text: notionapi.Text{Content: "Lacinato"},
@@ -458,7 +458,7 @@ func TestPageCreateRequest_MarshallJSON(t *testing.T) {
 					},
 				},
 			},
-			want: []byte(`{"parent":{"database_id":"some_id"},"properties":{"Link":{"url":"some_url"},"Name":{"title":[{"text":{"content":"New Media Article"}}]},"Publishing/Release Date":{"date":{"start":"2020-12-08T12:00:00Z","end":null}},"Read":{"checkbox":false},"Summary":{"text":[{"type":"text","text":{"content":"Some content"},"annotations":{"bold":true,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"blue"},"plain_text":"Some content"}]},"Type":{"select":{"id":"some_id","name":"Article","color":"default"}}},"children":[{"object":"block","type":"heading_2","heading_2":{"text":[{"type":"text","text":{"content":"Lacinato"}}]}},{"object":"block","type":"paragraph","paragraph":{"rich_text":[{"text":{"content":"Lacinato","link":{"url":"some_url"}}}]}}]}`),
+			want: []byte(`{"parent":{"database_id":"some_id"},"properties":{"Link":{"url":"some_url"},"Name":{"title":[{"text":{"content":"New Media Article"}}]},"Publishing/Release Date":{"date":{"start":"2020-12-08T12:00:00Z","end":null}},"Read":{"checkbox":false},"Summary":{"text":[{"type":"text","text":{"content":"Some content"},"annotations":{"bold":true,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"blue"},"plain_text":"Some content"}]},"Type":{"select":{"id":"some_id","name":"Article","color":"default"}}},"children":[{"object":"block","type":"heading_2","heading_2":{"rich_text":[{"type":"text","text":{"content":"Lacinato"}}]}},{"object":"block","type":"paragraph","paragraph":{"rich_text":[{"text":{"content":"Lacinato","link":{"url":"some_url"}}}]}}]}`),
 		},
 	}
 
diff --git a/testdata/block_array_unmarshal.json b/testdata/block_array_unmarshal.json
index 09b3b8a..5285f40 100644
--- a/testdata/block_array_unmarshal.json
+++ b/testdata/block_array_unmarshal.json
@@ -13,7 +13,7 @@
         "id": "some_id"
       },
     "callout": {
-        "text": [{
+        "rich_text": [{
             "type": "text",
             "text": {
                 "content": "This page is designed to be shared with students on the web. Click "
@@ -62,7 +62,7 @@
         "id": "some_id"
       },
     "heading_1": {
-        "text": [{
+        "rich_text": [{
             "type": "text",
             "text": {
                 "content": "History 340"
@@ -126,7 +126,7 @@
         "id": "some_id"
       },
     "heading_3": {
-        "text": [{
+        "rich_text": [{
             "type": "text",
             "text": {
                 "content": "Assignment Submission"