diff --git a/encoding_gob.go b/encoding_gob.go index 89a644f..e5dfd54 100644 --- a/encoding_gob.go +++ b/encoding_gob.go @@ -68,6 +68,19 @@ func gobEncodeItems(col ItemCollection) ([]byte, error) { return b.Bytes(), err } +func gobEncodeItemOrLink(it LinkOrIRI) ([]byte, error) { + if ob, ok := it.(Item); ok { + return gobEncodeItem(ob) + } + b := bytes.Buffer{} + err := OnLink(it, func(l *Link) error { + bytes, err := l.GobEncode() + b.Write(bytes) + return err + }) + return b.Bytes(), err +} + func gobEncodeItem(it Item) ([]byte, error) { if IsIRI(it) { if i, ok := it.(IRI); ok { @@ -97,6 +110,7 @@ func gobEncodeItem(it Item) ([]byte, error) { return err }) case LinkType, MentionType: + // TODO(marius): this shouldn't work, as Link does not implement Item? (or rather, should not) err = OnLink(it, func(l *Link) error { bytes, err := l.GobEncode() b.Write(bytes) @@ -333,7 +347,7 @@ func mapObjectProperties(mm map[string][]byte, o *Object) (hasData bool, err err hasData = true } if o.URL != nil { - if mm["url"], err = gobEncodeItem(o.URL); err != nil { + if mm["url"], err = gobEncodeItemOrLink(o.URL); err != nil { return hasData, err } hasData = true diff --git a/helpers.go b/helpers.go index 4c92072..75d2575 100644 --- a/helpers.go +++ b/helpers.go @@ -41,7 +41,7 @@ type WithOrderedCollectionPageFn func(*OrderedCollectionPage) error type WithItemCollectionFn func(*ItemCollection) error // OnLink calls function fn on it Item if it can be asserted to type Link -func OnLink(it Item, fn WithLinkFn) error { +func OnLink(it LinkOrIRI, fn WithLinkFn) error { if it == nil { return nil } diff --git a/object.go b/object.go index 395ae9f..dbf8401 100644 --- a/object.go +++ b/object.go @@ -135,10 +135,10 @@ func (a ActivityVocabularyType) MarshalBinary() ([]byte, error) { type Objects interface { Object | Tombstone | Place | Profile | Relationship | - Activities | - IntransitiveActivities | - Collections | - IRI + Activities | + IntransitiveActivities | + Collections | + IRI } // Object describes an ActivityPub object of any kind. @@ -427,14 +427,14 @@ func (m MimeType) MarshalBinary() ([]byte, error) { } // ToLink returns a Link pointer to the data in the current Item -func ToLink(it Item) (*Link, error) { +func ToLink(it LinkOrIRI) (*Link, error) { switch i := it.(type) { case *Link: return i, nil case Link: return &i, nil } - return nil, fmt.Errorf("unable to convert %q", it.GetType()) + return nil, fmt.Errorf("unable to convert %T", it) } // ToObject returns an Object pointer to the data in the current Item