diff --git a/internal/server/entity.go b/internal/server/entity.go index 9413a9b..97e09c1 100644 --- a/internal/server/entity.go +++ b/internal/server/entity.go @@ -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 { diff --git a/internal/server/entity_test.go b/internal/server/entity_test.go index e90fd95..63becf7 100644 --- a/internal/server/entity_test.go +++ b/internal/server/entity_test.go @@ -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) {