-
Notifications
You must be signed in to change notification settings - Fork 39
/
search.go
82 lines (67 loc) · 1.75 KB
/
search.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
package notion
import (
"encoding/json"
"fmt"
)
type SearchOpts struct {
Query string `json:"query,omitempty"`
Sort *SearchSort `json:"sort,omitempty"`
Filter *SearchFilter `json:"filter,omitempty"`
StartCursor string `json:"start_cursor,omitempty"`
PageSize int `json:"page_size,omitempty"`
}
type SearchSort struct {
Direction SortDirection `json:"direction,omitempty"`
Timestamp SearchSortTimestamp `json:"timestamp"`
}
type SearchSortTimestamp string
type SearchFilter struct {
Value string `json:"value"`
Property string `json:"property"`
}
type SearchResponse struct {
// Results are either pages or databases. See `SearchResponse.UnmarshalJSON`.
Results SearchResults `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}
type SearchResults []interface{}
const SearchSortTimestampLastEditedTime SearchSortTimestamp = "last_edited_time"
func (sr *SearchResults) UnmarshalJSON(b []byte) error {
rawResults := []json.RawMessage{}
err := json.Unmarshal(b, &rawResults)
if err != nil {
return err
}
type Object struct {
Object string `json:"object"`
}
results := make(SearchResults, len(rawResults))
for i, rawResult := range rawResults {
obj := Object{}
err := json.Unmarshal(rawResult, &obj)
if err != nil {
return err
}
switch obj.Object {
case "database":
var db Database
err := json.Unmarshal(rawResult, &db)
if err != nil {
return err
}
results[i] = db
case "page":
var page Page
err := json.Unmarshal(rawResult, &page)
if err != nil {
return err
}
results[i] = page
default:
return fmt.Errorf("unsupported result object %q", obj.Object)
}
}
*sr = results
return nil
}