From 8982b3f40a46b5d4b9f700b4faf5ec1bcd825b40 Mon Sep 17 00:00:00 2001 From: Marius Orcsik Date: Sun, 1 Dec 2019 19:27:45 +0100 Subject: [PATCH] Add IsCollection function to all base objects --- activity.go | 15 +++++++++++++ collections.go | 51 +++++++++++++++++++++++++++++++++++++-------- collections_test.go | 18 ++++++++-------- iri.go | 5 +++++ item.go | 13 ++++++++---- link.go | 10 +++++++++ object.go | 12 ++++++++--- 7 files changed, 99 insertions(+), 25 deletions(-) diff --git a/activity.go b/activity.go index 1a4b15f..815dbde 100644 --- a/activity.go +++ b/activity.go @@ -821,6 +821,11 @@ func (i IntransitiveActivity) IsObject() bool { return true } +// IsCollection returns false for IntransitiveActivity objects +func (i IntransitiveActivity) IsCollection() bool { + return false +} + // GetType returns the ActivityVocabulary type of the current Activity func (a Activity) GetType() ActivityVocabularyType { return a.Type @@ -846,6 +851,11 @@ func (a Activity) IsObject() bool { return true } +// IsCollection returns false for Activity objects +func (a Activity) IsCollection() bool { + return false +} + // GetID returns the ObjectID corresponding to the Like object func (l Like) GetID() *ObjectID { return Activity(l).GetID() @@ -1316,6 +1326,11 @@ func (q Question) IsObject() bool { return true } +// IsCollection returns false for Question objects +func (q Question) IsCollection() bool { + return false +} + // IsLink returns false for Question objects func (q Question) IsLink() bool { return false diff --git a/collections.go b/collections.go index 4ab2551..7a43062 100644 --- a/collections.go +++ b/collections.go @@ -5,7 +5,10 @@ import ( "github.com/buger/jsonparser" ) +const CollectionOfItems ActivityVocabularyType = "ItemCollection" + var CollectionTypes = ActivityVocabularyTypes{ + CollectionOfItems, CollectionType, OrderedCollectionType, CollectionPageType, @@ -14,7 +17,7 @@ var CollectionTypes = ActivityVocabularyTypes{ type CollectionInterface interface { ObjectOrLink - Collection() CollectionInterface + Collection() ItemCollection Append(ob Item) error Count() uint Contains(IRI) bool @@ -299,25 +302,44 @@ func (o *OrderedCollection) MarshalJSON() ([]byte, error) { */ // Collection returns the underlying Collection type -func (c *Collection) Collection() CollectionInterface { - return c +func (c *Collection) Collection() ItemCollection { + return c.Items +} + +// IsCollection returns true for Collection objects +func (c Collection) IsCollection() bool { + return true } // Collection returns the underlying Collection type -func (o *OrderedCollection) Collection() CollectionInterface { - return o +func (o *OrderedCollection) Collection() ItemCollection { + return o.OrderedItems +} + +// IsCollection returns true for OrderedCollection objects +func (o OrderedCollection) IsCollection() bool { + return true } // Collection returns the underlying Collection type -func (c *CollectionPage) Collection() CollectionInterface { - return c +func (c *CollectionPage) Collection() ItemCollection { + return c.Items +} + +// IsCollection returns true for CollectionPage objects +func (c CollectionPage) IsCollection() bool { + return true } // Collection returns the underlying Collection type -func (o *OrderedCollectionPage) Collection() CollectionInterface { - return o +func (o *OrderedCollectionPage) Collection() ItemCollection { + return o.OrderedItems } +// IsCollection returns true for OrderedCollectionPage objects +func (o OrderedCollectionPage) IsCollection() bool { + return true +} // FlattenItemCollection flattens the Collection's properties from Object type to IRI func FlattenItemCollection(c ItemCollection) ItemCollection { if c != nil && len(c) > 0 { @@ -328,6 +350,17 @@ func FlattenItemCollection(c ItemCollection) ItemCollection { return c } +// ToItemCollection +func ToItemCollection(it Item) (*ItemCollection, error) { + switch i := it.(type) { + case *ItemCollection: + return i, nil + case ItemCollection: + return &i, nil + } + return nil, errors.New("unable to convert to item collection") +} + // ToCollection func ToCollection(it Item) (*Collection, error) { switch i := it.(type) { diff --git a/collections_test.go b/collections_test.go index a13f49b..49fc46c 100644 --- a/collections_test.go +++ b/collections_test.go @@ -115,8 +115,8 @@ func TestCollection_Collection(t *testing.T) { c := CollectionNew(id) - if c.Collection() != c { - t.Errorf("Collection should return itself %q", *c.GetID()) + if !reflect.DeepEqual(c.Collection(), c.Items) { + t.Errorf("Collection items should be equal %v %v", c.Collection(), c.Items) } } @@ -299,10 +299,10 @@ func TestOrderedCollection_Append(t *testing.T) { func TestOrderedCollection_Collection(t *testing.T) { id := ObjectID("test") - c := OrderedCollectionNew(id) + o := OrderedCollectionNew(id) - if c.Collection() != c { - t.Errorf("Collection should return itself %q", *c.GetID()) + if !reflect.DeepEqual(o.Collection(), o.OrderedItems) { + t.Errorf("Collection items should be equal %v %v", o.Collection(), o.OrderedItems) } } @@ -488,8 +488,8 @@ func TestOrderedCollectionPage_Collection(t *testing.T) { c := OrderedCollectionNew(id) p := OrderedCollectionPageNew(c) - if p.Collection() != p { - t.Errorf("Collection should return itself %q", *p.GetID()) + if !reflect.DeepEqual(p.Collection(), p.OrderedItems) { + t.Errorf("Collection items should be equal %v %v", p.Collection(), p.OrderedItems) } } @@ -499,8 +499,8 @@ func TestCollectionPage_Collection(t *testing.T) { c := CollectionNew(id) p := CollectionPageNew(c) - if p.Collection() != p { - t.Errorf("Collection should return itself %q", *p.GetID()) + if !reflect.DeepEqual(p.Collection(), p.Items) { + t.Errorf("Collection items should be equal %v %v", p.Collection(), p.Items) } } diff --git a/iri.go b/iri.go index 95bcf8f..94b252e 100644 --- a/iri.go +++ b/iri.go @@ -56,6 +56,11 @@ func (i IRI) IsObject() bool { return false } +// IsCollection returns false for IRI objects +func (i IRI) IsCollection() bool { + return false +} + // FlattenToIRI checks if Item can be flatten to an IRI and returns it if so func FlattenToIRI(it Item) Item { if it != nil && it.IsObject() && len(it.GetLink()) > 0 { diff --git a/item.go b/item.go index a6ebbdc..cf2d6a8 100644 --- a/item.go +++ b/item.go @@ -18,7 +18,7 @@ func (i ItemCollection) GetLink() IRI { // GetType returns the ItemCollection's type func (i ItemCollection) GetType() ActivityVocabularyType { - return i.First().GetType() + return CollectionOfItems } // IsLink returns false for an ItemCollection object @@ -58,12 +58,17 @@ func (i ItemCollection) First() Item { } // Collection returns the current object as collection interface -func (i *ItemCollection) Collection() CollectionInterface { - return i +func (i *ItemCollection) Collection() ItemCollection { + return *i +} + +// IsCollection returns true for ItemCollection arrays +func (i ItemCollection) IsCollection() bool { + return true } // Contains verifies if IRIs array contains the received one -func(i ItemCollection) Contains(r IRI) bool { +func (i ItemCollection) Contains(r IRI) bool { if len(i) == 0 { return false } diff --git a/link.go b/link.go index e2d70be..699fd7d 100644 --- a/link.go +++ b/link.go @@ -74,6 +74,11 @@ func (l Link) IsObject() bool { return l.Type == ObjectType || ObjectTypes.Contains(l.Type) } +// IsCollection returns false for Link objects +func (l Link) IsCollection() bool { + return false +} + // GetID returns the ObjectID corresponding to the Link object func (l Link) GetID() *ObjectID { return &l.ID @@ -99,6 +104,11 @@ func (m Mention) IsObject() bool { return m.Type == ObjectType || ObjectTypes.Contains(m.Type) } +// IsCollection returns false for Mention objects +func (m Mention) IsCollection() bool { + return false +} + // GetID returns the ObjectID corresponding to the Mention object func (m Mention) GetID() *ObjectID { return Link(m).GetID() diff --git a/object.go b/object.go index 631098b..bd5d128 100644 --- a/object.go +++ b/object.go @@ -111,11 +111,12 @@ type ( LinkOrURI // GetType returns the ActivityStreams type GetType() ActivityVocabularyType - // IsLink shows if current object represents a link object or an IRI + // IsLink shows if current item represents a Link object or an IRI IsLink() bool - // IsObject shows if current object represents an ActivityStrems object + // IsObject shows if current item represents an ActivityStreams object IsObject() bool - //UnmarshalJSON([]byte) error + // IsCollection shows if the current item represents an ItemCollection + IsCollection()bool } // Mapper interface allows external objects to implement their own mechanism for loading information // from an ActivityStreams vocabulary object @@ -193,6 +194,11 @@ func (o Object) IsObject() bool { return true } +// IsCollection returns false for Object objects +func (o Object) IsCollection() bool { + return false +} + // MarshalJSON serializes the NaturalLanguageValues into JSON func (n NaturalLanguageValues) MarshalJSON() ([]byte, error) { if len(n) == 0 {