Added IsNil function to assert to Object or ItemCollection before checking for nil values in Item interface

This commit is contained in:
mariusor 2021-02-02 14:18:16 +01:00
parent 15ac5c20c7
commit 0c3f57f637
12 changed files with 151 additions and 19 deletions

View file

@ -314,7 +314,7 @@ func (c Collection) ItemsMatch(col ...Item) bool {
// Equals
func (c Collection) Equals(with Item) bool {
if with == nil {
if IsNil(with) {
return false
}
if !with.IsCollection() {

View file

@ -308,7 +308,7 @@ func (c CollectionPage) ItemsMatch(col ...Item) bool {
// Equals
func (c CollectionPage) Equals(with Item) bool {
if with == nil {
if IsNil(with) {
return false
}
if !with.IsCollection() {

View file

@ -171,7 +171,7 @@ func itemFn(data []byte) (Item, error) {
}
}
i, err := ItemTyperFunc(typ)
if err != nil || i == nil {
if err != nil || IsNil(i) {
return nil, nil
}

View file

@ -97,7 +97,7 @@ func writeIRIProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
}
func writeItemProp(b *[]byte, n string, i Item) (notEmpty bool) {
if i == nil {
if IsNil(i) {
return notEmpty
}
if im, ok := i.(json.Marshaler); ok {

View file

@ -314,8 +314,9 @@ func notEmptyActor(a *Actor) bool {
len(a.PublicKey.PublicKeyPem) > 0)
}
// NotEmpty
func NotEmpty(i Item) bool {
if i == nil {
if IsNil(i) {
return false
}
var notEmpty bool

26
item.go
View file

@ -13,7 +13,7 @@ const (
// ItemsEqual checks if it and with Items are equal
func ItemsEqual(it, with Item) bool {
if it == nil || with == nil{
if IsNil(it) || IsNil(with) {
return with == it
}
result := true
@ -76,7 +76,8 @@ func ItemsEqual(it, with Item) bool {
// IsItemCollection returns if the current Item interface holds a Collection
func IsItemCollection(it Item) bool {
_, ok := it.(ItemCollection)
return ok
_, okP := it.(*ItemCollection)
return ok || okP
}
// IsIRI returns if the current Item interface holds an IRI
@ -94,3 +95,24 @@ func IsObject(it Item) bool {
}
return ok
}
// IsNil checks if the object matching an ObjectOrLink interface is nil
func IsNil(it Item) bool {
if it == nil {
return true
}
// This is the default if the argument can't be casted to Object, as is the case for an ItemCollection
isNil := false
if IsItemCollection(it) {
OnItemCollection(it, func(c *ItemCollection) error {
isNil = c == nil
return nil
})
} else {
OnObject(it, func(o *Object) error {
isNil = o == nil
return nil
})
}
return isNil
}

View file

@ -198,7 +198,7 @@ func (i ItemCollection) ItemsMatch(col ...Item) bool {
// Equals
func (i ItemCollection) Equals(with Item) bool {
if with == nil {
if IsNil(with) {
return false
}
if !with.IsCollection() {

View file

@ -80,3 +80,112 @@ func TestItemsEqual(t *testing.T) {
})
}
}
func TestIsNil(t *testing.T) {
type args struct {
it Item
}
var (
o *Object
col *ItemCollection
obNil Item = o
colNil Item = col
)
tests := []struct {
name string
args args
want bool
}{
{
name: "plain-nil",
args: args{
it: nil,
},
want: true,
},
{
name: "interface-nil",
args: args{
it: Item(nil),
},
want: true,
},
{
name: "object-nil",
args: args{
it: obNil,
},
want: true,
},
{
name: "collection-nil",
args: args{
it: colNil,
},
want: true,
},
{
name: "collection-not-nil",
args: args{
it: ItemCollection{},
},
want: false,
},
{
name: "object-not-nil",
args: args{
it: &Object{},
},
want: false,
},
{
name: "place-not-nil",
args: args{
it: &Place{},
},
want: false,
},
{
name: "tombstone-not-nil",
args: args{
it: &Tombstone{},
},
want: false,
},
{
name: "collection-not-nil",
args: args{
it: &Collection{},
},
want: false,
},
{
name: "activity-not-nil",
args: args{
it: &Activity{},
},
want: false,
},
{
name: "intransitive-activity-not-nil",
args: args{
it: &IntransitiveActivity{},
},
want: false,
},
{
name: "actor-not-nil",
args: args{
it: &Actor{},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsNil(tt.args.it); got != tt.want {
t.Errorf("IsNil() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -427,7 +427,7 @@ func TestToObject(t *testing.T) {
if err != nil {
t.Errorf("Error returned when calling ToObject with activity should be nil, received %s", err)
}
if a == nil {
if IsNil(a) {
t.Errorf("Invalid return by ToObject #%v, should have not been nil", a)
}
}

View file

@ -409,7 +409,7 @@ func (o OrderedCollection) ItemsMatch(col ...Item) bool {
// Equals
func (o OrderedCollection) Equals(with Item) bool {
if with == nil {
if IsNil(with) {
return false
}
if !with.IsCollection() {

View file

@ -263,7 +263,7 @@ func (o OrderedCollectionPage) ItemsMatch(col ...Item) bool {
// Equals
func (o OrderedCollectionPage) Equals(with Item) bool {
if with == nil {
if IsNil(with) {
return false
}
if !with.IsCollection() {

View file

@ -25,7 +25,7 @@ S2S Server: Activities requiring the object property
obj := pub.MentionNew("gigel")
add := pub.AddNew("https://localhost/myactivity", obj, nil)
if add.Object == nil {
if pub.IsNil(add.Object) {
t.Errorf("Missing GetID in Add activity %#v", add.Object)
}
if add.Object != obj {
@ -33,7 +33,7 @@ S2S Server: Activities requiring the object property
}
block := pub.BlockNew("https://localhost/myactivity", obj)
if block.Object == nil {
if pub.IsNil(block.Object) {
t.Errorf("Missing GetID in Add activity %#v", block.Object)
}
if block.Object != obj {
@ -49,7 +49,7 @@ S2S Server: Activities requiring the object property
}
delete := pub.DeleteNew("https://localhost/myactivity", obj)
if delete.Object == nil {
if pub.IsNil(delete.Object) {
t.Errorf("Missing GetID in Delete activity %#v", delete.Object)
}
if delete.Object != obj {
@ -57,7 +57,7 @@ S2S Server: Activities requiring the object property
}
follow := pub.FollowNew("https://localhost/myactivity", obj)
if follow.Object == nil {
if pub.IsNil(follow.Object) {
t.Errorf("Missing GetID in Follow activity %#v", follow.Object)
}
if follow.Object != obj {
@ -65,7 +65,7 @@ S2S Server: Activities requiring the object property
}
like := pub.LikeNew("https://localhost/myactivity", obj)
if like.Object == nil {
if pub.IsNil(like.Object) {
t.Errorf("Missing GetID in Like activity %#v", like.Object)
}
if like.Object != obj {
@ -73,7 +73,7 @@ S2S Server: Activities requiring the object property
}
update := pub.UpdateNew("https://localhost/myactivity", obj)
if update.Object == nil {
if pub.IsNil(update.Object) {
t.Errorf("Missing GetID in Update activity %#v", update.Object)
}
if update.Object != obj {
@ -107,7 +107,7 @@ property: Add, Remove.
target := pub.MentionNew("bar")
add := pub.AddNew("https://localhost/myactivity", obj, target)
if add.Target == nil {
if pub.IsNil(add.Target) {
t.Errorf("Missing Target in Add activity %#v", add.Target)
}
if add.Target != target {
@ -115,7 +115,7 @@ property: Add, Remove.
}
remove := pub.RemoveNew("https://localhost/myactivity", obj, target)
if remove.Target == nil {
if pub.IsNil(remove.Target) {
t.Errorf("Missing Target in Remove activity %#v", remove.Target)
}
if remove.Target != target {