Adding a Count() method to the CollectionInterface

This commit is contained in:
Marius Orcsik 2019-06-02 16:45:37 +02:00
parent 3cde24f465
commit c6deab0d6b
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
6 changed files with 86 additions and 17 deletions

View file

@ -15,6 +15,7 @@ type CollectionInterface interface {
ObjectOrLink
Collection() CollectionInterface
Append(ob Item) error
Count() uint
}
// Collection is a subtype of Activity Pub Object that represents ordered or unordered sets of Activity Pub Object or Link instances.
@ -129,24 +130,56 @@ func (o *OrderedCollection) Append(ob Item) error {
return nil
}
// 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 a Collection
func (c *Collection) Append(ob Item) error {
c.Items = append(c.Items, ob)
return nil
}
// Count returns the maximum between the length of Items in collection and its TotalItems property
func (c *Collection) Count() uint {
if c.TotalItems > 0 {
return c.TotalItems
}
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
}
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
}
return uint(len(c.Items))
}
// GetType returns the Collection's type
func (c Collection) GetType() ActivityVocabularyType {
return c.Type

View file

@ -65,9 +65,9 @@ func Test_OrderedCollection_Append(t *testing.T) {
c := OrderedCollectionNew(id)
c.Append(val)
//if c.TotalItems != 1 {
// t.Errorf("Inbox collection of %q should have one element", *c.GetID())
//}
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)
}
@ -81,9 +81,9 @@ func TestCollection_Append(t *testing.T) {
c := CollectionNew(id)
c.Append(val)
//if c.TotalItems != 1 {
// t.Errorf("Inbox collection of %q should have one element", *c.GetID())
//}
if c.Count() != 1 {
t.Errorf("Inbox collection of %q should have one element", *c.GetID())
}
if !reflect.DeepEqual(c.Items[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
@ -102,9 +102,9 @@ func TestCollectionPage_Append(t *testing.T) {
if p.PartOf != c.GetLink() {
t.Errorf("Collection page should point to collection %q", c.GetLink())
}
//if p.TotalItems != 1 {
// t.Errorf("Collection page of %q should have exactly one element", *p.GetID())
//}
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)
}
@ -288,9 +288,9 @@ func TestOrderedCollection_Append(t *testing.T) {
if p.PartOf != c.GetLink() {
t.Errorf("Ordereed collection page should point to ordered collection %q", c.GetLink())
}
//if p.TotalItems != 1 {
// t.Errorf("Ordered collection page of %q should have exactly one element", *p.GetID())
//}
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)
}
@ -474,7 +474,9 @@ func TestOrderedCollectionPage_Append(t *testing.T) {
if p.PartOf != c.GetLink() {
t.Errorf("OrderedCollection page should point to OrderedCollection %q", c.GetLink())
}
//a
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)
}
@ -484,6 +486,18 @@ func TestOrderedCollectionPage_Collection(t *testing.T) {
t.Skipf("TODO")
}
func TestFlattenItemCollection(t *testing.T) {
func TestCollection_Count(t *testing.T) {
t.Skipf("TODO")
}
func TestCollectionPage_Count(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollection_Count(t *testing.T) {
t.Skipf("TODO")
}
func TestOrderedCollectionPage_Count(t *testing.T) {
t.Skipf("TODO")
}

View file

@ -44,6 +44,11 @@ func (i *ItemCollection) Append(o Item) error {
return nil
}
// Count returns the length of Items in the item collection
func (i *ItemCollection) Count() uint {
return uint(len(*i))
}
// First returns the ObjectID corresponding to ItemCollection
func (i ItemCollection) First() Item {
if len(i) == 0 {

View file

@ -33,3 +33,11 @@ func TestItemCollection_IsObject(t *testing.T) {
func TestItemCollection_First(t *testing.T) {
t.Skipf("TODO")
}
func TestFlattenItemCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestItemCollection_Count(t *testing.T) {
t.Skipf("TODO")
}

View file

@ -225,6 +225,11 @@ func (n *NaturalLanguageValues) Append(lang LangRef, value string) error {
return nil
}
// Count returns the length of Items in the item collection
func (n *NaturalLanguageValues) Count() uint {
return uint(len(*n))
}
// UnmarshalJSON tries to load the NaturalLanguage array from the incoming json value
func (l *LangRef) UnmarshalJSON(data []byte) error {
return l.UnmarshalText(data)

View file

@ -425,9 +425,9 @@ func TestNaturalLanguageValue_UnmarshalFullObjectJSON(t *testing.T) {
"` + langDe + `": "` + valDe + `"
}`
var a NaturalLanguageValues
a.Append(LangRef(langEn), valEn)
a.Append(LangRef(langDe), valDe)
a := make(NaturalLanguageValues, 0)
_ = a.Append(LangRef(langEn), valEn)
_ = a.Append(LangRef(langDe), valDe)
err := a.UnmarshalJSON([]byte(json))
if err != nil {
t.Error(err)
@ -715,3 +715,7 @@ func TestRelationship_Recipients(t *testing.T) {
func TestTombstone_Recipients(t *testing.T) {
t.Skipf("TODO")
}
func TestNaturalLanguageValues_Count(t *testing.T) {
t.Skipf("TODO")
}