Restructured the Collection,CollectionPage,OrderedCollection,OrderedCollectionPage types to be stand-alone

Moved functionality to their own files
This commit is contained in:
Marius Orcsik 2019-12-03 16:05:39 +01:00
parent fa6ec9411c
commit 1fcca5958e
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
11 changed files with 1476 additions and 834 deletions

271
collection_page.go Normal file
View file

@ -0,0 +1,271 @@
package activitystreams
import (
"errors"
"time"
)
// CollectionPage is a Collection that contains a large number of items and when it becomes impractical
// for an implementation to serialize every item contained by a Collection using the items
// property alone. In such cases, the items within a Collection can be divided into distinct subsets or "pages".
type CollectionPage struct {
// ID provides the globally unique identifier for anActivity Pub Object or Link.
ID ObjectID `jsonld:"id,omitempty"`
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
Type ActivityVocabularyType `jsonld:"type,omitempty"`
// Name a simple, human-readable, plain-text name for the object.
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
// The intent is to provide a model that is at least semantically similar to attachments in email.
Attachment Item `jsonld:"attachment,omitempty"`
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
// For instance, an object might be attributed to the completion of another activity.
AttributedTo Item `jsonld:"attributedTo,omitempty"`
// Audience identifies one or more entities that represent the total population of entities
// for which the object can considered to be relevant.
Audience ItemCollection `jsonld:"audience,omitempty"`
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
// By default, the value of content is HTML.
// The mediaType property can be used in the object to indicate a different content type.
// (The content MAY be expressed using multiple language-tagged values.)
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
// Context identifies the context within which the object exists or an activity was performed.
// The notion of "context" used is intentionally vague.
// The intended function is to serve as a means of grouping objects and activities that share a
// common originating context or purpose. An example could be all activities relating to a common project or event.
Context Item `jsonld:"context,omitempty"`
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
// If not specified, the content property is assumed to contain text/html content.
MediaType MimeType `jsonld:"mediaType,omitempty"`
// EndTime the date and time describing the actual or expected ending time of the object.
// When used with an Activity object, for instance, the endTime property specifies the moment
// the activity concluded or is expected to conclude.
EndTime time.Time `jsonld:"endTime,omitempty"`
// Generator identifies the entity (e.g. an application) that generated the object.
Generator Item `jsonld:"generator,omitempty"`
// Icon indicates an entity that describes an icon for this object.
// The image should have an aspect ratio of one (horizontal) to one (vertical)
// and should be suitable for presentation at a small size.
Icon Item `jsonld:"icon,omitempty"`
// Image indicates an entity that describes an image for this object.
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
Image Item `jsonld:"image,omitempty"`
// InReplyTo indicates one or more entities for which this object is considered a response.
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
// Location indicates one or more physical or logical locations associated with the object.
Location Item `jsonld:"location,omitempty"`
// Preview identifies an entity that provides a preview of this object.
Preview Item `jsonld:"preview,omitempty"`
// Published the date and time at which the object was published
Published time.Time `jsonld:"published,omitempty"`
// Replies identifies a Collection containing objects considered to be responses to this object.
Replies Item `jsonld:"replies,omitempty"`
// StartTime the date and time describing the actual or expected starting time of the object.
// When used with an Activity object, for instance, the startTime property specifies
// the moment the activity began or is scheduled to begin.
StartTime time.Time `jsonld:"startTime,omitempty"`
// Summary a natural language summarization of the object encoded as HTML.
// *Multiple language tagged summaries may be provided.)
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
// The key difference between attachment and tag is that the former implies association by inclusion,
// while the latter implies associated by reference.
Tag ItemCollection `jsonld:"tag,omitempty"`
// Updated the date and time at which the object was updated
Updated time.Time `jsonld:"updated,omitempty"`
// URL identifies one or more links to representations of the object
URL LinkOrIRI `jsonld:"url,omitempty"`
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
To ItemCollection `jsonld:"to,omitempty"`
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
Bto ItemCollection `jsonld:"bto,omitempty"`
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
CC ItemCollection `jsonld:"cc,omitempty"`
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
BCC ItemCollection `jsonld:"bcc,omitempty"`
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
// the duration property indicates the object's approximate duration.
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
Duration time.Duration `jsonld:"duration,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current ObjectOrLink `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
First ObjectOrLink `jsonld:"first,omitempty"`
// In a paged Collection, indicates the furthest proceeding page of the collection.
Last ObjectOrLink `jsonld:"last,omitempty"`
// A non-negative integer specifying the total number of objects contained by the logical view of the collection.
// This number might not reflect the actual number of items serialized within the Collection object instance.
TotalItems uint `jsonld:"totalItems"`
// Identifies the items contained in a collection. The items might be unordered.
Items ItemCollection `jsonld:"items,omitempty"`
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the next page of items.
Next Item `jsonld:"next,omitempty"`
// In a paged Collection, identifies the previous page of items.
Prev Item `jsonld:"prev,omitempty"`
}
// GetID returns the ObjectID corresponding to the CollectionPage object
func (c CollectionPage) GetID() *ObjectID {
return &c.ID
}
// GetType returns the CollectionPage's type
func (c CollectionPage) GetType() ActivityVocabularyType {
return c.Type
}
// IsLink returns false for a CollectionPage object
func (c CollectionPage) IsLink() bool {
return false
}
// IsObject returns true for a CollectionPage object
func (c CollectionPage) IsObject() bool {
return true
}
// IsCollection returns true for CollectionPage objects
func (c CollectionPage) IsCollection() bool {
return true
}
// GetLink returns the IRI corresponding to the CollectionPage object
func (c CollectionPage) GetLink() IRI {
return IRI(c.ID)
}
// Collection returns the ColleCollectionPagection items
func (c CollectionPage) Collection() ItemCollection {
return c.Items
}
// Count returns the maximum between the length of Items in the collection page and its TotalItems property
func (c *CollectionPage) Count() uint {
if c.TotalItems > 0 {
return c.TotalItems
}
return uint(len(c.Items))
}
// Append adds an element to a CollectionPage
func (c *CollectionPage) Append(ob Item) error {
c.Items = append(c.Items, ob)
return nil
}
// Contains verifies if CollectionPage array contains the received one
func (c CollectionPage) Contains(r IRI) bool {
if len(c.Items) == 0 {
return false
}
for _, iri := range c.Items {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// UnmarshalJSON
func (c *CollectionPage) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
c.ID = JSONGetObjectID(data)
c.Type = JSONGetType(data)
c.Name = JSONGetNaturalLanguageField(data, "name")
c.Content = JSONGetNaturalLanguageField(data, "content")
c.Summary = JSONGetNaturalLanguageField(data, "summary")
c.Context = JSONGetItem(data, "context")
c.URL = JSONGetURIItem(data, "url")
c.MediaType = MimeType(JSONGetString(data, "mediaType"))
c.Generator = JSONGetItem(data, "generator")
c.AttributedTo = JSONGetItem(data, "attributedTo")
c.Attachment = JSONGetItem(data, "attachment")
c.Location = JSONGetItem(data, "location")
c.Published = JSONGetTime(data, "published")
c.StartTime = JSONGetTime(data, "startTime")
c.EndTime = JSONGetTime(data, "endTime")
c.Duration = JSONGetDuration(data, "duration")
c.Icon = JSONGetItem(data, "icon")
c.Preview = JSONGetItem(data, "preview")
c.Image = JSONGetItem(data, "image")
c.Updated = JSONGetTime(data, "updated")
inReplyTo := JSONGetItems(data, "inReplyTo")
if len(inReplyTo) > 0 {
c.InReplyTo = inReplyTo
}
to := JSONGetItems(data, "to")
if len(to) > 0 {
c.To = to
}
audience := JSONGetItems(data, "audience")
if len(audience) > 0 {
c.Audience = audience
}
bto := JSONGetItems(data, "bto")
if len(bto) > 0 {
c.Bto = bto
}
cc := JSONGetItems(data, "cc")
if len(cc) > 0 {
c.CC = cc
}
bcc := JSONGetItems(data, "bcc")
if len(bcc) > 0 {
c.BCC = bcc
}
replies := JSONGetItem(data, "replies")
if replies != nil {
c.Replies = replies
}
tag := JSONGetItems(data, "tag")
if len(tag) > 0 {
c.Tag = tag
}
c.TotalItems = uint(JSONGetInt(data, "totalItems"))
c.Items = JSONGetItems(data, "items")
c.Current = JSONGetItem(data, "current")
c.First = JSONGetItem(data, "first")
c.Last = JSONGetItem(data, "last")
c.Next = JSONGetItem(data, "next")
c.Prev = JSONGetItem(data, "prev")
c.PartOf = JSONGetItem(data, "partOf")
return nil
}
// CollectionNew initializes a new CollectionPage
func CollectionPageNew(parent CollectionInterface) *CollectionPage {
p := CollectionPage{
PartOf: parent.GetLink(),
}
if pc, ok := parent.(*Collection); ok {
copyCollectionToPage(pc, &p)
}
p.Type = CollectionPageType
return &p
}
func copyCollectionToPage(c *Collection, p *CollectionPage) error {
p.ID = c.ID
return nil
}
// ToCollectionPage
func ToCollectionPage(it Item) (*CollectionPage, error) {
switch i := it.(type) {
case *CollectionPage:
return i, nil
case CollectionPage:
return &i, nil
}
return nil, errors.New("unable to convert to collection page")
}

145
collection_page_test.go Normal file
View file

@ -0,0 +1,145 @@
package activitystreams
import (
"reflect"
"testing"
)
func TestCollectionPageNew(t *testing.T) {
var testValue = ObjectID("test")
c := CollectionNew(testValue)
p := CollectionPageNew(c)
if reflect.DeepEqual(p.Collection, c) {
t.Errorf("Invalid collection parent '%v'", p.PartOf)
}
if p.PartOf != c.GetLink() {
t.Errorf("Invalid collection '%v'", p.PartOf)
}
}
func TestCollectionPage_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := CollectionNew(id)
p := CollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("Collection page should point to collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Collection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.Items[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestCollectionPage_UnmarshalJSON(t *testing.T) {
p := CollectionPage{}
dataEmpty := []byte("{}")
p.UnmarshalJSON(dataEmpty)
if p.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID)
}
if p.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", p.Type)
}
if p.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo)
}
if len(p.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name)
}
if len(p.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary)
}
if len(p.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content)
}
if p.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.Items) > 0 {
t.Errorf("Unmarshaled object should have empty Items, received %v", p.Items)
}
if p.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL)
}
if !p.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published)
}
if !p.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime)
}
if !p.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated)
}
if p.PartOf != nil {
t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf)
}
if p.Current != nil {
t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current)
}
if p.First != nil {
t.Errorf("Unmarshaled object should have empty First, received %q", p.First)
}
if p.Last != nil {
t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last)
}
if p.Next != nil {
t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next)
}
if p.Prev != nil {
t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev)
}
}
func TestCollectionPage_Collection(t *testing.T) {
id := ObjectID("test")
c := CollectionNew(id)
p := CollectionPageNew(c)
if !reflect.DeepEqual(p.Collection(), p.Items) {
t.Errorf("Collection items should be equal %v %v", p.Collection(), p.Items)
}
}
func TestCollectionPage_Count(t *testing.T) {
id := ObjectID("test")
c := CollectionNew(id)
p := CollectionPageNew(c)
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.Items) > 0 {
t.Errorf("Empty object should have empty Items, received %v", p.Items)
}
if p.Count() != uint(len(p.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
}
p.Append(IRI("test"))
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if p.Count() != uint(len(p.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
}
}
func TestToCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestCollectionPage_Contains(t *testing.T) {
t.Skipf("TODO")
}

View file

@ -2,7 +2,7 @@ package activitystreams
import (
"errors"
"github.com/buger/jsonparser"
"time"
)
const CollectionOfItems ActivityVocabularyType = "ItemCollection"
@ -25,7 +25,86 @@ type CollectionInterface interface {
// Collection is a subtype of Activity Pub Object that represents ordered or unordered sets of Activity Pub Object or Link instances.
type Collection struct {
Parent
// ID provides the globally unique identifier for anActivity Pub Object or Link.
ID ObjectID `jsonld:"id,omitempty"`
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
Type ActivityVocabularyType `jsonld:"type,omitempty"`
// Name a simple, human-readable, plain-text name for the object.
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
// The intent is to provide a model that is at least semantically similar to attachments in email.
Attachment Item `jsonld:"attachment,omitempty"`
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
// For instance, an object might be attributed to the completion of another activity.
AttributedTo Item `jsonld:"attributedTo,omitempty"`
// Audience identifies one or more entities that represent the total population of entities
// for which the object can considered to be relevant.
Audience ItemCollection `jsonld:"audience,omitempty"`
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
// By default, the value of content is HTML.
// The mediaType property can be used in the object to indicate a different content type.
// (The content MAY be expressed using multiple language-tagged values.)
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
// Context identifies the context within which the object exists or an activity was performed.
// The notion of "context" used is intentionally vague.
// The intended function is to serve as a means of grouping objects and activities that share a
// common originating context or purpose. An example could be all activities relating to a common project or event.
Context Item `jsonld:"context,omitempty"`
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
// If not specified, the content property is assumed to contain text/html content.
MediaType MimeType `jsonld:"mediaType,omitempty"`
// EndTime the date and time describing the actual or expected ending time of the object.
// When used with an Activity object, for instance, the endTime property specifies the moment
// the activity concluded or is expected to conclude.
EndTime time.Time `jsonld:"endTime,omitempty"`
// Generator identifies the entity (e.g. an application) that generated the object.
Generator Item `jsonld:"generator,omitempty"`
// Icon indicates an entity that describes an icon for this object.
// The image should have an aspect ratio of one (horizontal) to one (vertical)
// and should be suitable for presentation at a small size.
Icon Item `jsonld:"icon,omitempty"`
// Image indicates an entity that describes an image for this object.
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
Image Item `jsonld:"image,omitempty"`
// InReplyTo indicates one or more entities for which this object is considered a response.
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
// Location indicates one or more physical or logical locations associated with the object.
Location Item `jsonld:"location,omitempty"`
// Preview identifies an entity that provides a preview of this object.
Preview Item `jsonld:"preview,omitempty"`
// Published the date and time at which the object was published
Published time.Time `jsonld:"published,omitempty"`
// Replies identifies a Collection containing objects considered to be responses to this object.
Replies Item `jsonld:"replies,omitempty"`
// StartTime the date and time describing the actual or expected starting time of the object.
// When used with an Activity object, for instance, the startTime property specifies
// the moment the activity began or is scheduled to begin.
StartTime time.Time `jsonld:"startTime,omitempty"`
// Summary a natural language summarization of the object encoded as HTML.
// *Multiple language tagged summaries may be provided.)
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
// The key difference between attachment and tag is that the former implies association by inclusion,
// while the latter implies associated by reference.
Tag ItemCollection `jsonld:"tag,omitempty"`
// Updated the date and time at which the object was updated
Updated time.Time `jsonld:"updated,omitempty"`
// URL identifies one or more links to representations of the object
URL LinkOrIRI `jsonld:"url,omitempty"`
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
To ItemCollection `jsonld:"to,omitempty"`
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
Bto ItemCollection `jsonld:"bto,omitempty"`
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
CC ItemCollection `jsonld:"cc,omitempty"`
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
BCC ItemCollection `jsonld:"bcc,omitempty"`
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
// the duration property indicates the object's approximate duration.
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
Duration time.Duration `jsonld:"duration,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current ObjectOrLink `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
@ -39,57 +118,9 @@ type Collection struct {
Items ItemCollection `jsonld:"items,omitempty"`
}
type ParentCollection = Collection
// OrderedCollection is a subtype of Collection in which members of the logical
// collection are assumed to always be strictly ordered.
type OrderedCollection struct {
Parent
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current ObjectOrLink `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
First ObjectOrLink `jsonld:"first,omitempty"`
// In a paged Collection, indicates the furthest proceeding page of the collection.
Last ObjectOrLink `jsonld:"last,omitempty"`
// A non-negative integer specifying the total number of objects contained by the logical view of the collection.
// This number might not reflect the actual number of items serialized within the Collection object instance.
TotalItems uint `jsonld:"totalItems"`
// Identifies the items contained in a collection. The items might be ordered or unordered.
OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"`
}
// CollectionPage is a Collection that contains a large number of items and when it becomes impractical
// for an implementation to serialize every item contained by a Collection using the items (or orderedItems)
// property alone. In such cases, the items within a Collection can be divided into distinct subsets or "pages".
type CollectionPage struct {
ParentCollection
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the next page of items.
Next Item `jsonld:"next,omitempty"`
// In a paged Collection, identifies the previous page of items.
Prev Item `jsonld:"prev,omitempty"`
}
// OrderedCollectionPage type extends from both CollectionPage and OrderedCollection.
// In addition to the properties inherited from each of those, the OrderedCollectionPage
// may contain an additional startIndex property whose value indicates the relative index position
// of the first item contained by the page within the OrderedCollection to which the page belongs.
type OrderedCollectionPage struct {
OrderedCollection
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the next page of items.
Next Item `jsonld:"next,omitempty"`
// In a paged Collection, identifies the previous page of items.
Prev Item `jsonld:"prev,omitempty"`
// A non-negative integer value identifying the relative position within the logical view of a strictly ordered collection.
StartIndex uint `jsonld:"startIndex,omitempty"`
}
// CollectionNew initializes a new Collection
func CollectionNew(id ObjectID) *Collection {
c := Collection{Parent: Parent{ID: id, Type: CollectionType}}
c := Collection{ID: id, Type: CollectionType}
c.Name = NaturalLanguageValuesNew()
c.Content = NaturalLanguageValuesNew()
c.Summary = NaturalLanguageValuesNew()
@ -98,49 +129,46 @@ func CollectionNew(id ObjectID) *Collection {
// OrderedCollectionNew initializes a new OrderedCollection
func OrderedCollectionNew(id ObjectID) *OrderedCollection {
o := OrderedCollection{Parent: Parent{ID: id, Type: OrderedCollectionType}}
o := OrderedCollection{ID: id, Type: OrderedCollectionType}
o.Name = NaturalLanguageValuesNew()
o.Content = NaturalLanguageValuesNew()
return &o
}
// CollectionNew initializes a new CollectionPage
func CollectionPageNew(parent CollectionInterface) *CollectionPage {
p := CollectionPage{
PartOf: parent.GetLink(),
}
if pc, ok := parent.(*Collection); ok {
p.ParentCollection = *pc
}
p.Type = CollectionPageType
return &p
// GetID returns the ObjectID corresponding to the Collection object
func (c Collection) GetID() *ObjectID {
return &c.ID
}
// OrderedCollectionPageNew initializes a new OrderedCollectionPage
func OrderedCollectionPageNew(parent CollectionInterface) *OrderedCollectionPage {
p := OrderedCollectionPage{
PartOf: parent.GetLink(),
}
if pc, ok := parent.(*OrderedCollection); ok {
p.OrderedCollection = *pc
}
p.Type = OrderedCollectionPageType
return &p
// GetType returns the Collection's type
func (c Collection) GetType() ActivityVocabularyType {
return c.Type
}
// Append adds an element to an OrderedCollection
func (o *OrderedCollection) Append(ob Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
return nil
// IsLink returns false for a Collection object
func (c Collection) IsLink() bool {
return false
}
// Count returns the maximum between the length of Items in collection and its TotalItems property
func (o *OrderedCollection) Count() uint {
if o.TotalItems > 0 {
return o.TotalItems
}
return uint(len(o.OrderedItems))
// IsObject returns true for a Collection object
func (c Collection) IsObject() bool {
return true
}
// IsCollection returns true for Collection objects
func (c Collection) IsCollection() bool {
return true
}
// GetLink returns the IRI corresponding to the Collection object
func (c Collection) GetLink() IRI {
return IRI(c.ID)
}
// Collection returns the Collection's items
func (c Collection) Collection() ItemCollection {
return c.Items
}
// Append adds an element to a Collection
@ -157,104 +185,76 @@ func (c *Collection) Count() uint {
return uint(len(c.Items))
}
// Append adds an element to an OrderedCollectionPage
func (o *OrderedCollectionPage) Append(ob Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
return nil
}
// Count returns the maximum between the length of Items in the collection page and its TotalItems property
func (o *OrderedCollectionPage) Count() uint {
if o.TotalItems > 0 {
return o.TotalItems
// Contains verifies if Collection array contains the received one
func (c Collection) Contains(r IRI) bool {
if len(c.Items) == 0 {
return false
}
return uint(len(o.OrderedItems))
}
// Append adds an element to a CollectionPage
func (c *CollectionPage) Append(ob Item) error {
c.Items = append(c.Items, ob)
return nil
}
// Count returns the maximum between the length of Items in the collection page and its TotalItems property
func (c *CollectionPage) Count() uint {
if c.TotalItems > 0 {
return c.TotalItems
for _, iri := range c.Items {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return uint(len(c.Items))
}
// GetType returns the Collection's type
func (c Collection) GetType() ActivityVocabularyType {
return c.Type
}
// IsLink returns false for a Collection object
func (c Collection) IsLink() bool {
return false
}
// GetID returns the ObjectID corresponding to the Collection object
func (c Collection) GetID() *ObjectID {
return &c.ID
}
// GetLink returns the IRI corresponding to the Collection object
func (c Collection) GetLink() IRI {
return IRI(c.ID)
}
// IsObject returns true for a Collection object
func (c Collection) IsObject() bool {
return true
}
// GetType returns the OrderedCollection's type
func (o OrderedCollection) GetType() ActivityVocabularyType {
return o.Type
}
// IsLink returns false for an OrderedCollection object
func (o OrderedCollection) IsLink() bool {
return false
}
// GetID returns the ObjectID corresponding to the OrderedCollection
func (o OrderedCollection) GetID() *ObjectID {
return &o.ID
}
// GetLink returns the IRI corresponding to the OrderedCollection object
func (o OrderedCollection) GetLink() IRI {
return IRI(o.ID)
}
// IsObject returns true for am OrderedCollection object
func (o OrderedCollection) IsObject() bool {
return true
}
// UnmarshalJSON
func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
o.Parent.UnmarshalJSON(data)
o.TotalItems = uint(JSONGetInt(data, "totalItems"))
o.OrderedItems = JSONGetItems(data, "orderedItems")
o.Current = JSONGetItem(data, "current")
o.First = JSONGetItem(data, "first")
o.Last = JSONGetItem(data, "last")
return nil
}
// UnmarshalJSON
func (c *Collection) UnmarshalJSON(data []byte) error {
c.Parent.UnmarshalJSON(data)
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
c.ID = JSONGetObjectID(data)
c.Type = JSONGetType(data)
c.Name = JSONGetNaturalLanguageField(data, "name")
c.Content = JSONGetNaturalLanguageField(data, "content")
c.Summary = JSONGetNaturalLanguageField(data, "summary")
c.Context = JSONGetItem(data, "context")
c.URL = JSONGetURIItem(data, "url")
c.MediaType = MimeType(JSONGetString(data, "mediaType"))
c.Generator = JSONGetItem(data, "generator")
c.AttributedTo = JSONGetItem(data, "attributedTo")
c.Attachment = JSONGetItem(data, "attachment")
c.Location = JSONGetItem(data, "location")
c.Published = JSONGetTime(data, "published")
c.StartTime = JSONGetTime(data, "startTime")
c.EndTime = JSONGetTime(data, "endTime")
c.Duration = JSONGetDuration(data, "duration")
c.Icon = JSONGetItem(data, "icon")
c.Preview = JSONGetItem(data, "preview")
c.Image = JSONGetItem(data, "image")
c.Updated = JSONGetTime(data, "updated")
inReplyTo := JSONGetItems(data, "inReplyTo")
if len(inReplyTo) > 0 {
c.InReplyTo = inReplyTo
}
to := JSONGetItems(data, "to")
if len(to) > 0 {
c.To = to
}
audience := JSONGetItems(data, "audience")
if len(audience) > 0 {
c.Audience = audience
}
bto := JSONGetItems(data, "bto")
if len(bto) > 0 {
c.Bto = bto
}
cc := JSONGetItems(data, "cc")
if len(cc) > 0 {
c.CC = cc
}
bcc := JSONGetItems(data, "bcc")
if len(bcc) > 0 {
c.BCC = bcc
}
replies := JSONGetItem(data, "replies")
if replies != nil {
c.Replies = replies
}
tag := JSONGetItems(data, "tag")
if len(tag) > 0 {
c.Tag = tag
}
c.TotalItems = uint(JSONGetInt(data, "totalItems"))
c.Items = JSONGetItems(data, "items")
@ -266,81 +266,6 @@ func (c *Collection) UnmarshalJSON(data []byte) error {
return nil
}
// UnmarshalJSON
func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
o.OrderedCollection.UnmarshalJSON(data)
o.Next = JSONGetItem(data, "next")
o.Prev = JSONGetItem(data, "prev")
o.PartOf = JSONGetItem(data, "partOf")
if si, err := jsonparser.GetInt(data, "startIndex"); err != nil {
o.StartIndex = uint(si)
}
return nil
}
// UnmarshalJSON
func (c *CollectionPage) UnmarshalJSON(data []byte) error {
c.ParentCollection.UnmarshalJSON(data)
c.Next = JSONGetItem(data, "next")
c.Prev = JSONGetItem(data, "prev")
c.PartOf = JSONGetItem(data, "partOf")
return nil
}
/*
func (c *Collection) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (o *OrderedCollection) MarshalJSON() ([]byte, error) {
return nil, nil
}
*/
// Collection returns the underlying Collection type
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() 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() 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() ItemCollection {
return o.OrderedItems
}
// IsCollection returns true for OrderedCollectionPage objects
func (o OrderedCollectionPage) IsCollection() bool {
return true
}
// Flatten checks if Item can be flatten to an IRI or array of IRIs and returns it if so
func Flatten(it Item) Item {
if it.IsCollection() {
@ -385,88 +310,3 @@ func ToCollection(it Item) (*Collection, error) {
}
return nil, errors.New("unable to convert to collection")
}
// ToCollectionPage
func ToCollectionPage(it Item) (*CollectionPage, error) {
switch i := it.(type) {
case *CollectionPage:
return i, nil
case CollectionPage:
return &i, nil
}
return nil, errors.New("unable to convert to collection page")
}
// ToOrderedCollection
func ToOrderedCollection(it Item) (*OrderedCollection, error) {
switch i := it.(type) {
case *OrderedCollection:
return i, nil
case OrderedCollection:
return &i, nil
}
return nil, errors.New("unable to convert to ordered collection")
}
// ToOrderedCollectionPage
func ToOrderedCollectionPage(it Item) (*OrderedCollectionPage, error) {
switch i := it.(type) {
case *OrderedCollectionPage:
return i, nil
case OrderedCollectionPage:
return &i, nil
}
return nil, errors.New("unable to convert to ordered collection page")
}
// Contains verifies if Collection array contains the received one
func (c Collection) Contains(r IRI) bool {
if len(c.Items) == 0 {
return false
}
for _, iri := range c.Items {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// Contains verifies if OrderedCollection array contains the received one
func (o OrderedCollection) Contains(r IRI) bool {
if len(o.OrderedItems) == 0 {
return false
}
for _, iri := range o.OrderedItems {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// Contains verifies if CollectionPage array contains the received one
func (c CollectionPage) Contains(r IRI) bool {
if len(c.Items) == 0 {
return false
}
for _, iri := range c.Items {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// Contains verifies if OrderedCollectionPage array contains the received one
func (o OrderedCollectionPage) Contains(r IRI) bool {
if len(o.OrderedItems) == 0 {
return false
}
for _, iri := range o.OrderedItems {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}

View file

@ -18,61 +18,6 @@ func TestCollectionNew(t *testing.T) {
}
}
func TestOrderedCollectionNew(t *testing.T) {
var testValue = ObjectID("test")
c := OrderedCollectionNew(testValue)
if c.ID != testValue {
t.Errorf("APObject Id '%v' different than expected '%v'", c.ID, testValue)
}
if c.Type != OrderedCollectionType {
t.Errorf("APObject Type '%v' different than expected '%v'", c.Type, OrderedCollectionType)
}
}
func TestCollectionPageNew(t *testing.T) {
var testValue = ObjectID("test")
c := CollectionNew(testValue)
p := CollectionPageNew(c)
if reflect.DeepEqual(p.Collection, c) {
t.Errorf("Invalid collection parent '%v'", p.PartOf)
}
if p.PartOf != c.GetLink() {
t.Errorf("Invalid collection '%v'", p.PartOf)
}
}
func TestOrderedCollectionPageNew(t *testing.T) {
var testValue = ObjectID("test")
c := OrderedCollectionNew(testValue)
p := OrderedCollectionPageNew(c)
if reflect.DeepEqual(p.OrderedCollection, c) {
t.Errorf("Invalid ordered collection parent '%v'", p.PartOf)
}
if p.PartOf != c.GetLink() {
t.Errorf("Invalid collection '%v'", p.PartOf)
}
}
func Test_OrderedCollection_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
c.Append(val)
if c.Count() != 1 {
t.Errorf("Inbox collection of %q should have one element", *c.GetID())
}
if !reflect.DeepEqual(c.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestCollection_Append(t *testing.T) {
id := ObjectID("test")
@ -89,27 +34,6 @@ func TestCollection_Append(t *testing.T) {
}
}
func TestCollectionPage_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := CollectionNew(id)
p := CollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("Collection page should point to collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Collection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.Items[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestCollection_Collection(t *testing.T) {
id := ObjectID("test")
@ -214,296 +138,6 @@ func TestCollection_UnmarshalJSON(t *testing.T) {
}
}
func TestCollectionPage_UnmarshalJSON(t *testing.T) {
p := CollectionPage{}
dataEmpty := []byte("{}")
p.UnmarshalJSON(dataEmpty)
if p.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID)
}
if p.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", p.Type)
}
if p.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo)
}
if len(p.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name)
}
if len(p.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary)
}
if len(p.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content)
}
if p.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.Items) > 0 {
t.Errorf("Unmarshaled object should have empty Items, received %v", p.Items)
}
if p.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL)
}
if !p.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published)
}
if !p.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime)
}
if !p.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated)
}
if p.PartOf != nil {
t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf)
}
if p.Current != nil {
t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current)
}
if p.First != nil {
t.Errorf("Unmarshaled object should have empty First, received %q", p.First)
}
if p.Last != nil {
t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last)
}
if p.Next != nil {
t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next)
}
if p.Prev != nil {
t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev)
}
}
func TestOrderedCollection_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("Ordereed collection page should point to ordered collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Ordered collection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestOrderedCollection_Collection(t *testing.T) {
id := ObjectID("test")
o := OrderedCollectionNew(id)
if !reflect.DeepEqual(o.Collection(), o.OrderedItems) {
t.Errorf("Collection items should be equal %v %v", o.Collection(), o.OrderedItems)
}
}
func TestOrderedCollection_GetID(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if *c.GetID() != id {
t.Errorf("GetID should return %q, received %q", id, *c.GetID())
}
}
func TestOrderedCollection_GetLink(t *testing.T) {
id := ObjectID("test")
link := IRI(id)
c := OrderedCollectionNew(id)
if c.GetLink() != link {
t.Errorf("GetLink should return %q, received %q", link, c.GetLink())
}
}
func TestOrderedCollection_GetType(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.GetType() != OrderedCollectionType {
t.Errorf("OrderedCollection Type should be %q, received %q", OrderedCollectionType, c.GetType())
}
}
func TestOrderedCollection_IsLink(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.IsLink() != false {
t.Errorf("OrderedCollection should not be a link, received %t", c.IsLink())
}
}
func TestOrderedCollection_IsObject(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.IsObject() != true {
t.Errorf("OrderedCollection should be an object, received %t", c.IsObject())
}
}
func TestOrderedCollection_UnmarshalJSON(t *testing.T) {
c := OrderedCollection{}
dataEmpty := []byte("{}")
c.UnmarshalJSON(dataEmpty)
if c.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", c.ID)
}
if c.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", c.Type)
}
if c.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", c.AttributedTo)
}
if len(c.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", c.Name)
}
if len(c.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", c.Summary)
}
if len(c.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", c.Content)
}
if c.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", c.TotalItems)
}
if len(c.OrderedItems) > 0 {
t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", c.OrderedItems)
}
if c.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", c.URL)
}
if !c.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", c.Published)
}
if !c.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", c.StartTime)
}
if !c.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", c.Updated)
}
}
func TestOrderedCollectionPage_UnmarshalJSON(t *testing.T) {
p := OrderedCollectionPage{}
dataEmpty := []byte("{}")
p.UnmarshalJSON(dataEmpty)
if p.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID)
}
if p.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", p.Type)
}
if p.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo)
}
if len(p.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name)
}
if len(p.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary)
}
if len(p.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content)
}
if p.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.OrderedItems) > 0 {
t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", p.OrderedItems)
}
if p.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL)
}
if !p.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published)
}
if !p.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime)
}
if !p.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated)
}
if p.PartOf != nil {
t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf)
}
if p.Current != nil {
t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current)
}
if p.First != nil {
t.Errorf("Unmarshaled object should have empty First, received %q", p.First)
}
if p.Last != nil {
t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last)
}
if p.Next != nil {
t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next)
}
if p.Prev != nil {
t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev)
}
}
func TestOrderedCollectionPage_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("OrderedCollection page should point to OrderedCollection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("OrderedCollection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestOrderedCollectionPage_Collection(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
if !reflect.DeepEqual(p.Collection(), p.OrderedItems) {
t.Errorf("Collection items should be equal %v %v", p.Collection(), p.OrderedItems)
}
}
func TestCollectionPage_Collection(t *testing.T) {
id := ObjectID("test")
c := CollectionNew(id)
p := CollectionPageNew(c)
if !reflect.DeepEqual(p.Collection(), p.Items) {
t.Errorf("Collection items should be equal %v %v", p.Collection(), p.Items)
}
}
func TestCollection_Count(t *testing.T) {
id := ObjectID("test")
@ -528,108 +162,11 @@ func TestCollection_Count(t *testing.T) {
}
}
func TestCollectionPage_Count(t *testing.T) {
id := ObjectID("test")
c := CollectionNew(id)
p := CollectionPageNew(c)
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.Items) > 0 {
t.Errorf("Empty object should have empty Items, received %v", p.Items)
}
if p.Count() != uint(len(p.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
}
p.Append(IRI("test"))
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if p.Count() != uint(len(p.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
}
}
func TestOrderedCollection_Count(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if len(c.OrderedItems) > 0 {
t.Errorf("Empty object should have empty Items, received %v", c.OrderedItems)
}
if c.Count() != uint(len(c.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems))
}
c.Append(IRI("test"))
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if c.Count() != uint(len(c.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems))
}
}
func TestOrderedCollectionPage_Count(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.OrderedItems) > 0 {
t.Errorf("Empty object should have empty Items, received %v", p.OrderedItems)
}
if p.Count() != uint(len(p.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems))
}
p.Append(IRI("test"))
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if p.Count() != uint(len(p.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems))
}
}
func TestToCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestToCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestToOrderedCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestToOrderedCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestCollection_Contains(t *testing.T) {
t.Skipf("TODO")
}
func TestCollectionPage_Contains(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollection_Contains(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollectionPage_Contains(t *testing.T) {
t.Skipf("TODO")
}

View file

@ -582,8 +582,6 @@ func (o *Object) Clean() {
}
type (
object = Object
Parent = Object
// Article represents any kind of multi-paragraph written work.
Article = Object
// Audio represents an audio document of any kind.

260
ordered_collection.go Normal file
View file

@ -0,0 +1,260 @@
package activitystreams
import (
"errors"
"time"
)
// OrderedCollection is a subtype of Collection in which members of the logical
// collection are assumed to always be strictly ordered.
type OrderedCollection struct {
// ID provides the globally unique identifier for anActivity Pub Object or Link.
ID ObjectID `jsonld:"id,omitempty"`
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
Type ActivityVocabularyType `jsonld:"type,omitempty"`
// Name a simple, human-readable, plain-text name for the object.
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
// The intent is to provide a model that is at least semantically similar to attachments in email.
Attachment Item `jsonld:"attachment,omitempty"`
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
// For instance, an object might be attributed to the completion of another activity.
AttributedTo Item `jsonld:"attributedTo,omitempty"`
// Audience identifies one or more entities that represent the total population of entities
// for which the object can considered to be relevant.
Audience ItemCollection `jsonld:"audience,omitempty"`
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
// By default, the value of content is HTML.
// The mediaType property can be used in the object to indicate a different content type.
// (The content MAY be expressed using multiple language-tagged values.)
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
// Context identifies the context within which the object exists or an activity was performed.
// The notion of "context" used is intentionally vague.
// The intended function is to serve as a means of grouping objects and activities that share a
// common originating context or purpose. An example could be all activities relating to a common project or event.
Context Item `jsonld:"context,omitempty"`
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
// If not specified, the content property is assumed to contain text/html content.
MediaType MimeType `jsonld:"mediaType,omitempty"`
// EndTime the date and time describing the actual or expected ending time of the object.
// When used with an Activity object, for instance, the endTime property specifies the moment
// the activity concluded or is expected to conclude.
EndTime time.Time `jsonld:"endTime,omitempty"`
// Generator identifies the entity (e.g. an application) that generated the object.
Generator Item `jsonld:"generator,omitempty"`
// Icon indicates an entity that describes an icon for this object.
// The image should have an aspect ratio of one (horizontal) to one (vertical)
// and should be suitable for presentation at a small size.
Icon Item `jsonld:"icon,omitempty"`
// Image indicates an entity that describes an image for this object.
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
Image Item `jsonld:"image,omitempty"`
// InReplyTo indicates one or more entities for which this object is considered a response.
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
// Location indicates one or more physical or logical locations associated with the object.
Location Item `jsonld:"location,omitempty"`
// Preview identifies an entity that provides a preview of this object.
Preview Item `jsonld:"preview,omitempty"`
// Published the date and time at which the object was published
Published time.Time `jsonld:"published,omitempty"`
// Replies identifies a Collection containing objects considered to be responses to this object.
Replies Item `jsonld:"replies,omitempty"`
// StartTime the date and time describing the actual or expected starting time of the object.
// When used with an Activity object, for instance, the startTime property specifies
// the moment the activity began or is scheduled to begin.
StartTime time.Time `jsonld:"startTime,omitempty"`
// Summary a natural language summarization of the object encoded as HTML.
// *Multiple language tagged summaries may be provided.)
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
// The key difference between attachment and tag is that the former implies association by inclusion,
// while the latter implies associated by reference.
Tag ItemCollection `jsonld:"tag,omitempty"`
// Updated the date and time at which the object was updated
Updated time.Time `jsonld:"updated,omitempty"`
// URL identifies one or more links to representations of the object
URL LinkOrIRI `jsonld:"url,omitempty"`
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
To ItemCollection `jsonld:"to,omitempty"`
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
Bto ItemCollection `jsonld:"bto,omitempty"`
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
CC ItemCollection `jsonld:"cc,omitempty"`
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
BCC ItemCollection `jsonld:"bcc,omitempty"`
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
// the duration property indicates the object's approximate duration.
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
Duration time.Duration `jsonld:"duration,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current ObjectOrLink `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
First ObjectOrLink `jsonld:"first,omitempty"`
// In a paged Collection, indicates the furthest proceeding page of the collection.
Last ObjectOrLink `jsonld:"last,omitempty"`
// A non-negative integer specifying the total number of objects contained by the logical view of the collection.
// This number might not reflect the actual number of items serialized within the Collection object instance.
TotalItems uint `jsonld:"totalItems"`
// Identifies the items contained in a collection. The items might be ordered or unordered.
OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"`
}
// GetType returns the OrderedCollection's type
func (o OrderedCollection) GetType() ActivityVocabularyType {
return o.Type
}
// IsLink returns false for an OrderedCollection object
func (o OrderedCollection) IsLink() bool {
return false
}
// GetID returns the ObjectID corresponding to the OrderedCollection
func (o OrderedCollection) GetID() *ObjectID {
return &o.ID
}
// GetLink returns the IRI corresponding to the OrderedCollection object
func (o OrderedCollection) GetLink() IRI {
return IRI(o.ID)
}
// IsObject returns true for am OrderedCollection object
func (o OrderedCollection) IsObject() bool {
return true
}
// Collection returns the underlying Collection type
func (o *OrderedCollection) Collection() ItemCollection {
return o.OrderedItems
}
// IsCollection returns true for OrderedCollection objects
func (o OrderedCollection) IsCollection() bool {
return true
}
// Contains verifies if OrderedCollection array contains the received one
func (o OrderedCollection) Contains(r IRI) bool {
if len(o.OrderedItems) == 0 {
return false
}
for _, iri := range o.OrderedItems {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// Count returns the maximum between the length of Items in collection and its TotalItems property
func (o *OrderedCollection) Count() uint {
if o.TotalItems > 0 {
return o.TotalItems
}
return uint(len(o.OrderedItems))
}
// Append adds an element to an OrderedCollection
func (o *OrderedCollection) Append(ob Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
return nil
}
// UnmarshalJSON
func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
o.ID = JSONGetObjectID(data)
o.Type = JSONGetType(data)
o.Name = JSONGetNaturalLanguageField(data, "name")
o.Content = JSONGetNaturalLanguageField(data, "content")
o.Summary = JSONGetNaturalLanguageField(data, "summary")
o.Context = JSONGetItem(data, "context")
o.URL = JSONGetURIItem(data, "url")
o.MediaType = MimeType(JSONGetString(data, "mediaType"))
o.Generator = JSONGetItem(data, "generator")
o.AttributedTo = JSONGetItem(data, "attributedTo")
o.Attachment = JSONGetItem(data, "attachment")
o.Location = JSONGetItem(data, "location")
o.Published = JSONGetTime(data, "published")
o.StartTime = JSONGetTime(data, "startTime")
o.EndTime = JSONGetTime(data, "endTime")
o.Duration = JSONGetDuration(data, "duration")
o.Icon = JSONGetItem(data, "icon")
o.Preview = JSONGetItem(data, "preview")
o.Image = JSONGetItem(data, "image")
o.Updated = JSONGetTime(data, "updated")
inReplyTo := JSONGetItems(data, "inReplyTo")
if len(inReplyTo) > 0 {
o.InReplyTo = inReplyTo
}
to := JSONGetItems(data, "to")
if len(to) > 0 {
o.To = to
}
audience := JSONGetItems(data, "audience")
if len(audience) > 0 {
o.Audience = audience
}
bto := JSONGetItems(data, "bto")
if len(bto) > 0 {
o.Bto = bto
}
cc := JSONGetItems(data, "cc")
if len(cc) > 0 {
o.CC = cc
}
bcc := JSONGetItems(data, "bcc")
if len(bcc) > 0 {
o.BCC = bcc
}
replies := JSONGetItem(data, "replies")
if replies != nil {
o.Replies = replies
}
tag := JSONGetItems(data, "tag")
if len(tag) > 0 {
o.Tag = tag
}
o.TotalItems = uint(JSONGetInt(data, "totalItems"))
o.OrderedItems = JSONGetItems(data, "orderedItems")
o.Current = JSONGetItem(data, "current")
o.First = JSONGetItem(data, "first")
o.Last = JSONGetItem(data, "last")
return nil
}
// OrderedCollectionPageNew initializes a new OrderedCollectionPage
func OrderedCollectionPageNew(parent CollectionInterface) *OrderedCollectionPage {
p := OrderedCollectionPage{
PartOf: parent.GetLink(),
}
if pc, ok := parent.(*OrderedCollection); ok {
copyOrderedCollectionToPage(pc, &p)
}
p.Type = OrderedCollectionPageType
return &p
}
// ToOrderedCollection
func ToOrderedCollection(it Item) (*OrderedCollection, error) {
switch i := it.(type) {
case *OrderedCollection:
return i, nil
case OrderedCollection:
return &i, nil
}
return nil, errors.New("unable to convert to ordered collection")
}
func copyOrderedCollectionToPage(c *OrderedCollection, p *OrderedCollectionPage) error {
p.ID = c.ID
return nil
}

261
ordered_collection_page.go Normal file
View file

@ -0,0 +1,261 @@
package activitystreams
import (
"errors"
"github.com/buger/jsonparser"
"time"
)
// OrderedCollectionPage type extends from both CollectionPage and OrderedCollection.
// In addition to the properties inherited from each of those, the OrderedCollectionPage
// may contain an additional startIndex property whose value indicates the relative index position
// of the first item contained by the page within the OrderedCollection to which the page belongs.
type OrderedCollectionPage struct {
// ID provides the globally unique identifier for anActivity Pub Object or Link.
ID ObjectID `jsonld:"id,omitempty"`
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
Type ActivityVocabularyType `jsonld:"type,omitempty"`
// Name a simple, human-readable, plain-text name for the object.
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
// The intent is to provide a model that is at least semantically similar to attachments in email.
Attachment Item `jsonld:"attachment,omitempty"`
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
// For instance, an object might be attributed to the completion of another activity.
AttributedTo Item `jsonld:"attributedTo,omitempty"`
// Audience identifies one or more entities that represent the total population of entities
// for which the object can considered to be relevant.
Audience ItemCollection `jsonld:"audience,omitempty"`
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
// By default, the value of content is HTML.
// The mediaType property can be used in the object to indicate a different content type.
// (The content MAY be expressed using multiple language-tagged values.)
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
// Context identifies the context within which the object exists or an activity was performed.
// The notion of "context" used is intentionally vague.
// The intended function is to serve as a means of grouping objects and activities that share a
// common originating context or purpose. An example could be all activities relating to a common project or event.
Context Item `jsonld:"context,omitempty"`
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
// If not specified, the content property is assumed to contain text/html content.
MediaType MimeType `jsonld:"mediaType,omitempty"`
// EndTime the date and time describing the actual or expected ending time of the object.
// When used with an Activity object, for instance, the endTime property specifies the moment
// the activity concluded or is expected to conclude.
EndTime time.Time `jsonld:"endTime,omitempty"`
// Generator identifies the entity (e.g. an application) that generated the object.
Generator Item `jsonld:"generator,omitempty"`
// Icon indicates an entity that describes an icon for this object.
// The image should have an aspect ratio of one (horizontal) to one (vertical)
// and should be suitable for presentation at a small size.
Icon Item `jsonld:"icon,omitempty"`
// Image indicates an entity that describes an image for this object.
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
Image Item `jsonld:"image,omitempty"`
// InReplyTo indicates one or more entities for which this object is considered a response.
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
// Location indicates one or more physical or logical locations associated with the object.
Location Item `jsonld:"location,omitempty"`
// Preview identifies an entity that provides a preview of this object.
Preview Item `jsonld:"preview,omitempty"`
// Published the date and time at which the object was published
Published time.Time `jsonld:"published,omitempty"`
// Replies identifies a Collection containing objects considered to be responses to this object.
Replies Item `jsonld:"replies,omitempty"`
// StartTime the date and time describing the actual or expected starting time of the object.
// When used with an Activity object, for instance, the startTime property specifies
// the moment the activity began or is scheduled to begin.
StartTime time.Time `jsonld:"startTime,omitempty"`
// Summary a natural language summarization of the object encoded as HTML.
// *Multiple language tagged summaries may be provided.)
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
// The key difference between attachment and tag is that the former implies association by inclusion,
// while the latter implies associated by reference.
Tag ItemCollection `jsonld:"tag,omitempty"`
// Updated the date and time at which the object was updated
Updated time.Time `jsonld:"updated,omitempty"`
// URL identifies one or more links to representations of the object
URL LinkOrIRI `jsonld:"url,omitempty"`
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
To ItemCollection `jsonld:"to,omitempty"`
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
Bto ItemCollection `jsonld:"bto,omitempty"`
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
CC ItemCollection `jsonld:"cc,omitempty"`
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
BCC ItemCollection `jsonld:"bcc,omitempty"`
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
// the duration property indicates the object's approximate duration.
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
Duration time.Duration `jsonld:"duration,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current ObjectOrLink `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
First ObjectOrLink `jsonld:"first,omitempty"`
// In a paged Collection, indicates the furthest proceeding page of the collection.
Last ObjectOrLink `jsonld:"last,omitempty"`
// A non-negative integer specifying the total number of objects contained by the logical view of the collection.
// This number might not reflect the actual number of items serialized within the Collection object instance.
TotalItems uint `jsonld:"totalItems"`
// Identifies the items contained in a collection. The items might be ordered or unordered.
OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"`
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the next page of items.
Next Item `jsonld:"next,omitempty"`
// In a paged Collection, identifies the previous page of items.
Prev Item `jsonld:"prev,omitempty"`
// A non-negative integer value identifying the relative position within the logical view of a strictly ordered collection.
StartIndex uint `jsonld:"startIndex,omitempty"`
}
// GetID returns the ObjectID corresponding to the OrderedCollectionPage object
func (o OrderedCollectionPage) GetID() *ObjectID {
return &o.ID
}
// GetType returns the OrderedCollectionPage's type
func (o OrderedCollectionPage) GetType() ActivityVocabularyType {
return o.Type
}
// IsLink returns false for a OrderedCollectionPage object
func (o OrderedCollectionPage) IsLink() bool {
return false
}
// IsObject returns true for a OrderedCollectionPage object
func (o OrderedCollectionPage) IsObject() bool {
return true
}
// IsCollection returns true for OrderedCollectionPage objects
func (o OrderedCollectionPage) IsCollection() bool {
return true
}
// GetLink returns the IRI corresponding to the OrderedCollectionPage object
func (o OrderedCollectionPage) GetLink() IRI {
return IRI(o.ID)
}
// Collection returns the underlying Collection type
func (o *OrderedCollectionPage) Collection() ItemCollection {
return o.OrderedItems
}
// Count returns the maximum between the length of Items in the collection page and its TotalItems property
func (o *OrderedCollectionPage) Count() uint {
if o.TotalItems > 0 {
return o.TotalItems
}
return uint(len(o.OrderedItems))
}
// Append adds an element to an OrderedCollectionPage
func (o *OrderedCollectionPage) Append(ob Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
return nil
}
// Contains verifies if OrderedCollectionPage array contains the received one
func (o OrderedCollectionPage) Contains(r IRI) bool {
if len(o.OrderedItems) == 0 {
return false
}
for _, iri := range o.OrderedItems {
if r.Equals(iri.GetLink(), false) {
return true
}
}
return false
}
// UnmarshalJSON
func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
o.ID = JSONGetObjectID(data)
o.Type = JSONGetType(data)
o.Name = JSONGetNaturalLanguageField(data, "name")
o.Content = JSONGetNaturalLanguageField(data, "content")
o.Summary = JSONGetNaturalLanguageField(data, "summary")
o.Context = JSONGetItem(data, "context")
o.URL = JSONGetURIItem(data, "url")
o.MediaType = MimeType(JSONGetString(data, "mediaType"))
o.Generator = JSONGetItem(data, "generator")
o.AttributedTo = JSONGetItem(data, "attributedTo")
o.Attachment = JSONGetItem(data, "attachment")
o.Location = JSONGetItem(data, "location")
o.Published = JSONGetTime(data, "published")
o.StartTime = JSONGetTime(data, "startTime")
o.EndTime = JSONGetTime(data, "endTime")
o.Duration = JSONGetDuration(data, "duration")
o.Icon = JSONGetItem(data, "icon")
o.Preview = JSONGetItem(data, "preview")
o.Image = JSONGetItem(data, "image")
o.Updated = JSONGetTime(data, "updated")
inReplyTo := JSONGetItems(data, "inReplyTo")
if len(inReplyTo) > 0 {
o.InReplyTo = inReplyTo
}
to := JSONGetItems(data, "to")
if len(to) > 0 {
o.To = to
}
audience := JSONGetItems(data, "audience")
if len(audience) > 0 {
o.Audience = audience
}
bto := JSONGetItems(data, "bto")
if len(bto) > 0 {
o.Bto = bto
}
cc := JSONGetItems(data, "cc")
if len(cc) > 0 {
o.CC = cc
}
bcc := JSONGetItems(data, "bcc")
if len(bcc) > 0 {
o.BCC = bcc
}
replies := JSONGetItem(data, "replies")
if replies != nil {
o.Replies = replies
}
tag := JSONGetItems(data, "tag")
if len(tag) > 0 {
o.Tag = tag
}
o.TotalItems = uint(JSONGetInt(data, "totalItems"))
o.OrderedItems = JSONGetItems(data, "orderedItems")
o.Current = JSONGetItem(data, "current")
o.First = JSONGetItem(data, "first")
o.Last = JSONGetItem(data, "last")
o.Next = JSONGetItem(data, "next")
o.Prev = JSONGetItem(data, "prev")
o.PartOf = JSONGetItem(data, "partOf")
if si, err := jsonparser.GetInt(data, "startIndex"); err != nil {
o.StartIndex = uint(si)
}
return nil
}
// ToOrderedCollectionPage
func ToOrderedCollectionPage(it Item) (*OrderedCollectionPage, error) {
switch i := it.(type) {
case *OrderedCollectionPage:
return i, nil
case OrderedCollectionPage:
return &i, nil
}
return nil, errors.New("unable to convert to ordered collection page")
}

View file

@ -0,0 +1,120 @@
package activitystreams
import (
"reflect"
"testing"
)
func TestOrderedCollectionPageNew(t *testing.T) {
var testValue = ObjectID("test")
c := OrderedCollectionNew(testValue)
p := OrderedCollectionPageNew(c)
if reflect.DeepEqual(p, c) {
t.Errorf("Invalid ordered collection parent '%v'", p.PartOf)
}
if p.PartOf != c.GetLink() {
t.Errorf("Invalid collection '%v'", p.PartOf)
}
}
func TestOrderedCollectionPage_UnmarshalJSON(t *testing.T) {
p := OrderedCollectionPage{}
dataEmpty := []byte("{}")
p.UnmarshalJSON(dataEmpty)
if p.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID)
}
if p.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", p.Type)
}
if p.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo)
}
if len(p.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name)
}
if len(p.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary)
}
if len(p.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content)
}
if p.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.OrderedItems) > 0 {
t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", p.OrderedItems)
}
if p.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL)
}
if !p.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published)
}
if !p.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime)
}
if !p.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated)
}
if p.PartOf != nil {
t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf)
}
if p.Current != nil {
t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current)
}
if p.First != nil {
t.Errorf("Unmarshaled object should have empty First, received %q", p.First)
}
if p.Last != nil {
t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last)
}
if p.Next != nil {
t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next)
}
if p.Prev != nil {
t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev)
}
}
func TestOrderedCollectionPage_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("OrderedCollection page should point to OrderedCollection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("OrderedCollection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestOrderedCollectionPage_Collection(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
if !reflect.DeepEqual(p.Collection(), p.OrderedItems) {
t.Errorf("Collection items should be equal %v %v", p.Collection(), p.OrderedItems)
}
}
func TestToOrderedCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollectionPage_Contains(t *testing.T) {
t.Skipf("TODO")
}

218
ordered_collection_test.go Normal file
View file

@ -0,0 +1,218 @@
package activitystreams
import (
"reflect"
"testing"
)
func TestOrderedCollectionNew(t *testing.T) {
var testValue = ObjectID("test")
c := OrderedCollectionNew(testValue)
if c.ID != testValue {
t.Errorf("APObject Id '%v' different than expected '%v'", c.ID, testValue)
}
if c.Type != OrderedCollectionType {
t.Errorf("APObject Type '%v' different than expected '%v'", c.Type, OrderedCollectionType)
}
}
func Test_OrderedCollection_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
c.Append(val)
if c.Count() != 1 {
t.Errorf("Inbox collection of %q should have one element", *c.GetID())
}
if !reflect.DeepEqual(c.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestOrderedCollection_Append(t *testing.T) {
id := ObjectID("test")
val := Object{ID: ObjectID("grrr")}
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
p.Append(val)
if p.PartOf != c.GetLink() {
t.Errorf("Ordereed collection page should point to ordered collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Ordered collection page of %q should have exactly one element", *p.GetID())
}
if !reflect.DeepEqual(p.OrderedItems[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
func TestOrderedCollection_Collection(t *testing.T) {
id := ObjectID("test")
o := OrderedCollectionNew(id)
if !reflect.DeepEqual(o.Collection(), o.OrderedItems) {
t.Errorf("Collection items should be equal %v %v", o.Collection(), o.OrderedItems)
}
}
func TestOrderedCollection_GetID(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if *c.GetID() != id {
t.Errorf("GetID should return %q, received %q", id, *c.GetID())
}
}
func TestOrderedCollection_GetLink(t *testing.T) {
id := ObjectID("test")
link := IRI(id)
c := OrderedCollectionNew(id)
if c.GetLink() != link {
t.Errorf("GetLink should return %q, received %q", link, c.GetLink())
}
}
func TestOrderedCollection_GetType(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.GetType() != OrderedCollectionType {
t.Errorf("OrderedCollection Type should be %q, received %q", OrderedCollectionType, c.GetType())
}
}
func TestOrderedCollection_IsLink(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.IsLink() != false {
t.Errorf("OrderedCollection should not be a link, received %t", c.IsLink())
}
}
func TestOrderedCollection_IsObject(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.IsObject() != true {
t.Errorf("OrderedCollection should be an object, received %t", c.IsObject())
}
}
func TestOrderedCollection_UnmarshalJSON(t *testing.T) {
c := OrderedCollection{}
dataEmpty := []byte("{}")
c.UnmarshalJSON(dataEmpty)
if c.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", c.ID)
}
if c.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", c.Type)
}
if c.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", c.AttributedTo)
}
if len(c.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", c.Name)
}
if len(c.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", c.Summary)
}
if len(c.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", c.Content)
}
if c.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", c.TotalItems)
}
if len(c.OrderedItems) > 0 {
t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", c.OrderedItems)
}
if c.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", c.URL)
}
if !c.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", c.Published)
}
if !c.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", c.StartTime)
}
if !c.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", c.Updated)
}
}
func TestOrderedCollection_Count(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if len(c.OrderedItems) > 0 {
t.Errorf("Empty object should have empty Items, received %v", c.OrderedItems)
}
if c.Count() != uint(len(c.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems))
}
c.Append(IRI("test"))
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if c.Count() != uint(len(c.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems))
}
}
func TestOrderedCollectionPage_Count(t *testing.T) {
id := ObjectID("test")
c := OrderedCollectionNew(id)
p := OrderedCollectionPageNew(c)
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if len(p.OrderedItems) > 0 {
t.Errorf("Empty object should have empty Items, received %v", p.OrderedItems)
}
if p.Count() != uint(len(p.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems))
}
p.Append(IRI("test"))
if p.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems)
}
if p.Count() != uint(len(p.OrderedItems)) {
t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems))
}
}
func TestToOrderedCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollection_Contains(t *testing.T) {
t.Skipf("TODO")
}

