Skip to content

Commit

Permalink
Separate database properties from Properties as PropertiesConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
jomei committed Aug 12, 2021
1 parent ba3cd5d commit 86db1d0
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 282 deletions.
54 changes: 28 additions & 26 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ type ParagraphBlock struct {
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Paragraph struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"paragraph"`
Paragraph Paragraph `json:"paragraph"`
}

func (b *ParagraphBlock) GetType() BlockType {
type Paragraph struct {
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
}

func (b ParagraphBlock) GetType() BlockType {
return b.Type
}

Expand All @@ -112,11 +114,11 @@ type Heading1Block struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading1 struct {
Text Paragraph `json:"text"`
Text []RichText `json:"text"`
} `json:"heading_1"`
}

func (b *Heading1Block) GetType() BlockType {
func (b Heading1Block) GetType() BlockType {
return b.Type
}

Expand All @@ -128,11 +130,11 @@ type Heading2Block struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading2 struct {
Text Paragraph `json:"text"`
Text []RichText `json:"text"`
} `json:"heading_2"`
}

func (b *Heading2Block) GetType() BlockType {
func (b Heading2Block) GetType() BlockType {
return b.Type
}

Expand All @@ -144,11 +146,11 @@ type Heading3Block struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading3 struct {
Text Paragraph `json:"text"`
Text []RichText `json:"text"`
} `json:"heading_3"`
}

func (b *Heading3Block) GetType() BlockType {
func (b Heading3Block) GetType() BlockType {
return b.Type
}

Expand All @@ -160,12 +162,12 @@ type BulletedListItemBlock struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
BulletedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"bulleted_list_item"`
}

func (b *BulletedListItemBlock) GetType() BlockType {
func (b BulletedListItemBlock) GetType() BlockType {
return b.Type
}

Expand All @@ -177,12 +179,12 @@ type NumberedListItemBlock struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
NumberedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"numbered_list_item"`
}

func (b *NumberedListItemBlock) GetType() BlockType {
func (b NumberedListItemBlock) GetType() BlockType {
return b.Type
}

Expand All @@ -194,13 +196,13 @@ type ToDoBlock struct {
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children"`
ToDo struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Checked bool `json:"checked"`
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
Checked bool `json:"checked"`
} `json:"to_do"`
}

func (b *ToDoBlock) GetType() BlockType {
func (b ToDoBlock) GetType() BlockType {
return b.Type
}

Expand All @@ -211,15 +213,15 @@ type ToggleBlock struct {
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Text Paragraph `json:"text"`
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
Toggle struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"toggle"`
}

func (b *ToggleBlock) GetType() BlockType {
func (b ToggleBlock) GetType() BlockType {
return b.Type
}

Expand All @@ -235,7 +237,7 @@ type ChildPageBlock struct {
} `json:"child_page"`
}

func (b *ChildPageBlock) GetType() BlockType {
func (b ChildPageBlock) GetType() BlockType {
return b.Type
}

Expand Down
4 changes: 2 additions & 2 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestBlockClient(t *testing.T) {
Object: notionapi.ObjectTypeBlock,
Type: notionapi.BlockTypeHeading2,
Heading2: struct {
Text notionapi.Paragraph `json:"text"`
}{notionapi.Paragraph{
Text []notionapi.RichText `json:"text"`
}{[]notionapi.RichText{
{
Type: notionapi.ObjectTypeText,
Text: notionapi.Text{Content: "Hello"},
Expand Down
51 changes: 44 additions & 7 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,45 @@ const (
ObjectTypeError ObjectType = "error"
)

const (
PropertyConfigTypeTitle PropertyConfigType = "title"
PropertyConfigTypeRichText PropertyConfigType = "rich_text"
PropertyConfigTypeNumber PropertyConfigType = "number"
PropertyConfigTypeSelect PropertyConfigType = "select"
PropertyConfigTypeMultiSelect PropertyConfigType = "multi_select"
PropertyConfigTypeDate PropertyConfigType = "date"
PropertyConfigTypePeople PropertyConfigType = "people"
PropertyConfigTypeFiles PropertyConfigType = "files"
PropertyConfigTypeCheckbox PropertyConfigType = "checkbox"
PropertyConfigTypeURL PropertyConfigType = "url"
PropertyConfigTypeEmail PropertyConfigType = "email"
PropertyConfigTypePhoneNumber PropertyConfigType = "phone_number"
PropertyConfigTypeFormula PropertyConfigType = "formula"
PropertyConfigTypeRelation PropertyConfigType = "relation"
PropertyConfigTypeRollup PropertyConfigType = "rollup"
PropertyConfigCreatedTime PropertyConfigType = "created_time"
PropertyConfigCreatedBy PropertyConfigType = "created_by"
PropertyConfigLastEditedTime PropertyConfigType = "last_edited_time"
PropertyConfigLastEditedBy PropertyConfigType = "last_edited_by"
)

const (
PropertyTypeTitle PropertyType = "title"
PropertyTypeRichText PropertyType = "rich_text"
PropertyTypeText PropertyType = "text"
PropertyTypeNumber PropertyType = "number"
PropertyTypeSelect PropertyType = "select"
PropertyTypeMultiSelect PropertyType = "multi_select"
PropertyTypeNumber PropertyType = "number"
PropertyTypeCheckbox PropertyType = "checkbox"
PropertyTypeEmail PropertyType = "email"
PropertyTypeURL PropertyType = "url"
PropertyTypeFile PropertyType = "file"
PropertyTypePhoneNumber PropertyType = "phone_number"
PropertyTypeFormula PropertyType = "formula"
PropertyTypeDate PropertyType = "date"
PropertyTypeFormula PropertyType = "formula"
PropertyTypeRelation PropertyType = "relation"
PropertyTypeRollup PropertyType = "rollup"
PropertyTypePeople PropertyType = "people"
PropertyTypeFiles PropertyType = "files"
PropertyTypeCheckbox PropertyType = "checkbox"
PropertyTypeURL PropertyType = "url"
PropertyTypeEmail PropertyType = "email"
PropertyTypePhoneNumber PropertyType = "phone_number"
PropertyTypeCreatedTime PropertyType = "created_time"
PropertyTypeCreatedBy PropertyType = "created_by"
PropertyTypeLastEditedTime PropertyType = "last_edited_time"
Expand Down Expand Up @@ -143,6 +166,7 @@ const (
const (
ParentTypeDatabaseID ParentType = "database_id"
ParentTypePageID ParentType = "page_id"
ParentTypeWorkspace ParentType = "workspace"
)

const (
Expand All @@ -164,3 +188,16 @@ const (
BlockTypeChildPage BlockType = "child_page"
BlockTypeUnsupported BlockType = "unsupported"
)

const (
FormulaTypeString FormulaType = "string"
FormulaTypeNumber FormulaType = "number"
FormulaTypeBoolean FormulaType = "boolean"
FormulaTypeDate FormulaType = "date"
)

const (
RollupTypeNumber RollupType = "number"
RollupTypeDate RollupType = "date"
RollupTypeArray RollupType = "array"
)
5 changes: 3 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ type Database struct {
ID ObjectID `json:"id"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
Title Paragraph `json:"title"`
Properties Properties `json:"properties"`
Title []RichText `json:"title"`
// Properties is a map of property configurations that defines what Page.Properties each page of the database can use
Properties PropertyConfigs `json:"properties"`
}

func (db *Database) GetObject() ObjectType {
Expand Down
39 changes: 17 additions & 22 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,23 @@ func TestDatabaseClient(t *testing.T) {
Href: "",
},
},

// Properties: notionapi.Properties{
// "Tags": notionapi.MultiSelectProperty{
// ID: ";s|V",
// Type: notionapi.PropertyTypeMultiSelect,
// MultiSelect: notionapi.Select{Options: []notionapi.Option{{ID: "id", Name: "tag", Color: "Blue"}}},
// },
// "Some another column": notionapi.PeopleProperty{
// ID: "rJt\\",
// Type: notionapi.PropertyTypePeople,
// },
// "SomeColumn": notionapi.RichTextProperty{
// ID: "~j_@",
// Type: notionapi.PropertyTypeRichText,
// RichText: notionapi.RichText{},
// },
// "Name": notionapi.TitleProperty{
// ID: "title",
// Type: notionapi.PropertyTypeTitle,
// Title: notionapi.RichText{},
// },
//Properties: notionapi.PropertyConfigs{
// "Tags": notionapi.MultiSelectPropertyConfig{
// ID: ";s|V",
// Type: notionapi.PropertyConfigTypeMultiSelect,
// MultiSelect: notionapi.Select{Options: []notionapi.Option{{ID: "id", Name: "tag", Color: "Blue"}}},
// },
// "Some another column": notionapi.PeoplePropertyConfig{
// ID: "rJt\\",
// Type: notionapi.PropertyConfigTypePeople,
// },
//
// "Name": notionapi.TitlePropertyConfig{
// ID: "title",
// Type: notionapi.PropertyConfigTypeTitle,
// Title: notionapi.RichText{},
// },
//},
},
wantErr: false,
},
Expand Down Expand Up @@ -110,7 +105,7 @@ func TestDatabaseClient(t *testing.T) {
ID: "some_id",
CreatedTime: timestamp,
LastEditedTime: timestamp,
Title: notionapi.Paragraph{
Title: []notionapi.RichText{
{
Type: notionapi.ObjectTypeText,
Text: notionapi.Text{
Expand Down
18 changes: 11 additions & 7 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ type Annotations struct {
Color Color `json:"color"`
}

type Paragraph []RichText

type FormulaObject struct {
Value string `json:"value"`
}

type RelationObject struct {
Database DatabaseID `json:"database"`
SyncedPropertyName string `json:"synced_property_name"`
Expand All @@ -75,6 +69,16 @@ func (d *Date) String() string {
return time.Time(*d).Format(time.RFC3339)
}

func (d *Date) MarshalText() ([]byte, error) {
func (d Date) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}

type File struct {
Name string `json:"name"`
}

type PropertyID string

func (pID PropertyID) String() string {
return string(pID)
}
2 changes: 1 addition & 1 deletion page.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p *Page) GetObject() ObjectType {
type ParentType string

type Parent struct {
Type ParentType `json:"type"`
Type ParentType `json:"type,omitempty"`
PageID PageID `json:"page_id,omitempty"`
DatabaseID DatabaseID `json:"database_id,omitempty"`
}
Expand Down
Loading

0 comments on commit 86db1d0

Please sign in to comment.