-
Notifications
You must be signed in to change notification settings - Fork 85
/
util.go
185 lines (166 loc) · 3.78 KB
/
util.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
package notionapi
import (
"fmt"
"io"
"strings"
)
type NotionID struct {
DashID string
NoDashID string
}
func NewNotionID(maybeID string) *NotionID {
if IsValidDashID(maybeID) {
return &NotionID{
DashID: maybeID,
NoDashID: ToNoDashID(maybeID),
}
}
if IsValidNoDashID(maybeID) {
return &NotionID{
DashID: ToDashID(maybeID),
NoDashID: maybeID,
}
}
return nil
}
var (
dashIDLen = len("2131b10c-ebf6-4938-a127-7089ff02dbe4")
noDashIDLen = len("2131b10cebf64938a1277089ff02dbe4")
)
// only hex chars seem to be valid
func isValidNoDashIDChar(c byte) bool {
switch {
case c >= '0' && c <= '9':
return true
case c >= 'a' && c <= 'f':
return true
case c >= 'A' && c <= 'F':
// currently not used but just in case notion starts using them
return true
}
return false
}
func isValidDashIDChar(c byte) bool {
if c == '-' {
return true
}
return isValidNoDashIDChar(c)
}
// IsValidDashID returns true if id looks like a valid Notion dash id
func IsValidDashID(id string) bool {
if len(id) != dashIDLen {
return false
}
if id[8] != '-' ||
id[13] != '-' ||
id[18] != '-' ||
id[23] != '-' {
return false
}
for i := range id {
if !isValidDashIDChar(id[i]) {
return false
}
}
return true
}
// IsValidNoDashID returns true if id looks like a valid Notion no dash id
func IsValidNoDashID(id string) bool {
if len(id) != noDashIDLen {
return false
}
for i := range id {
if !isValidNoDashIDChar(id[i]) {
return false
}
}
return true
}
// ToNoDashID converts 2131b10c-ebf6-4938-a127-7089ff02dbe4
// to 2131b10cebf64938a1277089ff02dbe4.
// If not in expected format, we leave it untouched
func ToNoDashID(id string) string {
s := strings.Replace(id, "-", "", -1)
if IsValidNoDashID(s) {
return s
}
return ""
}
// ToDashID convert id in format bb760e2dd6794b64b2a903005b21870a
// to bb760e2d-d679-4b64-b2a9-03005b21870a
// If id is not in that format, we leave it untouched.
func ToDashID(id string) string {
if IsValidDashID(id) {
return id
}
s := strings.Replace(id, "-", "", -1)
if len(s) != noDashIDLen {
return id
}
res := id[:8] + "-" + id[8:12] + "-" + id[12:16] + "-" + id[16:20] + "-" + id[20:]
return res
}
func isIDEqual(id1, id2 string) bool {
id1 = ToNoDashID(id1)
id2 = ToNoDashID(id2)
return id1 == id2
}
func isSafeChar(r rune) bool {
if r >= '0' && r <= '9' {
return true
}
if r >= 'a' && r <= 'z' {
return true
}
if r >= 'A' && r <= 'Z' {
return true
}
return false
}
// SafeName returns a file-system safe name
func SafeName(s string) string {
var res string
for _, r := range s {
if !isSafeChar(r) {
res += "-"
} else {
res += string(r)
}
}
// replace multi-dash with single dash
for strings.Contains(res, "--") {
res = strings.Replace(res, "--", "-", -1)
}
res = strings.TrimLeft(res, "-")
res = strings.TrimRight(res, "-")
return res
}
// ErrPageNotFound is returned by Client.DownloadPage if page
// cannot be found
type ErrPageNotFound struct {
PageID string
}
func newErrPageNotFound(pageID string) *ErrPageNotFound {
return &ErrPageNotFound{
PageID: pageID,
}
}
// Error return error string
func (e *ErrPageNotFound) Error() string {
pageID := ToNoDashID(e.PageID)
return fmt.Sprintf("couldn't retrieve page '%s'", pageID)
}
// IsErrPageNotFound returns true if err is an instance of ErrPageNotFound
func IsErrPageNotFound(err error) bool {
_, ok := err.(*ErrPageNotFound)
return ok
}
func closeNoError(c io.Closer) {
_ = c.Close()
}
// log JSON after pretty printing it
func logJSON(client *Client, js []byte) {
pp := string(PrettyPrintJS(js))
client.vlogf("%s\n\n", pp)
// fmt.Printf("%s\n\n", pp)
}