-
Notifications
You must be signed in to change notification settings - Fork 3
/
object.go
174 lines (154 loc) · 3.31 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
package notion
import (
"encoding/json"
)
// ObjectType is enum of Notion top level resource types.
type ObjectType string
// ObjectType enums.
const (
ObjectList ObjectType = "list"
ObjectDatabase ObjectType = "database"
ObjectPage ObjectType = "page"
ObjectUser ObjectType = "user"
ObjectBlock ObjectType = "block"
)
// Object is the mix type of Top-level resources.
type Object struct {
Type ObjectType
list *List
database *Database
page *Page
user *User
block *Block
}
// List cast Object to List
func (o *Object) List() *List {
if o == nil {
return nil
}
return o.list
}
// Database casts Object to Database
func (o *Object) Database() *Database {
if o == nil {
return nil
}
return o.database
}
// Page casts Object to Page
func (o *Object) Page() *Page {
if o == nil {
return nil
}
return o.page
}
// User casts Object to User
func (o *Object) User() *User {
if o == nil {
return nil
}
return o.user
}
// Block casts Object to Block
func (o *Object) Block() *Block {
if o == nil {
return nil
}
return o.block
}
func (o *Object) value() (interface{}, bool) {
if o == nil {
return nil, false
}
switch o.Type {
case ObjectList:
o.list = new(List)
return o.list, true
case ObjectDatabase:
o.database = new(Database)
return o.database, true
case ObjectBlock:
o.block = new(Block)
return o.block, true
case ObjectPage:
o.page = new(Page)
return o.page, true
case ObjectUser:
o.user = new(User)
return o.user, true
}
return nil, false
}
var _ json.Marshaler = &Object{}
// MarshalJSON picks the value field for JSON marshalling.
func (o *Object) MarshalJSON() ([]byte, error) {
v, _ := o.value()
return json.Marshal(v)
}
var _ json.Unmarshaler = &Object{}
// UnmarshalJSON unmarshalls bytes to the correct Object field according to "object" type field.
func (o *Object) UnmarshalJSON(bytes []byte) error {
if o == nil {
return nil
}
var tmp struct {
Object ObjectType `json:"object"`
}
if err := json.Unmarshal(bytes, &tmp); err != nil {
return err
}
o.Type = tmp.Object
v, ok := o.value()
if !ok {
return nil
}
return json.Unmarshal(bytes, v)
}
// ObjectReference is simple type of Object that just contains id and type.
type ObjectReference struct {
// Object may be empty in some cases.
Object ObjectType `json:"object,omitempty"`
ID string `json:"id,omitempty"`
}
// Objects is wrapper of Object list.
type Objects []*Object
// Databases extracts valid databases from objects.
func (os Objects) Databases() []*Database {
dest := make([]*Database, 0, len(os))
for _, o := range os {
if v := o.Database(); v != nil {
dest = append(dest, v)
}
}
return dest
}
// Users extracts valid users from objects.
func (os Objects) Users() []*User {
dest := make([]*User, 0, len(os))
for _, o := range os {
if v := o.User(); v != nil {
dest = append(dest, v)
}
}
return dest
}
// Blocks extracts valid blocks from objects.
func (os Objects) Blocks() []*Block {
dest := make([]*Block, 0, len(os))
for _, o := range os {
if v := o.Block(); v != nil {
dest = append(dest, v)
}
}
return dest
}
// Pages extracts valid pages from objects.
func (os Objects) Pages() []*Page {
dest := make([]*Page, 0, len(os))
for _, o := range os {
if v := o.Page(); v != nil {
dest = append(dest, v)
}
}
return dest
}