Improvements to how we set the typer function for Unmarshalling

This commit is contained in:
Marius Orcsik 2019-05-18 23:13:00 +02:00
parent 8ea6a5ef72
commit 706be92cc2
No known key found for this signature in database
GPG key ID: 8218F7122969D484
9 changed files with 45 additions and 283 deletions

View file

@ -87,7 +87,7 @@ func actorNew(id as.ObjectID, typ as.ActivityVocabularyType) *actor {
typ = as.ActorType
}
a := actor{Parent: as.Object {ID: id, Type: typ}}
a := actor{Parent: as.Object{ID: id, Type: typ}}
a.Name = as.NaturalLanguageValuesNew()
a.Content = as.NaturalLanguageValuesNew()
a.Summary = as.NaturalLanguageValuesNew()
@ -138,9 +138,10 @@ func ServiceNew(id as.ObjectID) *Service {
return &o
}
func (a *actor)UnmarshalJSON(data []byte) error {
as.ItemTyperFunc = JSONGetItemByType
func (a *actor) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
a.Parent.UnmarshalJSON(data)
a.PreferredUsername = as.JSONGetNaturalLanguageField(data, "preferredUsername")
a.Followers = as.JSONGetItem(data, "followers")
@ -155,7 +156,6 @@ func (a *actor)UnmarshalJSON(data []byte) error {
return nil
}
// ToObject
func ToPerson(it as.Item) (*Person, error) {
switch i := it.(type) {

View file

@ -1,9 +1,9 @@
package activitypub
import (
as "github.com/go-ap/activitystreams"
"reflect"
"testing"
as "github.com/go-ap/activitystreams"
)
func TestActorNew(t *testing.T) {
@ -60,7 +60,7 @@ func TestGroupNew(t *testing.T) {
t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
}
if o.Type != as.GroupType {
t.Errorf("APObject Type '%v' different than expected '%v'", o.Type,as. GroupType)
t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, as.GroupType)
}
}

View file

@ -1,13 +1,12 @@
package activitypub
import (
"fmt"
as "github.com/go-ap/activitystreams"
)
type (
// FollowersCollection is a collection of followers
FollowersCollection Followers
FollowersCollection = Followers
// Followers is a Collection type
Followers as.Collection
@ -27,16 +26,6 @@ func FollowersNew() *Followers {
return &i
}
// Append adds an element to an FollowersCollection
func (f *FollowersCollection) Append(o as.Item) error {
if f == nil {
return fmt.Errorf("nil ")
}
f.Items = append(f.Items, o)
f.TotalItems++
return nil
}
// Append adds an element to an Followers
func (f *Followers) Append(ob as.Item) error {
f.Items = append(f.Items, ob)
@ -44,31 +33,6 @@ func (f *Followers) Append(ob as.Item) error {
return nil
}
// GetID returns the ObjectID corresponding to FollowersCollection
func (f FollowersCollection) GetID() *as.ObjectID {
return f.Collection().GetID()
}
// GetLink returns the IRI corresponding to the current FollowersCollection object
func (f FollowersCollection) GetLink() as.IRI {
return as.IRI(f.ID)
}
// GetType returns the FollowersCollection's type
func (f FollowersCollection) GetType() as.ActivityVocabularyType {
return f.Type
}
// IsLink returns false for an FollowersCollection object
func (f FollowersCollection) IsLink() bool {
return false
}
// IsObject returns true for a FollowersCollection object
func (f FollowersCollection) IsObject() bool {
return true
}
// GetID returns the ObjectID corresponding to Followers
func (f Followers) GetID() *as.ObjectID {
return f.Collection().GetID()
@ -94,18 +58,11 @@ func (f Followers) IsObject() bool {
return true
}
// UnmarshalJSON
func (f *FollowersCollection) UnmarshalJSON(data []byte) error {
c := as.Collection(*f)
err := c.UnmarshalJSON(data)
*f = FollowersCollection(c)
return err
}
// UnmarshalJSON
func (f *Followers) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
c := as.Collection(*f)
err := c.UnmarshalJSON(data)
@ -119,9 +76,3 @@ func (f Followers) Collection() as.CollectionInterface {
c := as.Collection(f)
return &c
}
// Collection returns the underlying Collection type
func (f FollowersCollection) Collection() as.CollectionInterface {
c := as.Collection(f)
return &c
}

View file

@ -1,7 +1,6 @@
package activitypub
import (
"fmt"
as "github.com/go-ap/activitystreams"
)
@ -9,7 +8,7 @@ type (
// FollowingCollection is a list of everybody that the actor has followed, added as a side effect.
// The following collection MUST be either an OrderedCollection or a Collection and MAY
// be filtered on privileges of an authenticated user or as appropriate when no authentication is given.
FollowingCollection Following
FollowingCollection = Following
// Following is a type alias for a simple Collection
Following as.Collection
@ -29,16 +28,6 @@ func FollowingNew() *Following {
return &i
}
// Append adds an element to an FollowingCollection
func (f *FollowingCollection) Append(o as.Item) error {
if f == nil {
return fmt.Errorf("nil ")
}
f.Items = append(f.Items, o)
f.TotalItems++
return nil
}
// Append adds an element to an Following
func (f *Following) Append(ob as.Item) error {
f.Items = append(f.Items, ob)
@ -46,31 +35,6 @@ func (f *Following) Append(ob as.Item) error {
return nil
}
// GetID returns the ObjectID corresponding to FollowingCollection
func (f FollowingCollection) GetID() *as.ObjectID {
return f.Collection().GetID()
}
// GetLink returns the IRI corresponding to the current FollowingCollection object
func (f FollowingCollection) GetLink() as.IRI {
return as.IRI(f.ID)
}
// GetType returns the FollowingCollection's type
func (f FollowingCollection) GetType() as.ActivityVocabularyType {
return f.Type
}
// IsLink returns false for an FollowingCollection object
func (f FollowingCollection) IsLink() bool {
return false
}
// IsObject returns true for a FollowingCollection object
func (f FollowingCollection) IsObject() bool {
return true
}
// GetID returns the ObjectID corresponding to Following
func (f Following) GetID() *as.ObjectID {
return f.Collection().GetID()
@ -96,18 +60,11 @@ func (f Following) IsObject() bool {
return true
}
// UnmarshalJSON
func (f *FollowingCollection) UnmarshalJSON(data []byte) error {
c := as.Collection(*f)
err := c.UnmarshalJSON(data)
*f = FollowingCollection(c)
return err
}
// UnmarshalJSON
func (f *Following) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
c := as.Collection(*f)
err := c.UnmarshalJSON(data)
@ -121,9 +78,3 @@ func (f Following) Collection() as.CollectionInterface {
c := as.Collection(f)
return &c
}
// Collection returns the underlying Collection type
func (f FollowingCollection) Collection() as.CollectionInterface {
c := as.Collection(f)
return &c
}

View file

@ -1,8 +1,6 @@
package activitypub
import (
"fmt"
as "github.com/go-ap/activitystreams"
)
@ -12,7 +10,7 @@ type (
// In general, the owner of an inbox is likely to be able to access all of their inbox contents.
// Depending on access control, some other content may be public, whereas other content may
// require authentication for non-owner users, if they can access the inbox at all.
InboxStream Inbox
InboxStream = Inbox
// Inbox is a type alias for an Ordered Collection
Inbox as.OrderedCollection
@ -31,16 +29,6 @@ func InboxNew() *as.OrderedCollection {
return &i
}
// Append adds an element to an InboxStream
func (i *InboxStream) Append(o as.Item) error {
if i == nil {
return fmt.Errorf("nil ")
}
i.OrderedItems = append(i.OrderedItems, o)
i.TotalItems++
return nil
}
// Append adds an element to an Inbox
func (i *Inbox) Append(ob as.Item) error {
i.OrderedItems = append(i.OrderedItems, ob)
@ -48,31 +36,6 @@ func (i *Inbox) Append(ob as.Item) error {
return nil
}
// GetID returns the ObjectID corresponding to InboxStream
func (i InboxStream) GetID() *as.ObjectID {
return i.Collection().GetID()
}
// GetLink returns the IRI corresponding to the current InboxStream object
func (i InboxStream) GetLink() as.IRI {
return as.IRI(i.ID)
}
// GetType returns the InboxStream's type
func (i InboxStream) GetType() as.ActivityVocabularyType {
return i.Type
}
// IsLink returns false for an InboxStream object
func (i InboxStream) IsLink() bool {
return false
}
// IsObject returns true for a InboxStream object
func (i InboxStream) IsObject() bool {
return true
}
// GetID returns the ObjectID corresponding to Inbox
func (i Inbox) GetID() *as.ObjectID {
return i.Collection().GetID()
@ -98,18 +61,11 @@ func (i Inbox) IsObject() bool {
return true
}
// UnmarshalJSON
func (i *InboxStream) UnmarshalJSON(data []byte) error {
c := as.OrderedCollection(*i)
err := c.UnmarshalJSON(data)
*i = InboxStream(c)
return err
}
// UnmarshalJSON
func (i *Inbox) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
c := as.OrderedCollection(*i)
err := c.UnmarshalJSON(data)
@ -123,9 +79,3 @@ func (i Inbox) Collection() as.CollectionInterface {
c := as.OrderedCollection(i)
return &c
}
// Collection returns the underlying Collection type
func (i InboxStream) Collection() as.CollectionInterface {
c := as.OrderedCollection(i)
return &c
}

View file

@ -47,6 +47,9 @@ func (s *Source) UnmarshalJSON(data []byte) error {
// UnmarshalJSON
func (o *Object) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
o.Parent.UnmarshalJSON(data)
o.Source = GetAPSource(data)
return nil

View file

@ -6,7 +6,7 @@ type (
// OutboxStream contains activities the user has published,
// subject to the ability of the requestor to retrieve the activity (that is,
// the contents of the outbox are filtered by the permissions of the person reading it).
OutboxStream Outbox
OutboxStream = Outbox
// Outbox is a type alias for an Ordered Collection
Outbox as.OrderedCollection
@ -25,13 +25,6 @@ func OutboxNew() *Outbox {
return &i
}
// Append adds an element to an OutboxStream
func (o *OutboxStream) Append(ob as.Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
o.TotalItems++
return nil
}
// Append adds an element to an Outbox
func (o *Outbox) Append(ob as.Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
@ -39,31 +32,6 @@ func (o *Outbox) Append(ob as.Item) error {
return nil
}
// GetID returns the ObjectID corresponding to the OutboxStream
func (o OutboxStream) GetID() *as.ObjectID {
return o.Collection().GetID()
}
// GetLink returns the IRI corresponding to the current OutboxStream object
func (o OutboxStream) GetLink() as.IRI {
return as.IRI(o.ID)
}
// GetType returns the OutboxStream's type
func (o OutboxStream) GetType() as.ActivityVocabularyType {
return o.Type
}
// IsLink returns false for an OutboxStream object
func (o OutboxStream) IsLink() bool {
return false
}
// IsObject returns true for a OutboxStream object
func (o OutboxStream) IsObject() bool {
return true
}
// GetID returns the ObjectID corresponding to Outbox
func (o Outbox) GetID() *as.ObjectID {
return o.Collection().GetID()
@ -89,16 +57,6 @@ func (o Outbox) IsObject() bool {
return true
}
// UnmarshalJSON
func (o *OutboxStream) UnmarshalJSON(data []byte) error {
c := as.OrderedCollection(*o)
err := c.UnmarshalJSON(data)
*o = OutboxStream(c)
return err
}
// UnmarshalJSON
func (o *Outbox) UnmarshalJSON(data []byte) error {
c := as.OrderedCollection(*o)
@ -114,9 +72,3 @@ func (o Outbox) Collection() as.CollectionInterface {
c := as.OrderedCollection(o)
return &c
}
// Collection returns the underlying Collection type
func (o OutboxStream) Collection() as.CollectionInterface {
c := as.OrderedCollection(o)
return &c
}

View file

@ -7,7 +7,7 @@ type (
// added as a side effect. The shares collection MUST be either an OrderedCollection or a Collection
// and MAY be filtered on privileges of an authenticated user or as appropriate when no authentication
// is given.
SharesCollection Shares
SharesCollection = Shares
// Shares is a type alias for an Ordered Collection
Shares as.OrderedCollection
@ -26,13 +26,6 @@ func SharesNew() *Shares {
return &i
}
// Append adds an element to an SharesCollection
func (o *SharesCollection) Append(ob as.Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
o.TotalItems++
return nil
}
// Append adds an element to an Shares
func (o *Shares) Append(ob as.Item) error {
o.OrderedItems = append(o.OrderedItems, ob)
@ -40,31 +33,6 @@ func (o *Shares) Append(ob as.Item) error {
return nil
}
// GetID returns the ObjectID corresponding to the SharesCollection
func (o SharesCollection) GetID() *as.ObjectID {
return o.Collection().GetID()
}
// GetLink returns the IRI corresponding to the current SharesCollection object
func (o SharesCollection) GetLink() as.IRI {
return as.IRI(o.ID)
}
// GetType returns the SharesCollection's type
func (o SharesCollection) GetType() as.ActivityVocabularyType {
return o.Type
}
// IsLink returns false for an SharesCollection object
func (o SharesCollection) IsLink() bool {
return false
}
// IsObject returns true for a SharesCollection object
func (o SharesCollection) IsObject() bool {
return true
}
// GetID returns the ObjectID corresponding to Shares
func (o Shares) GetID() *as.ObjectID {
return o.Collection().GetID()
@ -90,18 +58,11 @@ func (o Shares) IsObject() bool {
return true
}
// UnmarshalJSON
func (o *SharesCollection) UnmarshalJSON(data []byte) error {
c := as.OrderedCollection(*o)
err := c.UnmarshalJSON(data)
*o = SharesCollection(c)
return err
}
// UnmarshalJSON
func (o *Shares) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
c := as.OrderedCollection(*o)
err := c.UnmarshalJSON(data)
@ -115,9 +76,3 @@ func (o Shares) Collection() as.CollectionInterface {
c := as.OrderedCollection(o)
return &c
}
// Collection returns the underlying Collection type
func (o SharesCollection) Collection() as.CollectionInterface {
c := as.OrderedCollection(o)
return &c
}

View file

@ -10,33 +10,33 @@ func JSONGetItemByType(typ as.ActivityVocabularyType) (as.Item, error) {
switch typ {
case as.ObjectType:
o := Object{}
o := &Object{}
o.Type = typ
ret = &o
ret = o
case as.ActorType:
ret = &Object{}
o := ret.(*Object)
o := &actor{}
o.Type = typ
ret = o
case as.ApplicationType:
ret = &Application{}
o := ret.(*Application)
o.Type = typ
a := &Application{}
a.Type = typ
ret = a
case as.GroupType:
ret = &Group{}
o := ret.(*Group)
o.Type = typ
g := &Group{}
g.Type = typ
ret = g
case as.OrganizationType:
ret = &Organization{}
o := ret.(*Organization)
o := &Organization{}
o.Type = typ
ret = o
case as.PersonType:
ret = &Person{}
o := ret.(*Person)
o.Type = typ
p := &Person{}
p.Type = typ
ret = p
case as.ServiceType:
ret = &Service{}
o := ret.(*Service)
o.Type = typ
s := &Service{}
s.Type = typ
ret = s
case "":
// when no type is available use a plain Object
ret = &Object{}