View file

@ -273,10 +273,8 @@ var allTests = tests{
Type: a.ObjectType,
ID: a.ObjectID("http://www.test.example/object/1"),
Replies: &a.Collection{
Parent: a.Parent{
ID: a.ObjectID("http://www.test.example/object/1/replies"),
Type: a.CollectionType,
},
ID: a.ObjectID("http://www.test.example/object/1/replies"),
Type: a.CollectionType,
TotalItems: 1,
Items: a.ItemCollection{
&a.Object{
@ -320,11 +318,9 @@ var allTests = tests{
expected: true,
blank: &a.OrderedCollection{},
result: &a.OrderedCollection{
Parent: a.Parent{
ID: a.ObjectID("http://example.com/outbox"),
Type: a.OrderedCollectionType,
URL: a.IRI("http://example.com/outbox"),
},
ID: a.ObjectID("http://example.com/outbox"),
Type: a.OrderedCollectionType,
URL: a.IRI("http://example.com/outbox"),
TotalItems: 1,
OrderedItems: a.ItemCollection{
&a.Object{
@ -345,29 +341,25 @@ var allTests = tests{
expected: true,
blank: &a.OrderedCollectionPage{},
result: &a.OrderedCollectionPage{
PartOf: a.IRI("http://example.com/outbox"),
Next: a.IRI("http://example.com/outbox?page=3"),
Prev: a.IRI("http://example.com/outbox?page=1"),
OrderedCollection: a.OrderedCollection{
Parent: a.Parent{
ID: a.ObjectID("http://example.com/outbox?page=2"),
Type: a.OrderedCollectionPageType,
URL: a.IRI("http://example.com/outbox?page=2"),
},
Current: a.IRI("http://example.com/outbox?page=2"),
TotalItems: 1,
OrderedItems: a.ItemCollection{
&a.Object{
ID: a.ObjectID("http://example.com/outbox/53c6fb47"),
Type: a.ArticleType,
Name: a.NaturalLanguageValues{{a.NilLangRef, "Example title"}},
Content: a.NaturalLanguageValues{{a.NilLangRef, "Example content!"}},
URL: a.IRI("http://example.com/53c6fb47"),
MediaType: a.MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: a.IRI("http://example.com"),
AttributedTo: a.IRI("http://example.com/accounts/alice"),
},
PartOf: a.IRI("http://example.com/outbox"),
Next: a.IRI("http://example.com/outbox?page=3"),
Prev: a.IRI("http://example.com/outbox?page=1"),
ID: a.ObjectID("http://example.com/outbox?page=2"),
Type: a.OrderedCollectionPageType,
URL: a.IRI("http://example.com/outbox?page=2"),
Current: a.IRI("http://example.com/outbox?page=2"),
TotalItems: 1,
OrderedItems: a.ItemCollection{
&a.Object{
ID: a.ObjectID("http://example.com/outbox/53c6fb47"),
Type: a.ArticleType,
Name: a.NaturalLanguageValues{{a.NilLangRef, "Example title"}},
Content: a.NaturalLanguageValues{{a.NilLangRef, "Example content!"}},
URL: a.IRI("http://example.com/53c6fb47"),
MediaType: a.MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: a.IRI("http://example.com"),
AttributedTo: a.IRI("http://example.com/accounts/alice"),
},
},
},

View file

@ -325,13 +325,13 @@ func JSONGetItemByType(typ ActivityVocabularyType) (Item, error) {
case ActorType:
return ObjectNew(typ), nil
case CollectionType:
return &Collection{Parent: Parent{Type: typ}}, nil
return &Collection{Type: typ}, nil
case OrderedCollectionType:
return &OrderedCollection{Parent: Parent{Type: typ}}, nil
return &OrderedCollection{Type: typ}, nil
case CollectionPageType:
return &CollectionPage{ParentCollection: ParentCollection{Parent: Parent{Type: typ}}}, nil
return &CollectionPage{Type: typ}, nil
case OrderedCollectionPageType:
return &OrderedCollectionPage{OrderedCollection: OrderedCollection{Parent: Parent{Type: typ}}}, nil
return &OrderedCollectionPage{Type: typ}, nil
case ArticleType:
return ObjectNew(typ), nil
case AudioType: