forked from go-ap/activitypub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
344 lines (327 loc) · 8.74 KB
/
encoding.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package activitypub
import (
"encoding/json"
"fmt"
"time"
)
func writeComma(b *[]byte) {
if len(*b) > 1 && (*b)[len(*b)-1] != ',' {
*b = append(*b, ',')
}
}
func writeProp(b *[]byte, name string, val []byte) (notEmpty bool) {
if len(val) == 0 {
return false
}
writeComma(b)
success := writePropName(b, name) && writeValue(b, val)
if !success {
*b = (*b)[:len(*b)-1]
}
return success
}
func write(b *[]byte, c ...byte) {
*b = append(*b, c...)
}
func writeS(b *[]byte, s string) {
*b = append(*b, s...)
}
func writePropName(b *[]byte, s string) (notEmpty bool) {
if len(s) == 0 {
return false
}
write(b, '"')
writeS(b, s)
write(b, '"', ':')
return true
}
func writeValue(b *[]byte, s []byte) (notEmpty bool) {
if len(s) == 0 {
return false
}
write(b, s...)
return true
}
func writeNaturalLanguageProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) {
l := nl.Count()
if l > 1 {
n += "Map"
}
if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 {
return writeProp(b, n, v)
}
return false
}
func writeStringProp(b *[]byte, n string, s string) (notEmpty bool) {
return writeProp(b, n, []byte(fmt.Sprintf(`"%s"`, s)))
}
func writeBoolProp(b *[]byte, n string, t bool) (notEmpty bool) {
return writeProp(b, n, []byte(fmt.Sprintf(`"%t"`, t)))
}
func writeIntProp(b *[]byte, n string, d int64) (notEmpty bool) {
return writeProp(b, n, []byte(fmt.Sprintf("%d", d)))
}
func writeFloatProp(b *[]byte, n string, f float64) (notEmpty bool) {
return writeProp(b, n, []byte(fmt.Sprintf("%f", f)))
}
func writeTimeProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
if v, err := t.UTC().MarshalJSON(); err == nil {
return writeProp(b, n, v)
}
return false
}
func writeDurationProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
if v, err := marshalXSD(d); err == nil {
return writeProp(b, n, v)
}
return false
}
func writeIRIProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
url := i.GetLink().String()
if len(url) == 0 {
return false
}
writeStringProp(b, n, url)
return true
}
func writeItemProp(b *[]byte, n string, i Item) (notEmpty bool) {
if i == nil {
return notEmpty
}
if im, ok := i.(json.Marshaler); ok {
v, err := im.MarshalJSON()
if err != nil {
return false
}
return writeProp(b, n, v)
}
return notEmpty
}
func writeStringValue(b *[]byte, s string) (notEmpty bool) {
if len(s) == 0 {
return false
}
write(b, '"')
writeS(b, s)
write(b, '"')
return true
}
func writeItemCollectionValue(b *[]byte, col ItemCollection) (notEmpty bool) {
if len(col) == 0 {
return notEmpty
}
writeCommaIfNotEmpty := func(notEmpty bool) {
if notEmpty {
write(b, ',')
}
}
write(b, '[')
for i, it := range col {
if im, ok := it.(json.Marshaler); ok {
v, err := im.MarshalJSON()
if err != nil {
return false
}
writeCommaIfNotEmpty(i > 0)
write(b, v...)
}
}
write(b, ']')
return true
}
func writeItemCollectionProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) {
if len(col) == 0 {
return notEmpty
}
writeComma(b)
success := writePropName(b, n) && writeItemCollectionValue(b, col)
if !success {
*b = (*b)[:len(*b)-1]
}
return success
}
func writeObjectValue(b *[]byte, o Object) (notEmpty bool) {
if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "id", v) || notEmpty
}
if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "type", v) || notEmpty
}
if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "mediaType", v) || notEmpty
}
if len(o.Name) > 0 {
notEmpty = writeNaturalLanguageProp(b, "name", o.Name) || notEmpty
}
if len(o.Summary) > 0 {
notEmpty = writeNaturalLanguageProp(b, "summary", o.Summary) || notEmpty
}
if len(o.Content) > 0 {
notEmpty = writeNaturalLanguageProp(b, "content", o.Content) || notEmpty
}
if o.Attachment != nil {
notEmpty = writeItemProp(b, "attachment", o.Attachment) || notEmpty
}
if o.AttributedTo != nil {
notEmpty = writeItemProp(b, "attributedTo", o.AttributedTo) || notEmpty
}
if o.Audience != nil {
notEmpty = writeItemProp(b, "audience", o.Audience) || notEmpty
}
if o.Context != nil {
notEmpty = writeItemProp(b, "context", o.Context) || notEmpty
}
if o.Generator != nil {
notEmpty = writeItemProp(b, "generator", o.Generator) || notEmpty
}
if o.Icon != nil {
notEmpty = writeItemProp(b, "icon", o.Icon) || notEmpty
}
if o.Image != nil {
notEmpty = writeItemProp(b, "image", o.Image) || notEmpty
}
if o.InReplyTo != nil {
notEmpty = writeItemProp(b, "inReplyTo", o.InReplyTo) || notEmpty
}
if o.Location != nil {
notEmpty = writeItemProp(b, "location", o.Location) || notEmpty
}
if o.Preview != nil {
notEmpty = writeItemProp(b, "preview", o.Preview) || notEmpty
}
if o.Replies != nil {
notEmpty = writeItemProp(b, "replies", o.Replies) || notEmpty
}
if o.Tag != nil {
notEmpty = writeItemProp(b, "tag", o.Tag) || notEmpty
}
if o.URL != nil {
notEmpty = writeIRIProp(b, "url", o.URL) || notEmpty
}
if o.To != nil {
notEmpty = writeItemProp(b, "to", o.To) || notEmpty
}
if o.Bto != nil {
notEmpty = writeItemProp(b, "bto", o.Bto) || notEmpty
}
if o.CC != nil {
notEmpty = writeItemProp(b, "cc", o.CC) || notEmpty
}
if o.BCC != nil {
notEmpty = writeItemProp(b, "bcc", o.BCC) || notEmpty
}
if !o.Published.IsZero() {
notEmpty = writeTimeProp(b, "published", o.Published) || notEmpty
}
if !o.Updated.IsZero() {
notEmpty = writeTimeProp(b, "updated", o.Updated) || notEmpty
}
if !o.StartTime.IsZero() {
notEmpty = writeTimeProp(b, "startTime", o.StartTime) || notEmpty
}
if !o.EndTime.IsZero() {
notEmpty = writeTimeProp(b, "endTime", o.EndTime) || notEmpty
}
if o.Duration != 0 {
// TODO(marius): maybe don't use 0 as a nil value for Object types
// which can have a valid duration of 0 - (Video, Audio, etc)
notEmpty = writeDurationProp(b, "duration", o.Duration) || notEmpty
}
if o.Likes != nil {
notEmpty = writeItemProp(b, "likes", o.Likes) || notEmpty
}
if o.Shares != nil {
notEmpty = writeItemProp(b, "shares", o.Shares) || notEmpty
}
if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "source", v) || notEmpty
}
return notEmpty
}
func writeActivityValue(b *[]byte, a Activity) (notEmpty bool) {
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
if i == nil {
return nil
}
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
return nil
})
if a.Object != nil {
notEmpty = writeItemProp(b, "object", a.Object) || notEmpty
}
return notEmpty
}
func writeIntransitiveActivityValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
OnObject(i, func(o *Object) error {
if o == nil {
return nil
}
notEmpty = writeObjectValue(b, *o) || notEmpty
return nil
})
if i.Actor != nil {
notEmpty = writeItemProp(b, "actor", i.Actor) || notEmpty
}
if i.Target != nil {
notEmpty = writeItemProp(b, "target", i.Target) || notEmpty
}
if i.Result != nil {
notEmpty = writeItemProp(b, "result", i.Result) || notEmpty
}
if i.Origin != nil {
notEmpty = writeItemProp(b, "origin", i.Origin) || notEmpty
}
if i.Instrument != nil {
notEmpty = writeItemProp(b, "instrument", i.Instrument) || notEmpty
}
return notEmpty
}
func writeQuestionValue(b *[]byte, q Question) (notEmpty bool) {
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
if i == nil {
return nil
}
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
return nil
})
if q.OneOf != nil {
notEmpty = writeItemProp(b, "oneOf", q.OneOf) || notEmpty
} else if q.AnyOf != nil {
notEmpty = writeItemProp(b, "anyOf", q.OneOf) || notEmpty
}
notEmpty = writeBoolProp(b, "closed", q.Closed) || notEmpty
return notEmpty
}
func writeLinkValue(b *[]byte, l Link) (notEmpty bool) {
if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "id", v) || notEmpty
}
if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "type", v) || notEmpty
}
if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "mediaType", v) || notEmpty
}
if len(l.Name) > 0 {
notEmpty = writeNaturalLanguageProp(b, "name", l.Name) || notEmpty
}
if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "rel", v) || notEmpty
}
if l.Height > 0 {
notEmpty = writeIntProp(b, "height", int64(l.Height))
}
if l.Width > 0 {
notEmpty = writeIntProp(b, "width", int64(l.Width))
}
if l.Preview != nil {
notEmpty = writeItemProp(b, "rel", l.Preview) || notEmpty
}
if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeProp(b, "href", v) || notEmpty
}
if len(l.HrefLang) > 0 {
notEmpty = writeStringProp(b, "hrefLang", string(l.HrefLang)) || notEmpty
}
return notEmpty
}