Added ToX functions for Collection types

This commit is contained in:
Marius Orcsik 2019-08-21 22:32:53 +02:00
parent 585b5f048f
commit 1c9ce8dc10
No known key found for this signature in database
GPG key ID: 77618B618F79EB72
3 changed files with 69 additions and 1 deletions

2
go.mod
View file

@ -2,6 +2,6 @@ module github.com/go-ap/activitypub
require (
github.com/buger/jsonparser v0.0.0-20181023193515-52c6e1462ebd
github.com/go-ap/activitystreams v0.0.0-20190820195421-bd2f80c09306
github.com/go-ap/activitystreams v0.0.0-20190821202132-4a1645657eaa
github.com/go-ap/jsonld v0.0.0-20190630153951-3e340483bd9f
)

View file

@ -11,6 +11,10 @@ type withActivityFn func (*activitystreams.Activity) error
type withIntransitiveActivityFn func (*activitystreams.IntransitiveActivity) error
type withQuestionFn func (*activitystreams.Question) error
type withPersonFn func (*Person) error
type withCollectionFn func (*activitystreams.Collection) error
type withCollectionPageFn func (*activitystreams.CollectionPage) error
type withOrderedCollectionFn func (*activitystreams.OrderedCollection) error
type withOrderedCollectionPageFn func (*activitystreams.OrderedCollectionPage) error
// OnObject
func OnObject(it activitystreams.Item, fn withObjectFn) error {
@ -74,3 +78,51 @@ func OnPerson(it activitystreams.Item, fn withPersonFn) error {
}
return fn(pers)
}
// OnCollection
func OnCollection(it activitystreams.Item, fn withCollectionFn) error {
if it.GetType() != activitystreams.CollectionType {
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Collection", it, it.GetType()))
}
col, err := activitystreams.ToCollection(it)
if err != nil {
return err
}
return fn(col)
}
// OnCollectionPage
func OnCollectionPage(it activitystreams.Item, fn withCollectionPageFn) error {
if it.GetType() != activitystreams.CollectionPageType {
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Collection Page", it, it.GetType()))
}
col, err := activitystreams.ToCollectionPage(it)
if err != nil {
return err
}
return fn(col)
}
// OnOrderedCollection
func OnOrderedCollection(it activitystreams.Item, fn withOrderedCollectionFn) error {
if it.GetType() != activitystreams.OrderedCollectionType {
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection", it, it.GetType()))
}
col, err := activitystreams.ToOrderedCollection(it)
if err != nil {
return err
}
return fn(col)
}
// OnOrderedCollectionPage
func OnOrderedCollectionPage(it activitystreams.Item, fn withOrderedCollectionPageFn) error {
if it.GetType() != activitystreams.OrderedCollectionPageType {
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType()))
}
col, err := activitystreams.ToOrderedCollectionPage(it)
if err != nil {
return err
}
return fn(col)
}

View file

@ -129,3 +129,19 @@ func TestOnPerson(t *testing.T) {
t.Errorf("Unexpected error returned %s", err)
}
}
func TestOnCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestOnCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestOnOrderedCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestOnOrderedCollectionPage(t *testing.T) {
t.Skipf("TODO")
}