Changes
4 changed files (+323/-0)
-
decoding_gob.go (new)
-
@@ -0,0 +1,44 @@package activitypub import ( "bytes" "encoding/gob" ) func GobEncode(it Item) ([]byte, error) { b := new(bytes.Buffer) err := gob.NewEncoder(b).Encode(it) return b.Bytes(), err } type hasType struct { Type ActivityVocabularyType } func GobUnmarshalToItem(data []byte) Item { if len(data) == 0 { return nil } if ItemTyperFunc == nil { return nil } typer := new(hasType) err := gob.NewDecoder(bytes.NewReader(data)).Decode(typer) if err != nil { return nil } var it Item switch typer.Type { } return it } // UnmarshalGob func UnmarshalGob(data []byte) (Item, error) { if ItemTyperFunc == nil { ItemTyperFunc = GetItemByType } return GobUnmarshalToItem(data), nil }
-
-
decoding_gob_test.go (new)
-
@@ -0,0 +1,63 @@package activitypub import ( "reflect" "testing" ) func TestGobEncode(t *testing.T) { type args struct { it Item } tests := []struct { name string args args want []byte wantErr bool }{ // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GobEncode(tt.args.it) if (err != nil) != tt.wantErr { t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GobEncode() got = %v, want %v", got, tt.want) } }) } } func TestUnmarshalGob(t *testing.T) { type args struct { data []byte } tests := []struct { name string args args want Item wantErr bool }{ { name: "empty", args: args{}, want: nil, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := UnmarshalGob(tt.args.data) if (err != nil) != tt.wantErr { t.Errorf("UnmarshalGob() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("UnmarshalGob() = %v, want %v", got, tt.want) } }) } }
-
-
encoding_gob.go (new)
-
@@ -0,0 +1,168 @@package activitypub import ( "bytes" "encoding/gob" "errors" "fmt" "io" "time" ) type gobEncoder struct { 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) writeIRIProp(i IRI) (bool, error) { if len(i) == 0 { return true, nil } return false, e.enc.Encode(i) } func (e *gobEncoder)writeGobProp (p string, b []byte) bool { return true } 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 writeIRIGobProp(buf io.Writer, i IRI) (int, error) { return 0, errors.New(fmt.Sprintf("writeIRIGobProp is not implemented for %T", i)) } 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 }
-
-
encoding_gob_test.go (new)
-
@@ -0,0 +1,48 @@package activitypub import ( "bytes" "encoding/gob" "reflect" "testing" ) func TestMarshalGob(t *testing.T) { tests := []struct { name string it Item want []byte wantErr bool }{ { name: "empty object", it: &Object{ ID: "test", }, want: []byte{}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { //got, err := MarshalGob(tt.it) buf := bytes.NewBuffer(make([]byte,0)) err := gob.NewEncoder(buf).Encode(tt.it) got := buf.Bytes() it := new(Object) gob.NewDecoder(bytes.NewReader(got)).Decode(it) if it.ID == tt.it.GetID() { t.Logf("Yay!") } if (err != nil) != tt.wantErr { t.Errorf("MarshalGob() error = %s, wantErr %v", err, tt.wantErr) return } if !tt.wantErr && !reflect.DeepEqual(got, tt.want) { t.Errorf("MarshalGob() got = %#v, want %#v", got, tt.want) } }) } }
-