-
Notifications
You must be signed in to change notification settings - Fork 82
/
object.go
217 lines (169 loc) · 4.67 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package notionapi
import (
"fmt"
"time"
)
type ObjectType string
func (ot ObjectType) String() string {
return string(ot)
}
type ObjectID string
func (oID ObjectID) String() string {
return string(oID)
}
type Object interface {
GetObject() ObjectType
}
type Color string
func (c Color) String() string {
return string(c)
}
func (c Color) MarshalText() ([]byte, error) {
if c == "" {
return []byte(ColorDefault), nil
}
return []byte(c), nil
}
type MentionType string
func (mType MentionType) String() string {
return string(mType)
}
type DatabaseMention struct {
ID ObjectID `json:"id"`
}
type PageMention struct {
ID ObjectID `json:"id"`
}
type TemplateMentionType string
func (tMType TemplateMentionType) String() string {
return string(tMType)
}
type TemplateMention struct {
Type TemplateMentionType `json:"type"`
TemplateMentionUser string `json:"template_mention_user,omitempty"`
TemplateMentionDate string `json:"template_mention_date,omitempty"`
}
type Mention struct {
Type MentionType `json:"type,omitempty"`
Database *DatabaseMention `json:"database,omitempty"`
Page *PageMention `json:"page,omitempty"`
User *User `json:"user,omitempty"`
Date *DateObject `json:"date,omitempty"`
TemplateMention *TemplateMention `json:"template_mention,omitempty"`
}
type RichText struct {
Type ObjectType `json:"type,omitempty"`
Text *Text `json:"text,omitempty"`
Mention *Mention `json:"mention,omitempty"`
Equation *Equation `json:"equation,omitempty"`
Annotations *Annotations `json:"annotations,omitempty"`
PlainText string `json:"plain_text,omitempty"`
Href string `json:"href,omitempty"`
}
type Text struct {
Content string `json:"content"`
Link *Link `json:"link,omitempty"`
}
type Link struct {
Url string `json:"url,omitempty"`
}
type Annotations struct {
Bold bool `json:"bold"`
Italic bool `json:"italic"`
Strikethrough bool `json:"strikethrough"`
Underline bool `json:"underline"`
Code bool `json:"code"`
Color Color `json:"color,omitempty"`
}
type RelationObject struct {
Database DatabaseID `json:"database"`
SyncedPropertyName string `json:"synced_property_name"`
}
type FunctionType string
func (ft FunctionType) String() string {
return string(ft)
}
type Cursor string
func (c Cursor) String() string {
return string(c)
}
type Date time.Time
func (d *Date) String() string {
return time.Time(*d).Format(time.RFC3339)
}
func (d Date) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}
func (d *Date) UnmarshalText(data []byte) error {
t, err := time.Parse(time.RFC3339, string(data))
// Because the API does not distinguish between datetime with a
// timezone and dates, we eventually have to try both.
if err != nil {
if _, ok := err.(*time.ParseError); !ok {
return err
} else {
t, err = time.Parse("2006-01-02", string(data)) // Date
if err != nil {
// Still cannot parse it, nothing else to try.
return err
}
}
}
*d = Date(t)
return nil
}
type FileType string
type File struct {
Name string `json:"name"`
Type FileType `json:"type"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}
type FileObject struct {
URL string `json:"url,omitempty"`
ExpiryTime *time.Time `json:"expiry_time,omitempty"`
}
type Icon struct {
Type FileType `json:"type"`
Emoji *Emoji `json:"emoji,omitempty"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}
// GetURL returns the external or internal URL depending on the image type.
func (i Icon) GetURL() string {
if i.File != nil {
return i.File.URL
}
if i.External != nil {
return i.External.URL
}
return ""
}
type Emoji string
type PropertyID string
func (pID PropertyID) String() string {
return string(pID)
}
type Status = Option
type UniqueID struct {
Prefix *string `json:"prefix,omitempty"`
Number int `json:"number"`
}
func (uID UniqueID) String() string {
if uID.Prefix != nil {
return fmt.Sprintf("%s-%d", *uID.Prefix, uID.Number)
}
return fmt.Sprintf("%d", uID.Number)
}
type VerificationState string
func (vs VerificationState) String() string {
return string(vs)
}
// Verification documented here: https://developers.notion.com/reference/page-property-values#verification
type Verification struct {
State VerificationState `json:"state"`
VerifiedBy *User `json:"verified_by,omitempty"`
Date *DateObject `json:"date,omitempty"`
}
type Button struct {
}