Skip to content

Commit

Permalink
Merge pull request #317 from mimiro-io/fix/entity-equality
Browse files Browse the repository at this point in the history
handle nil values in equality check
  • Loading branch information
rompetroll authored Aug 19, 2024
2 parents ba0b77b + 828c5d5 commit 260bea1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/server/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ func toJsonValue(value any) (any, reflect.Type) {
case []map[string]any:
return v, vt.Type()
default:
return v, vt.Type()
if vt.IsValid() {
return v, vt.Type()
}
return v, reflect.TypeOf(0)
}
}
func IsEntityEqual(prevJson []byte, thisJson []byte, prevEntity *Entity, thisEntity *Entity) bool {
Expand Down
26 changes: 26 additions & 0 deletions internal/server/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ var _ = ginkgo.Describe("the IsEntityEqual function",
})
Expect(IsEntityEqual(prevEntityJson, newEntityJson, prevEntity, newEntity)).To(BeFalse())
})
ginkgo.It("Should return true: nil values", func() {
newEntity, prevEntity, newEntityJson, prevEntityJson := newEntities(func(newEntity *Entity, prevEntity *Entity) {
prevEntity.Properties["ns0:prop1"] = nil
newEntity.Properties["ns0:prop1"] = nil
})
Expect(IsEntityEqual(prevEntityJson, newEntityJson, prevEntity, newEntity)).To(BeTrue())
newEntity, prevEntity, newEntityJson, prevEntityJson = newEntities(func(newEntity *Entity, prevEntity *Entity) {
prevEntity.Properties["ns0:prop1"] = []any{nil}
newEntity.Properties["ns0:prop1"] = []any{nil}
})
Expect(IsEntityEqual(prevEntityJson, newEntityJson, prevEntity, newEntity)).To(BeTrue())
})
ginkgo.It("Should return false: nil to int", func() {
newEntity, prevEntity, newEntityJson, prevEntityJson := newEntities(func(newEntity *Entity, prevEntity *Entity) {
prevEntity.Properties["ns0:prop1"] = nil
newEntity.Properties["ns0:prop1"] = 8
})
Expect(IsEntityEqual(prevEntityJson, newEntityJson, prevEntity, newEntity)).To(BeFalse())
})
ginkgo.It("Should return false: int to nil", func() {
newEntity, prevEntity, newEntityJson, prevEntityJson := newEntities(func(newEntity *Entity, prevEntity *Entity) {
prevEntity.Properties["ns0:prop1"] = 8
newEntity.Properties["ns0:prop1"] = nil
})
Expect(IsEntityEqual(prevEntityJson, newEntityJson, prevEntity, newEntity)).To(BeFalse())
})
})

func BenchmarkIsEntityEqual(b *testing.B) {
Expand Down

0 comments on commit 260bea1

Please sign in to comment.