Modified GetID method to not return a pointer

ObjectID type has now a IsValid method
This commit is contained in:
Marius Orcsik 2019-12-03 16:26:43 +01:00
parent 23227e2552
commit f968addfe0
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
25 changed files with 67 additions and 61 deletions

View file

@ -317,8 +317,8 @@ func (a Activity) IsLink() bool {
}
// GetID returns the ObjectID corresponding to the Activity object
func (a Activity) GetID() *ObjectID {
return &a.ID
func (a Activity) GetID() ObjectID {
return a.ID
}
// GetLink returns the IRI corresponding to the Activity object

View file

@ -635,15 +635,15 @@ func TestUpdate_Recipients(t *testing.T) {
func TestActivity_GetID(t *testing.T) {
a := ActivityNew("test", ActivityType, Person{})
if *a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), *a.GetID())
if a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), a.GetID())
}
}
func TestActivity_GetIDGetType(t *testing.T) {
a := ActivityNew("test", ActivityType, Person{})
if *a.GetID() != "test" || a.GetType() != ActivityType {
t.Errorf("%T should not return an empty %T object. Received %#v", a, a.GetID(), *a.GetID())
if a.GetID() != "test" || a.GetType() != ActivityType {
t.Errorf("%T should not return an empty %T object. Received %#v", a, a.GetID(), a.GetID())
}
}
func TestActivity_IsLink(t *testing.T) {
@ -664,11 +664,11 @@ func TestActivity_IsObject(t *testing.T) {
func checkDedup(list ItemCollection, recIds *[]ObjectID) error {
for _, rec := range list {
for _, id := range *recIds {
if *rec.GetID() == id {
if rec.GetID() == id {
return fmt.Errorf("%T[%s] already stored in recipients list, Deduplication faild", rec, id)
}
}
*recIds = append(*recIds, *rec.GetID())
*recIds = append(*recIds, rec.GetID())
}
return nil
}

View file

@ -109,8 +109,8 @@ type CollectionPage struct {
}
// GetID returns the ObjectID corresponding to the CollectionPage object
func (c CollectionPage) GetID() *ObjectID {
return &c.ID
func (c CollectionPage) GetID() ObjectID {
return c.ID
}
// GetType returns the CollectionPage's type

View file

@ -32,7 +32,7 @@ func TestCollectionPage_Append(t *testing.T) {
t.Errorf("Collection page should point to collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Collection page of %q should have exactly one element", *p.GetID())
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)

View file

@ -137,8 +137,8 @@ func OrderedCollectionNew(id ObjectID) *OrderedCollection {
}
// GetID returns the ObjectID corresponding to the Collection object
func (c Collection) GetID() *ObjectID {
return &c.ID
func (c Collection) GetID() ObjectID {
return c.ID
}
// GetType returns the Collection's type

View file

@ -27,7 +27,7 @@ func TestCollection_Append(t *testing.T) {
c.Append(val)
if c.Count() != 1 {
t.Errorf("Inbox collection of %q should have one element", *c.GetID())
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)
@ -49,8 +49,8 @@ func TestCollection_GetID(t *testing.T) {
c := CollectionNew(id)
if *c.GetID() != id {
t.Errorf("GetID should return %s, received %s", id, *c.GetID())
if c.GetID() != id {
t.Errorf("GetID should return %s, received %s", id, c.GetID())
}
}

View file

@ -145,8 +145,8 @@ func (i IntransitiveActivity) IsLink() bool {
}
// GetID returns the ObjectID corresponding to the IntransitiveActivity object
func (i IntransitiveActivity) GetID() *ObjectID {
return &i.ID
func (i IntransitiveActivity) GetID() ObjectID {
return i.ID
}
// GetLink returns the IRI corresponding to the IntransitiveActivity object

View file

@ -108,14 +108,14 @@ func TestIntransitiveActivityRecipients(t *testing.T) {
func TestIntransitiveActivity_GetLink(t *testing.T) {
i := IntransitiveActivityNew("test", QuestionType)
if *i.GetID() != "test" {
if i.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", i, i, i)
}
}
func TestIntransitiveActivity_GetObject(t *testing.T) {
i := IntransitiveActivityNew("test", QuestionType)
if *i.GetID() != "test" || i.GetType() != QuestionType {
if i.GetID() != "test" || i.GetType() != QuestionType {
t.Errorf("%T should not return an empty %T object. Received %#v", i, i, i)
}
}
@ -170,8 +170,8 @@ func TestIntransitiveActivity_Recipients(t *testing.T) {
func TestIntransitiveActivity_GetID(t *testing.T) {
a := IntransitiveActivityNew("test", IntransitiveActivityType)
if *a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), *a.GetID())
if a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), a.GetID())
}
}

5
iri.go
View file

@ -36,9 +36,8 @@ func (i *IRI) UnmarshalJSON(s []byte) error {
}
// GetID
func (i IRI) GetID() *ObjectID {
o := ObjectID(i)
return &o
func (i IRI) GetID() ObjectID {
return ObjectID(i)
}
// GetType

View file

@ -20,8 +20,8 @@ func TestIRI_String(t *testing.T) {
func TestIRI_GetID(t *testing.T) {
i := IRI("http://example.com")
if id := i.GetID(); id == nil || *id != ObjectID(i) {
t.Errorf("ObjectID %q (%T) should equal %q (%T)", *id, id, i, ObjectID(i))
if id := i.GetID(); !id.IsValid() || id != ObjectID(i) {
t.Errorf("ObjectID %q (%T) should equal %q (%T)", id, id, i, ObjectID(i))
}
}

View file

@ -6,14 +6,17 @@ type ItemCollection []Item
// Item struct
type Item ObjectOrLink
const EmptyObjectID = ObjectID("")
const EmptyIRI = IRI("")
// GetID returns the ObjectID corresponding to ItemCollection
func (i ItemCollection) GetID() *ObjectID {
return nil
func (i ItemCollection) GetID() ObjectID {
return EmptyObjectID
}
// GetLink returns the empty IRI
func (i ItemCollection) GetLink() IRI {
return IRI("")
return EmptyIRI
}
// GetType returns the ItemCollection's type

View file

@ -80,8 +80,8 @@ func (l Link) IsCollection() bool {
}
// GetID returns the ObjectID corresponding to the Link object
func (l Link) GetID() *ObjectID {
return &l.ID
func (l Link) GetID() ObjectID {
return l.ID
}
// GetLink returns the IRI corresponding to the current Link

View file

@ -98,7 +98,7 @@ type (
// is currently happening, or has already happened
ActivityObject interface {
// GetID returns the dereferenceable ActivityStreams object id
GetID() *ObjectID
GetID() ObjectID
// GetType returns the ActivityStreams type
GetType() ActivityVocabularyType
}
@ -479,8 +479,8 @@ func ObjectNew(typ ActivityVocabularyType) *Object {
}
// GetID returns the ObjectID corresponding to the current Object
func (o Object) GetID() *ObjectID {
return &o.ID
func (o Object) GetID() ObjectID {
return o.ID
}
// GetLink returns the IRI corresponding to the current Object
@ -649,6 +649,10 @@ func (i *ObjectID) UnmarshalJSON(data []byte) error {
return nil
}
func (i *ObjectID) IsValid() bool {
return i != nil && len(*i) > 0
}
// UnmarshalJSON
func (c *MimeType) UnmarshalJSON(data []byte) error {
*c = MimeType(strings.Trim(string(data), "\""))

View file

@ -619,8 +619,8 @@ func TestObject_GetID(t *testing.T) {
a := Object{}
testVal := "crash$"
a.ID = ObjectID(testVal)
if string(*a.GetID()) != testVal {
t.Errorf("%T should return %q, Received %q", a.GetID, testVal, *a.GetID())
if string(a.GetID()) != testVal {
t.Errorf("%T should return %q, Received %q", a.GetID, testVal, a.GetID())
}
}

View file

@ -112,8 +112,8 @@ func (o OrderedCollection) IsLink() bool {
}
// GetID returns the ObjectID corresponding to the OrderedCollection
func (o OrderedCollection) GetID() *ObjectID {
return &o.ID
func (o OrderedCollection) GetID() ObjectID {
return o.ID
}
// GetLink returns the IRI corresponding to the OrderedCollection object

View file

@ -113,8 +113,8 @@ type OrderedCollectionPage struct {
}
// GetID returns the ObjectID corresponding to the OrderedCollectionPage object
func (o OrderedCollectionPage) GetID() *ObjectID {
return &o.ID
func (o OrderedCollectionPage) GetID() ObjectID {
return o.ID
}
// GetType returns the OrderedCollectionPage's type

View file

@ -93,7 +93,7 @@ func TestOrderedCollectionPage_Append(t *testing.T) {
t.Errorf("OrderedCollection page should point to OrderedCollection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("OrderedCollection page of %q should have exactly one element", *p.GetID())
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)

View file

@ -27,7 +27,7 @@ func Test_OrderedCollection_Append(t *testing.T) {
c.Append(val)
if c.Count() != 1 {
t.Errorf("Inbox collection of %q should have one element", *c.GetID())
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)
@ -49,7 +49,7 @@ func TestOrderedCollection_Append(t *testing.T) {
t.Errorf("Ordereed collection page should point to ordered collection %q", c.GetLink())
}
if p.Count() != 1 {
t.Errorf("Ordered collection page of %q should have exactly one element", *p.GetID())
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)
@ -71,8 +71,8 @@ func TestOrderedCollection_GetID(t *testing.T) {
c := OrderedCollectionNew(id)
if *c.GetID() != id {
t.Errorf("GetID should return %q, received %q", id, *c.GetID())
if c.GetID() != id {
t.Errorf("GetID should return %q, received %q", id, c.GetID())
}
}

View file

@ -134,8 +134,8 @@ func (p Place) GetType() ActivityVocabularyType {
}
// GetID returns the ID corresponding to the current Place
func (p Place) GetID() *ObjectID {
return &p.ID
func (p Place) GetID() ObjectID {
return p.ID
}
// UnmarshalJSON

View file

@ -120,8 +120,8 @@ func (p Profile) GetType() ActivityVocabularyType {
}
// GetID returns the ID corresponding to the current Profile
func (p Profile) GetID() *ObjectID {
return &p.ID
func (p Profile) GetID() ObjectID {
return p.ID
}
// UnmarshalJSON

View file

@ -120,8 +120,8 @@ type Question struct {
}
// GetID returns the ObjectID corresponding to the Question object
func (q Question) GetID() *ObjectID {
return &q.ID
func (q Question) GetID() ObjectID {
return q.ID
}
// GetLink returns the IRI corresponding to the Question object

View file

@ -18,8 +18,8 @@ func TestQuestionNew(t *testing.T) {
func TestQuestion_GetID(t *testing.T) {
a := QuestionNew("test")
if *a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), *a.GetID())
if a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), a.GetID())
}
}

View file

@ -130,8 +130,8 @@ func (r Relationship) GetType() ActivityVocabularyType {
}
// GetID returns the ID corresponding to the current Relationship
func (r Relationship) GetID() *ObjectID {
return &r.ID
func (r Relationship) GetID() ObjectID {
return r.ID
}
// UnmarshalJSON

View file

@ -122,8 +122,8 @@ func (t Tombstone) GetType() ActivityVocabularyType {
}
// GetID returns the ID corresponding to the current Tombstone
func (t Tombstone) GetID() *ObjectID {
return &t.ID
func (t Tombstone) GetID() ObjectID {
return t.ID
}
// UnmarshalJSON

View file

@ -19,11 +19,11 @@ var (
// ItemTyperFunc will return an instance of a struct that implements activitystreams.Item
// The default for this package is JSONGetItemByType but can be overwritten
var ItemTyperFunc TyperFunction
var ItemTyperFunc TyperFn
// TyperFunction is the type of the function which returns an activitystreams.Item struct instance
// TyperFn is the type of the function which returns an activitystreams.Item struct instance
// for a specific ActivityVocabularyType
type TyperFunction func(ActivityVocabularyType) (Item, error)
type TyperFn func(ActivityVocabularyType) (Item, error)
func JSONGetObjectID(data []byte) ObjectID {
i, err := jsonparser.GetString(data, "id")