Added some OnX functions to allow code execution in the context of converting an Item to X transparent to the developer
This commit is contained in:
parent
90cf4813c9
commit
152c99eba3
|
@ -154,6 +154,10 @@ func (a *actor) UnmarshalJSON(data []byte) error {
|
|||
// ToObject
|
||||
func ToPerson(it as.Item) (*Person, error) {
|
||||
switch i := it.(type) {
|
||||
case *as.Object:
|
||||
return &Person{Parent: *i}, nil
|
||||
case as.Object:
|
||||
return &Person{Parent: i}, nil
|
||||
case *actor:
|
||||
return i, nil
|
||||
case actor:
|
||||
|
|
44
helpers.go
Normal file
44
helpers.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-ap/activitystreams"
|
||||
)
|
||||
|
||||
type withObjectFn func (*activitystreams.Object) error
|
||||
type withActivityFn func (*activitystreams.Activity) error
|
||||
type withPersonFn func (*Person) error
|
||||
|
||||
func OnObject(it activitystreams.Item, fn withObjectFn) error {
|
||||
if !activitystreams.ObjectTypes.Contains(it.GetType()) {
|
||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Object", it, it.GetType()))
|
||||
}
|
||||
ob, err := activitystreams.ToObject(it)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(ob)
|
||||
}
|
||||
|
||||
func OnActivity(it activitystreams.Item, fn withActivityFn) error {
|
||||
if !activitystreams.ActivityTypes.Contains(it.GetType()) {
|
||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Activity", it, it.GetType()))
|
||||
}
|
||||
act, err := activitystreams.ToActivity(it)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(act)
|
||||
}
|
||||
|
||||
func OnPerson(it activitystreams.Item, fn withPersonFn) error {
|
||||
if !activitystreams.ActorTypes.Contains(it.GetType()) {
|
||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Person", it, it.GetType()))
|
||||
}
|
||||
pers, err := ToPerson(it)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(pers)
|
||||
}
|
82
helpers_test.go
Normal file
82
helpers_test.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"github.com/go-ap/activitystreams"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOnObject(t *testing.T) {
|
||||
ob := activitystreams.ObjectNew(activitystreams.ArticleType)
|
||||
|
||||
err := OnObject(ob, func(o *activitystreams.Object) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
|
||||
|
||||
err = OnObject(ob, func(o *activitystreams.Object) error {
|
||||
if o.Type != ob.Type {
|
||||
t.Errorf("In function type %s different than expected, %s", o.Type, ob.Type)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnActivity(t *testing.T) {
|
||||
ob := activitystreams.ObjectNew(activitystreams.ArticleType)
|
||||
act := activitystreams.CreateNew("test", ob)
|
||||
|
||||
err := OnActivity(act, func(a *activitystreams.Activity) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
|
||||
err = OnActivity(act, func(a *activitystreams.Activity) error {
|
||||
if a.Type != act.Type {
|
||||
t.Errorf("In function type %s different than expected, %s", a.Type, act.Type)
|
||||
}
|
||||
if a.ID != act.ID {
|
||||
t.Errorf("In function ID %s different than expected, %s", a.ID, act.ID)
|
||||
}
|
||||
if a.Object != act.Object { // This is valid comparison, as they should pointers, pointing to the same object
|
||||
t.Errorf("In function object %#v different than expected, %#v", a.Object, act.Object)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnPerson(t *testing.T) {
|
||||
pers := activitystreams.PersonNew("testPerson")
|
||||
err := OnPerson(pers, func(a *Person) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
|
||||
err = OnPerson(pers, func(p *Person) error {
|
||||
if p.Type != pers.Type {
|
||||
t.Errorf("In function type %s different than expected, %s", p.Type, pers.Type)
|
||||
}
|
||||
if p.ID != pers.ID {
|
||||
t.Errorf("In function ID %s different than expected, %s", p.ID, pers.ID)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error returned %s", err)
|
||||
}
|
||||
}
|
Reference in a new issue