Changes
3 changed files (+87/-6)
-
-
@@ -2,6 +2,6 @@ module github.com/go-ap/activitypubrequire ( github.com/buger/jsonparser v0.0.0-20181023193515-52c6e1462ebd github.com/go-ap/activitystreams v0.0.0-20190706101500-3072014ce9d9 github.com/go-ap/activitystreams v0.0.0-20190820195421-bd2f80c09306 github.com/go-ap/jsonld v0.0.0-20190630153951-3e340483bd9f )
-
-
-
@@ -8,8 +8,11 @@ import (type withObjectFn func (*activitystreams.Object) error type withActivityFn func (*activitystreams.Activity) error type withIntransitiveActivityFn func (*activitystreams.IntransitiveActivity) error type withQuestionFn func (*activitystreams.Question) error type withPersonFn func (*Person) error // OnObject 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()))
-
@@ -21,6 +24,7 @@ func OnObject(it activitystreams.Item, fn withObjectFn) error {return fn(ob) } // OnActivity 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()))
-
@@ -32,6 +36,34 @@ func OnActivity(it activitystreams.Item, fn withActivityFn) error {return fn(act) } // OnIntransitiveActivity func OnIntransitiveActivity(it activitystreams.Item, fn withIntransitiveActivityFn) error { if !activitystreams.IntransitiveActivityTypes.Contains(it.GetType()) { return errors.New(fmt.Sprintf("%T[%s] can't be converted to Activity", it, it.GetType())) } if it.GetType() == activitystreams.QuestionType { errors.New(fmt.Sprintf("For %T[%s] you need to use OnQuestion function", it, it.GetType())) } act, err := activitystreams.ToIntransitiveActivity(it) if err != nil { return err } return fn(act) } // OnQuestion func OnQuestion(it activitystreams.Item, fn withQuestionFn) error { if it.GetType() != activitystreams.QuestionType { errors.New(fmt.Sprintf("For %T[%s] can't be converted to Question", it, it.GetType())) } act, err := activitystreams.ToQuestion(it) if err != nil { return err } return fn(act) } // OnPerson 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()))
-
-
-
@@ -11,12 +11,11 @@ func TestOnObject(t *testing.T) {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)
-
@@ -30,7 +29,7 @@ func TestOnObject(t *testing.T) {func TestOnActivity(t *testing.T) { ob := activitystreams.ObjectNew(activitystreams.ArticleType) act := activitystreams.CreateNew("test", ob) act := activitystreams.ActivityNew("test", activitystreams.CreateType, ob) err := OnActivity(act, func(a *activitystreams.Activity) error { return nil
-
@@ -47,8 +46,58 @@ func TestOnActivity(t *testing.T) {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) if a.Object != act.Object { t.Errorf("In function object %s different than expected, %s", a.Object, act.Object) } return nil }) if err != nil { t.Errorf("Unexpected error returned %s", err) } } func TestOnIntransitiveActivity(t *testing.T) { act := activitystreams.IntransitiveActivityNew("test", activitystreams.ArriveType) err := OnIntransitiveActivity(act, func(a *activitystreams.IntransitiveActivity) error { return nil }) if err != nil { t.Errorf("Unexpected error returned %s", err) } err = OnIntransitiveActivity(act, func(a *activitystreams.IntransitiveActivity) 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) } return nil }) if err != nil { t.Errorf("Unexpected error returned %s", err) } } func TestOnQuestion(t *testing.T) { act := activitystreams.QuestionNew("test") err := OnQuestion(act, func(a *activitystreams.Question) error { return nil }) if err != nil { t.Errorf("Unexpected error returned %s", err) } err = OnQuestion(act, func(a *activitystreams.Question) 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) } return nil })
-