Adding gob encode/decode for place, profile, question, relationship, tombstone types

This commit is contained in:
mariusor 2022-01-13 12:22:34 +01:00
parent 3e9850f7d0
commit af0f5aa0ee
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
9 changed files with 508 additions and 105 deletions

View file

@ -8,6 +8,31 @@ import (
"time"
)
func gobDecodeUint(i *uint, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(i)
}
func gobDecodeFloat64(f *float64, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(f)
}
func gobDecodeInt64(i *int64, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(i)
}
func gobDecodeString(s *string, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(s)
}
func gobDecodeBool(b *bool, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(b)
}
func unmapActorProperties(mm map[string][]byte, a *Actor) error {
err := OnObject(a, func(ob *Object) error {
return unmapObjectProperties(mm, ob)
@ -589,3 +614,123 @@ func unmapOrderedCollectionPageProperties(mm map[string][]byte, c *OrderedCollec
}
return err
}
func unmapPlaceProperties(mm map[string][]byte, p *Place) error {
err := OnObject(p, func(ob *Object) error {
return unmapObjectProperties(mm, ob)
})
if err != nil {
return err
}
if raw, ok := mm["accuracy"]; ok {
if err = gobDecodeFloat64(&p.Accuracy, raw); err != nil {
return err
}
}
if raw, ok := mm["altitude"]; ok {
if err = gobDecodeFloat64(&p.Altitude, raw); err != nil {
return err
}
}
if raw, ok := mm["latitude"]; ok {
if err = gobDecodeFloat64(&p.Latitude, raw); err != nil {
return err
}
}
if raw, ok := mm["radius"]; ok {
if err = gobDecodeInt64(&p.Radius, raw); err != nil {
return err
}
}
if raw, ok := mm["units"]; ok {
if err = gobDecodeString(&p.Units, raw); err != nil {
return err
}
}
return nil
}
func unmapProfileProperties(mm map[string][]byte, p *Profile) error {
err := OnObject(p, func(ob *Object) error {
return unmapObjectProperties(mm, ob)
})
if err != nil {
return err
}
if raw, ok := mm["Describes"]; ok {
if p.Describes, err = gobDecodeItem(raw); err != nil {
return err
}
}
return nil
}
func unmapRelationshipProperties(mm map[string][]byte, r *Relationship) error {
err := OnObject(r, func(ob *Object) error {
return unmapObjectProperties(mm, ob)
})
if err != nil {
return err
}
if raw, ok := mm["subject"]; ok {
if r.Subject, err = gobDecodeItem(raw); err != nil {
return err
}
}
if raw, ok := mm["object"]; ok {
if r.Object, err = gobDecodeItem(raw); err != nil {
return err
}
}
if raw, ok := mm["relationship"]; ok {
if r.Relationship, err = gobDecodeItem(raw); err != nil {
return err
}
}
return nil
}
func unmapTombstoneProperties(mm map[string][]byte, t *Tombstone) error {
err := OnObject(t, func(ob *Object) error {
return unmapObjectProperties(mm, ob)
})
if err != nil {
return err
}
if raw, ok := mm["formerType"]; ok {
if err = t.FormerType.GobDecode(raw); err != nil {
return err
}
}
if raw, ok := mm["deleted"]; ok {
if err = t.Deleted.GobDecode(raw); err != nil {
return err
}
}
return nil
}
func unmapQuestionProperties(mm map[string][]byte, q *Question) error {
err := OnIntransitiveActivity(q, func(act *IntransitiveActivity) error {
return unmapIntransitiveActivityProperties(mm, act)
})
if err != nil {
return err
}
if raw, ok := mm["oneOf"]; ok {
if q.OneOf, err = gobDecodeItem(raw); err != nil {
return err
}
}
if raw, ok := mm["anyOf"]; ok {
if q.AnyOf, err = gobDecodeItem(raw); err != nil {
return err
}
}
if raw, ok := mm["closed"]; ok {
if err = gobDecodeBool(&q.Closed, raw); err != nil {
return err
}
}
return nil
}

View file

@ -26,6 +26,40 @@ func gobEncodeUint(i uint) ([]byte, error) {
return b.Bytes(), nil
}
func gobEncodeFloat64(f float64) ([]byte, error) {
b := bytes.Buffer{}
gg := gob.NewEncoder(&b)
if err := gg.Encode(f); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func gobEncodeBool(t bool) ([]byte, error) {
b := bytes.Buffer{}
gg := gob.NewEncoder(&b)
if err := gg.Encode(t); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func gobEncodeBytes(s []byte) ([]byte, error) {
b := bytes.Buffer{}
gg := gob.NewEncoder(&b)
if err := gg.Encode(s); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func gobEncodeStringLikeType(g *gob.Encoder, s []byte) error {
if err := g.Encode(s); err != nil {
return err
}
return nil
}
func gobEncodeItems(col ItemCollection) ([]byte, error) {
b := bytes.Buffer{}
tt := make([][]byte, 0)
@ -533,3 +567,193 @@ func mapOrderedCollectionPageProperties(mm map[string][]byte, c OrderedCollectio
}
return
}
func mapLinkProperties(mm map[string][]byte, l Link) (hasData bool, err error) {
if len(l.ID) > 0 {
if mm["id"], err = l.ID.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.Type) > 0 {
if mm["type"], err = l.Type.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.MediaType) > 0 {
if mm["mediaType"], err = l.MediaType.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.Href) > 0 {
if mm["href"], err = l.Href.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.HrefLang) > 0 {
if mm["hrefLang"], err = l.HrefLang.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.Name) > 0 {
if mm["name"], err = l.Name.GobEncode(); err != nil {
return
}
hasData = true
}
if len(l.Rel) > 0 {
if mm["rel"], err = l.Rel.GobEncode(); err != nil {
return
}
hasData = true
}
if l.Width > 0 {
if mm["width"], err = gobEncodeUint(l.Width); err != nil {
return
}
hasData = true
}
if l.Height > 0 {
if mm["height"], err = gobEncodeUint(l.Height); err != nil {
return
}
hasData = true
}
return
}
func mapPlaceProperties(mm map[string][]byte, p Place) (hasData bool, err error) {
err = OnObject(p, func(o *Object) error {
hasData, err = mapObjectProperties(mm, o)
return err
})
if p.Accuracy > 0 {
if mm["accuracy"], err = gobEncodeFloat64(p.Accuracy); err != nil {
return
}
hasData = true
}
if p.Altitude > 0 {
if mm["altitude"], err = gobEncodeFloat64(p.Altitude); err != nil {
return
}
hasData = true
}
if p.Latitude > 0 {
if mm["latitude"], err = gobEncodeFloat64(p.Latitude); err != nil {
return
}
hasData = true
}
if p.Longitude > 0 {
if mm["longitude"], err = gobEncodeFloat64(p.Longitude); err != nil {
return
}
hasData = true
}
if p.Radius > 0 {
if mm["radius"], err = gobEncodeInt64(p.Radius); err != nil {
return
}
hasData = true
}
if len(p.Units) > 0 {
if mm["units"], err = gobEncodeBytes([]byte(p.Units)); err != nil {
return
}
hasData = true
}
return
}
func mapProfileProperties(mm map[string][]byte, p Profile) (hasData bool, err error) {
err = OnObject(p, func(o *Object) error {
hasData, err = mapObjectProperties(mm, o)
return err
})
if p.Describes != nil {
if mm["describes"], err = gobEncodeItem(p.Describes); err != nil {
return
}
hasData = true
}
return
}
func mapRelationshipProperties(mm map[string][]byte, r Relationship) (hasData bool, err error) {
err = OnObject(r, func(o *Object) error {
hasData, err = mapObjectProperties(mm, o)
return err
})
if r.Subject != nil {
if mm["subject"], err = gobEncodeItem(r.Subject); err != nil {
return
}
hasData = true
}
if r.Object != nil {
if mm["object"], err = gobEncodeItem(r.Object); err != nil {
return
}
hasData = true
}
if r.Relationship != nil {
if mm["relationship"], err = gobEncodeItem(r.Relationship); err != nil {
return
}
hasData = true
}
return
}
func mapTombstoneProperties(mm map[string][]byte, t Tombstone) (hasData bool, err error) {
err = OnObject(t, func(o *Object) error {
hasData, err = mapObjectProperties(mm, o)
return err
})
if len(t.FormerType) > 0 {
if mm["formerType"], err = t.FormerType.GobEncode(); err != nil {
return hasData, err
}
hasData = true
}
if !t.Deleted.IsZero() {
if mm["deleted"], err = t.Deleted.GobEncode(); err != nil {
return hasData, err
}
hasData = true
}
return
}
func mapQuestionProperties(mm map[string][]byte, q Question) (hasData bool, err error) {
err = OnObject(q, func(o *Object) error {
hasData, err = mapObjectProperties(mm, o)
return err
})
if q.OneOf != nil {
if mm["oneOf"], err = gobEncodeItem(q.OneOf); err != nil {
return
}
hasData = true
}
if q.AnyOf != nil {
if mm["anyOf"], err = gobEncodeItem(q.AnyOf); err != nil {
return
}
hasData = true
}
if q.Closed {
hasData = true
}
if hasData {
if mm["closed"], err = gobEncodeBool(q.Closed); err != nil {
return
}
}
return
}

67
link.go
View file

@ -125,64 +125,10 @@ func (l Link) MarshalBinary() ([]byte, error) {
}
func (l Link) GobEncode() ([]byte, error) {
var (
mm = make(map[string][]byte)
err error
hasData bool
)
if len(l.ID) > 0 {
if mm["id"], err = l.ID.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.Type) > 0 {
if mm["type"], err = l.Type.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.MediaType) > 0 {
if mm["mediaType"], err = l.MediaType.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.Href) > 0 {
if mm["href"], err = l.Href.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.HrefLang) > 0 {
if mm["hrefLang"], err = l.HrefLang.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.Name) > 0 {
if mm["name"], err = l.Name.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if len(l.Rel) > 0 {
if mm["rel"], err = l.Rel.GobEncode(); err != nil {
return nil, err
}
hasData = true
}
if l.Width > 0 {
if mm["width"], err = gobEncodeUint(l.Width); err != nil {
return nil, err
}
hasData = true
}
if l.Height > 0 {
if mm["height"], err = gobEncodeUint(l.Height); err != nil {
return nil, err
}
hasData = true
mm := make(map[string][]byte)
hasData, err := mapLinkProperties(mm, l)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
@ -195,11 +141,6 @@ func (l Link) GobEncode() ([]byte, error) {
return bb.Bytes(), nil
}
func gobDecodeUint(i *uint, data []byte) error {
g := gob.NewDecoder(bytes.NewReader(data))
return g.Decode(i)
}
func (l *Link) GobDecode(data []byte) error {
if len(data) == 0 {
return nil

View file

@ -196,13 +196,6 @@ func (l *LangRefValue) UnmarshalText(data []byte) error {
return nil
}
func gobEncodeStringLikeType(g *gob.Encoder, s []byte) error {
if err := g.Encode(s); err != nil {
return err
}
return nil
}
func (l LangRef) GobEncode() ([]byte, error) {
if len(l) == 0 {
return []byte{}, nil

View file

@ -1,6 +1,8 @@
package activitypub
import (
"bytes"
"encoding/gob"
"fmt"
"reflect"
"time"
@ -198,27 +200,45 @@ func (p Place) MarshalJSON() ([]byte, error) {
return nil, nil
}
/*
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (p *Place) UnmarshalBinary(data []byte) error {
return errors.New(fmt.Sprintf("UnmarshalBinary is not implemented for %T", *p))
return p.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (p Place) MarshalBinary() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("MarshalBinary is not implemented for %T", p))
return p.GobEncode()
}
// GobEncode
func (p Place) GobEncode() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("GobEncode is not implemented for %T", p))
mm := make(map[string][]byte)
hasData, err := mapPlaceProperties(mm, p)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
}
bb := bytes.Buffer{}
g := gob.NewEncoder(&bb)
if err := g.Encode(mm); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// GobDecode
func (p *Place) GobDecode([]byte) error {
return errors.New(fmt.Sprintf("GobDecode is not implemented for %T", *p))
func (p *Place) GobDecode(data []byte) error {
if len(data) == 0 {
return nil
}
mm, err := gobDecodeObjectAsMap(data)
if err != nil {
return err
}
return unmapPlaceProperties(mm, p)
}
*/
// Recipients performs recipient de-duplication on the Place object's To, Bto, CC and BCC properties
func (p *Place) Recipients() ItemCollection {

View file

@ -1,6 +1,8 @@
package activitypub
import (
"bytes"
"encoding/gob"
"fmt"
"reflect"
"time"
@ -170,27 +172,45 @@ func (p Profile) MarshalJSON() ([]byte, error) {
return nil, nil
}
/*
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (p *Profile) UnmarshalBinary(data []byte) error {
return errors.New(fmt.Sprintf("UnmarshalBinary is not implemented for %T", *p))
return p.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (p Profile) MarshalBinary() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("MarshalBinary is not implemented for %T", p))
return p.GobEncode()
}
// GobEncode
func (p Profile) GobEncode() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("GobEncode is not implemented for %T", p))
mm := make(map[string][]byte)
hasData, err := mapProfileProperties(mm, p)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
}
bb := bytes.Buffer{}
g := gob.NewEncoder(&bb)
if err := g.Encode(mm); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// GobDecode
func (p *Profile) GobDecode([]byte) error {
return errors.New(fmt.Sprintf("GobDecode is not implemented for %T", *p))
func (p *Profile) GobDecode(data []byte) error {
if len(data) == 0 {
return nil
}
mm, err := gobDecodeObjectAsMap(data)
if err != nil {
return err
}
return unmapProfileProperties(mm, p)
}
*/
// Recipients performs recipient de-duplication on the Profile object's To, Bto, CC and BCC properties
func (p *Profile) Recipients() ItemCollection {

View file

@ -1,6 +1,8 @@
package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"reflect"
"time"
@ -186,27 +188,45 @@ func (q Question) MarshalJSON() ([]byte, error) {
return b, nil
}
/*
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (q *Question) UnmarshalBinary(data []byte) error {
return errors.New(fmt.Sprintf("UnmarshalBinary is not implemented for %T", *q))
return q.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (q Question) MarshalBinary() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("MarshalBinary is not implemented for %T", q))
return q.GobEncode()
}
// GobEncode
func (q Question) GobEncode() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("GobEncode is not implemented for %T", q))
mm := make(map[string][]byte)
hasData, err := mapQuestionProperties(mm, q)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
}
bb := bytes.Buffer{}
g := gob.NewEncoder(&bb)
if err := g.Encode(mm); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// GobDecode
func (q *Question) GobDecode([]byte) error {
return errors.New(fmt.Sprintf("GobDecode is not implemented for %T", *q))
func (q *Question) GobDecode(data []byte) error {
if len(data) == 0 {
return nil
}
mm, err := gobDecodeObjectAsMap(data)
if err != nil {
return err
}
return unmapQuestionProperties(mm, q)
}
*/
// QuestionNew initializes a Question activity
func QuestionNew(id ID) *Question {

View file

@ -1,6 +1,8 @@
package activitypub
import (
"bytes"
"encoding/gob"
"fmt"
"reflect"
"time"
@ -187,27 +189,45 @@ func (r Relationship) MarshalJSON() ([]byte, error) {
return nil, nil
}
/*
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (r *Relationship) UnmarshalBinary(data []byte) error {
return errors.New(fmt.Sprintf("UnmarshalBinary is not implemented for %T", *r))
return r.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (r Relationship) MarshalBinary() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("MarshalBinary is not implemented for %T", r))
return r.GobEncode()
}
// GobEncode
func (r Relationship) GobEncode() ([]byte, error) {
return nil, errors.New(fmt.Sprintf("GobEncode is not implemented for %T", r))
mm := make(map[string][]byte)
hasData, err := mapRelationshipProperties(mm, r)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
}
bb := bytes.Buffer{}
g := gob.NewEncoder(&bb)
if err := g.Encode(mm); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// GobDecode
func (r *Relationship) GobDecode([]byte) error {
return errors.New(fmt.Sprintf("GobDecode is not implemented for %T", *r))
func (r *Relationship) GobDecode(data []byte) error {
if len(data) == 0 {
return nil
}
mm, err := gobDecodeObjectAsMap(data)
if err != nil {
return err
}
return unmapRelationshipProperties(mm, r)
}
*/
// Recipients performs recipient de-duplication on the Relationship object's To, Bto, CC and BCC properties
func (r *Relationship) Recipients() ItemCollection {

View file

@ -1,6 +1,8 @@
package activitypub
import (
"bytes"
"encoding/gob"
"fmt"
"reflect"
"time"
@ -176,27 +178,45 @@ func (t Tombstone) MarshalJSON() ([]byte, error) {
return nil, nil
}
/*
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (t *Tombstone) UnmarshalBinary(data []byte) error {
return fmt.Errorf("UnmarshalBinary is not implemented for %T", *t)
return t.GobDecode(data)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (t Tombstone) MarshalBinary() ([]byte, error) {
return nil, fmt.Errorf("MarshalBinary is not implemented for %T", t)
return t.GobEncode()
}
// GobEncode
func (t Tombstone) GobEncode() ([]byte, error) {
return nil, fmt.Errorf("GobEncode is not implemented for %T", t)
mm := make(map[string][]byte)
hasData, err := mapTombstoneProperties(mm, t)
if err != nil {
return nil, err
}
if !hasData {
return []byte{}, nil
}
bb := bytes.Buffer{}
g := gob.NewEncoder(&bb)
if err := g.Encode(mm); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// GobDecode
func (t *Tombstone) GobDecode([]byte) error {
return fmt.Errorf("GobDecode is not implemented for %T", *t)
func (t *Tombstone) GobDecode(data []byte) error {
if len(data) == 0 {
return nil
}
mm, err := gobDecodeObjectAsMap(data)
if err != nil {
return err
}
return unmapTombstoneProperties(mm, t)
}
*/
// Recipients performs recipient de-duplication on the Tombstone object's To, Bto, CC and BCC properties
func (t *Tombstone) Recipients() ItemCollection {