Add IsCollection function to all base objects
This commit is contained in:
parent
d82e449e14
commit
8982b3f40a
15
activity.go
15
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
5
iri.go
5
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 {
|
||||
|
|
13
item.go
13
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
|
||||
}
|
||||
|
|
10
link.go
10
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()
|
||||
|
|
12
object.go
12
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 {
|
||||
|
|
Reference in a new issue