-
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
package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"reflect"
"time"
)
type typeId int32
type gobEncoder struct {
sent map[reflect.Type]typeId // which types we've already sent
w *bytes.Buffer
enc *gob.Encoder
}
func (e *gobEncoder) encode (it Item) ([]byte, error) {
err := e.enc.Encode(it)
if err != nil {
return nil, err
}
return e.w.Bytes(), nil
}
//// GobEncode
//func GobEncode(it Item) ([]byte, error) {
// w := bytes.NewBuffer(make([]byte, 0))
// enc := gobEncoder{
// w: w,
// enc: gob.NewEncoder(w),
// }
// return enc.encode(it)
//}
func (e *gobEncoder) writeS(s string) error {
return e.enc.Encode(s)
}
func (e *gobEncoder) writeIRIProp(i IRI) error {
return e.enc.Encode(i.String())
}
func (e *gobEncoder) writeGobProp (p string, b []byte) bool {
c, _ := e.w.Write([]byte(p))
d, _ := e.w.Write(b)
return c+d > 0
}
func (e *gobEncoder)writeItemGobProp (p string, it Item) bool {
return true
}
func (e *gobEncoder)writeNaturalLanguageGobProp (p string, v NaturalLanguageValues) bool {
return true
}
func (e *gobEncoder)writeIRIGobProp (p string, i LinkOrIRI) bool {
return true
}
func (e *gobEncoder)writeTimeGobProp (p string, t time.Time) bool {
return true
}
func (e *gobEncoder)writeDurationGobProp (p string, d time.Duration) bool {
return true
}
func writeObjectGobValue(buf io.Writer, o *Object) (int, error) {
return 0, errors.New(fmt.Sprintf("writeObjectGobValue is not implemented for %T", *o))
}
/*
func (e *gobEncoder) writeObjectGobValue(o Object) (bool, error) {
notEmpty := true
if v, err := o.ID.GobEncode(); err == nil && len(v) > 0 {
notEmpty = e.writeGobProp("id", v) || notEmpty
}
if v, err := o.Type.GobEncode(); err == nil && len(v) > 0 {
notEmpty = e.writeGobProp("type", v) || notEmpty
}
if v, err := o.MediaType.GobEncode(); err == nil && len(v) > 0 {
notEmpty = e.writeGobProp("mediaType", v) || notEmpty
}
if len(o.Name) > 0 {
notEmpty = e.writeNaturalLanguageGobProp("name", o.Name) || notEmpty
}
if len(o.Summary) > 0 {
notEmpty = e.writeNaturalLanguageGobProp("summary", o.Summary) || notEmpty
}
if len(o.Content) > 0 {
notEmpty = e.writeNaturalLanguageGobProp("content", o.Content) || notEmpty
}
if o.Attachment != nil {
notEmpty = e.writeItemGobProp("attachment", o.Attachment) || notEmpty
}
if o.AttributedTo != nil {
notEmpty = e.writeItemGobProp("attributedTo", o.AttributedTo) || notEmpty
}
if o.Audience != nil {
notEmpty = e.writeItemGobProp("audience", o.Audience) || notEmpty
}
if o.Context != nil {
notEmpty = e.writeItemGobProp("context", o.Context) || notEmpty
}
if o.Generator != nil {
notEmpty = e.writeItemGobProp("generator", o.Generator) || notEmpty
}
if o.Icon != nil {
notEmpty = e.writeItemGobProp("icon", o.Icon) || notEmpty
}
if o.Image != nil {
notEmpty = e.writeItemGobProp("image", o.Image) || notEmpty
}
if o.InReplyTo != nil {
notEmpty = e.writeItemGobProp("inReplyTo", o.InReplyTo) || notEmpty
}
if o.Location != nil {
notEmpty = e.writeItemGobProp("location", o.Location) || notEmpty
}
if o.Preview != nil {
notEmpty = e.writeItemGobProp("preview", o.Preview) || notEmpty
}
if o.Replies != nil {
notEmpty = e.writeItemGobProp("replies", o.Replies) || notEmpty
}
if o.Tag != nil {
notEmpty = e.writeItemGobProp("tag", o.Tag) || notEmpty
}
if o.URL != nil {
notEmpty = e.writeIRIGobProp("url", o.URL) || notEmpty
}
if o.To != nil {
notEmpty = e.writeItemGobProp("to", o.To) || notEmpty
}
if o.Bto != nil {
notEmpty = e.writeItemGobProp("bto", o.Bto) || notEmpty
}
if o.CC != nil {
notEmpty = e.writeItemGobProp("cc", o.CC) || notEmpty
}
if o.BCC != nil {
notEmpty = e.writeItemGobProp("bcc", o.BCC) || notEmpty
}
if !o.Published.IsZero() {
notEmpty = e.writeTimeGobProp("published", o.Published) || notEmpty
}
if !o.Updated.IsZero() {
notEmpty = e.writeTimeGobProp("updated", o.Updated) || notEmpty
}
if !o.StartTime.IsZero() {
notEmpty = e.writeTimeGobProp("startTime", o.StartTime) || notEmpty
}
if !o.EndTime.IsZero() {
notEmpty = e.writeTimeGobProp("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 = e.writeDurationGobProp("duration", o.Duration) || notEmpty
}
if o.Likes != nil {
notEmpty = e.writeItemGobProp("likes", o.Likes) || notEmpty
}
if o.Shares != nil {
notEmpty = e.writeItemGobProp("shares", o.Shares) || notEmpty
}
if v, err := o.Source.GobEncode(); err == nil && len(v) > 0 {
notEmpty = e.writeGobProp("source", v) || notEmpty
}
return notEmpty, nil
}
*/