Skip to content

Commit

Permalink
fix: fix kv equality check for empty values
Browse files Browse the repository at this point in the history
We did not distinguish between an entry with an empty value vs a non-existent key. This caused resource labels/annotations to not get updated in some cases.

Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir committed Dec 19, 2024
1 parent ce09295 commit eea1d62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
17 changes: 1 addition & 16 deletions pkg/resource/internal/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,7 @@ func (kv *KV) Raw() map[string]string {

// Equal checks kv for equality.
func (kv KV) Equal(other KV) bool {
// shortcut for common case of having no keys
if kv.m == nil && other.m == nil {
return true
}

if len(kv.m) != len(other.m) {
return false
}

for k, v := range kv.m {
if v != other.m[k] {
return false
}
}

return true
return maps.Equal(kv.m, other.m)
}

// Empty if there are no pairs.
Expand Down
24 changes: 24 additions & 0 deletions pkg/resource/internal/kv/kv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package kv_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/cosi-project/runtime/pkg/resource/internal/kv"
)

func TestEqualSameLenEmptyValue(t *testing.T) {
var kv1, kv2 kv.KV

kv1.Set("a", "")
kv2.Set("b", "")

equal := kv1.Equal(kv2)

assert.False(t, equal, "Expected kv1 and kv2 to be not equal")
}

0 comments on commit eea1d62

Please sign in to comment.