Added gob Marshal/Unmarshal for ActivityVocabularyType type

This commit is contained in:
mariusor 2021-12-30 16:22:15 +01:00
parent 2f50a0196d
commit a7d54ee31e
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
2 changed files with 92 additions and 22 deletions

View file

@ -111,36 +111,43 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
return b, nil
}
/*
// GobEncode
func (a ActivityVocabularyType) GobEncode() ([]byte, error) {
if len(a) == 0 {
return []byte{}, nil
}
b := bytes.Buffer{}
gg := gob.NewEncoder(&b)
if err := encodeGobStringLikeType(gg, []byte(a)); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// GobDecode
func (a *ActivityVocabularyType) GobDecode(data []byte) error {
if len(data) == 0 {
// NOTE(marius): this behaviour diverges from vanilla gob package
return nil
}
var bb string
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&bb); err != nil {
return err
}
*a = ActivityVocabularyType(bb)
return nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (a *ActivityVocabularyType) UnmarshalBinary(data []byte) error {
return errors.New(fmt.Sprintf("UnmarshalBinary is not implemented for %T", *a))
return a.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (a ActivityVocabularyType) MarshalBinary() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("MarshalBinary is not implemented for %T", a))
return a.GobEncode()
}
// GobEncode
func (a ActivityVocabularyType) GobEncode() ([]byte, error) {
if len(a) == 0 {
return nil, nil
}
w := &bytes.Buffer{}
enc := gobEncoder{ w: w, enc: gob.NewEncoder(w) }
if err := enc.writeS(string(a)); err != nil {
return nil, err
}
return w.Bytes(), nil
}
// GobDecode
func (a *ActivityVocabularyType) GobDecode([]byte) error {
return errors.New(fmt.Sprintf("GobDecode is not implemented for %T", *a))
}
*/
// Object describes an ActivityPub object of any kind.
// It serves as the base type for most of the other kinds of objects defined in the Activity
// Vocabulary, including other Core types such as Activity, IntransitiveActivity, Collection and OrderedCollection.

View file

@ -560,6 +560,69 @@ func TestActivityVocabularyType_MarshalJSON(t *testing.T) {
t.Skip("TODO")
}
func TestActivityVocabularyType_GobDecode(t *testing.T) {
tests := []struct {
name string
t ActivityVocabularyType
data []byte
wantErr bool
}{
{
name: "empty",
t: "",
data: []byte{},
wantErr: false,
},
{
name: "some activity type",
t: PersonType,
data: gobValue("Person"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.t.GobDecode(tt.data); (err != nil) != tt.wantErr {
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestActivityVocabularyType_GobEncode(t *testing.T) {
tests := []struct {
name string
t ActivityVocabularyType
want []byte
wantErr bool
}{
{
name: "empty",
t: "",
want: []byte{},
wantErr: false,
},
{
name: "some activity type",
t: ActivityType,
want: gobValue([]byte("Activity")),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.t.GobEncode()
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 TestObject_MarshalJSON(t *testing.T) {
type fields struct {
ID ID