diff --git a/activity.go b/activity.go index e7a9643..b764e0c 100644 --- a/activity.go +++ b/activity.go @@ -400,8 +400,7 @@ func (a *Activity) Recipients() ItemCollection { if len(alwaysRemove) > 0 { removeFromAudience(a, alwaysRemove...) } - rec, _ := ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) - return rec + return ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) } // Clean removes Bto and BCC properties diff --git a/actor.go b/actor.go index 7a0173a..167cc42 100644 --- a/actor.go +++ b/actor.go @@ -306,8 +306,7 @@ func ServiceNew(id ID) *Service { } func (a *Actor) Recipients() ItemCollection { - rec, _ := ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) - return rec + return ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) } func (a *Actor) Clean() { diff --git a/intransitive_activity.go b/intransitive_activity.go index 6c38587..03e3fff 100644 --- a/intransitive_activity.go +++ b/intransitive_activity.go @@ -135,10 +135,7 @@ type ( // Recipients performs recipient de-duplication on the Activity's To, Bto, CC and BCC properties func (i *IntransitiveActivity) Recipients() ItemCollection { - actor := make(ItemCollection, 0) - actor.Append(i.Actor) - rec, _ := ItemCollectionDeduplication(&actor, &i.To, &i.Bto, &i.CC, &i.BCC, &i.Audience) - return rec + return ItemCollectionDeduplication(&ItemCollection{i.Actor}, &i.To, &i.Bto, &i.CC, &i.BCC, &i.Audience) } // Clean removes Bto and BCC properties diff --git a/item_collection.go b/item_collection.go index b126004..6de23f3 100644 --- a/item_collection.go +++ b/item_collection.go @@ -89,7 +89,7 @@ func (i ItemCollection) Contains(r Item) bool { } // ItemCollectionDeduplication normalizes the received arguments lists into a single unified one -func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, error) { +func ItemCollectionDeduplication(recCols ...*ItemCollection) ItemCollection { rec := make(ItemCollection, 0) for _, recCol := range recCols { @@ -105,14 +105,14 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er } var testIt IRI if cur.IsObject() { - testIt = IRI(cur.GetID()) + testIt = cur.GetID() } else if cur.IsLink() { testIt = cur.GetLink() } else { continue } for _, it := range rec { - if testIt.Equals(IRI(it.GetID()), false) { + if testIt.Equals(it.GetID(), false) { // mark the element for removal toRemove = append(toRemove, i) save = false @@ -128,7 +128,7 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er *recCol = append((*recCol)[:idx], (*recCol)[idx+1:]...) } } - return rec, nil + return rec } // FlattenItemCollection flattens the Collection's properties from Object type to IRI diff --git a/natural_language_values_test.go b/natural_language_values_test.go index a4ca24f..7b595f1 100644 --- a/natural_language_values_test.go +++ b/natural_language_values_test.go @@ -477,9 +477,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) { want bool }{ { - name: "equal-key-value", n: NaturalLanguageValues{LangRefValue{ - Ref: "en", - Value: "test123#", + name: "equal-key-value", + n: NaturalLanguageValues{LangRefValue{ + Ref: "en", + Value: "test123#", }}, args: args{ with: NaturalLanguageValues{LangRefValue{ @@ -490,9 +491,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) { want: true, }, { - name: "not-equal-key", n: NaturalLanguageValues{LangRefValue{ - Ref: "en", - Value: "test123#", + name: "not-equal-key", + n: NaturalLanguageValues{LangRefValue{ + Ref: "en", + Value: "test123#", }}, args: args{ with: NaturalLanguageValues{LangRefValue{ @@ -503,9 +505,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) { want: false, }, { - name: "not-equal-value", n: NaturalLanguageValues{LangRefValue{ - Ref: "en", - Value: "test123#", + name: "not-equal-value", + n: NaturalLanguageValues{LangRefValue{ + Ref: "en", + Value: "test123#", }}, args: args{ with: NaturalLanguageValues{LangRefValue{ diff --git a/object.go b/object.go index 10fbcd3..61b2488 100644 --- a/object.go +++ b/object.go @@ -278,8 +278,7 @@ func (o Object) MarshalJSON() ([]byte, error) { // Recipients performs recipient de-duplication on the Object's To, Bto, CC and BCC properties func (o *Object) Recipients() ItemCollection { var aud ItemCollection - rec, _ := ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience) - return rec + return ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience) } // Clean removes Bto and BCC properties @@ -569,6 +568,10 @@ func (o Object) Equals(with Item) bool { } } if w.URL != nil { + if o.URL == nil { + result = false + return nil + } if !w.URL.GetLink().Equals(o.URL.GetLink(), false) { result = false return nil diff --git a/object_test.go b/object_test.go index d846d1c..63bb1d7 100644 --- a/object_test.go +++ b/object_test.go @@ -272,11 +272,6 @@ func TestRecipients(t *testing.T) { if len(second) != 0 { t.Errorf("Second Objects array should have exactly 0(zero) elements, not %d", len(second)) } - - _, err := ItemCollectionDeduplication(&first, &second, nil) - if err != nil { - t.Errorf("Deduplication with empty array failed") - } } func validateEmptyObject(o Object, t *testing.T) { @@ -751,16 +746,16 @@ func TestSource_MarshalJSON(t *testing.T) { wantErr: false, }, { - name: "MediaType", - fields: fields{ + name: "MediaType", + fields: fields{ MediaType: MimeType("blank"), }, want: []byte(`{"mediaType":"blank"}`), wantErr: false, }, { - name: "OneContentValue", - fields: fields{ + name: "OneContentValue", + fields: fields{ Content: NaturalLanguageValues{ {Value: "test"}, }, @@ -769,8 +764,8 @@ func TestSource_MarshalJSON(t *testing.T) { wantErr: false, }, { - name: "MultipleContentValues", - fields: fields{ + name: "MultipleContentValues", + fields: fields{ Content: NaturalLanguageValues{ { Ref: "en", @@ -803,3 +798,55 @@ func TestSource_MarshalJSON(t *testing.T) { }) } } + +func TestObject_Equals(t *testing.T) { + type args struct { + with Object + } + tests := []struct { + name string + o Object + args args + want bool + }{ + { + name: "equal-empty-object", + o: Object{}, + args: args{ + with: Object{}, + }, + want: true, + }, + { + name: "equal-object-just-id", + o: Object{ID: "test"}, + args: args{ + with: Object{ID: "test"}, + }, + want: true, + }, + { + name: "equal-object-id", + o: Object{ID: "test", URL: IRI("example.com")}, + args: args{ + with: Object{ID: "test"}, + }, + want: true, + }, + { + name: "equal-false-with-id-and-url", + o: Object{ID: "test"}, + args: args{ + with: Object{ID: "test", URL: IRI("example.com")}, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.o.Equals(tt.args.with); got != tt.want { + t.Errorf("Equals() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/place.go b/place.go index a8bf115..6028f9f 100644 --- a/place.go +++ b/place.go @@ -192,9 +192,7 @@ func (p Place) MarshalJSON() ([]byte, error) { // Recipients performs recipient de-duplication on the Place object's To, Bto, CC and BCC properties func (p *Place) Recipients() ItemCollection { - var aud ItemCollection - rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience) - return rec + return ItemCollectionDeduplication(&p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience) } // Clean removes Bto and BCC properties diff --git a/profile.go b/profile.go index 17963b5..26398b4 100644 --- a/profile.go +++ b/profile.go @@ -164,9 +164,7 @@ func (p Profile) MarshalJSON() ([]byte, error) { // Recipients performs recipient de-duplication on the Profile object's To, Bto, CC and BCC properties func (p *Profile) Recipients() ItemCollection { - var aud ItemCollection - rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience) - return rec + return ItemCollectionDeduplication(&p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience) } // Clean removes Bto and BCC properties diff --git a/relationship.go b/relationship.go index d5e366b..f2eb455 100644 --- a/relationship.go +++ b/relationship.go @@ -181,9 +181,7 @@ func (r Relationship) MarshalJSON() ([]byte, error) { // Recipients performs recipient de-duplication on the Relationship object's To, Bto, CC and BCC properties func (r *Relationship) Recipients() ItemCollection { - var aud ItemCollection - rec, _ := ItemCollectionDeduplication(&aud, &r.To, &r.Bto, &r.CC, &r.BCC, &r.Audience) - return rec + return ItemCollectionDeduplication(&r.To, &r.Bto, &r.CC, &r.BCC, &r.Audience) } // Clean removes Bto and BCC properties diff --git a/tombstone.go b/tombstone.go index 88878ec..35d0364 100644 --- a/tombstone.go +++ b/tombstone.go @@ -170,9 +170,7 @@ func (t Tombstone) MarshalJSON() ([]byte, error) { // Recipients performs recipient de-duplication on the Tombstone object's To, Bto, CC and BCC properties func (t *Tombstone) Recipients() ItemCollection { - var aud ItemCollection - rec, _ := ItemCollectionDeduplication(&aud, &t.To, &t.Bto, &t.CC, &t.BCC, &t.Audience) - return rec + return ItemCollectionDeduplication(&t.To, &t.Bto, &t.CC, &t.BCC, &t.Audience) } // Clean removes Bto and BCC properties