From cb08613ac35fb7a91b9c3981b06086ce4a9e2302 Mon Sep 17 00:00:00 2001 From: 77 <73806583+congziqi77@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:05:13 +0800 Subject: [PATCH] fix(orderedmap): fix set bug (#252) set : when the key is present, the value of the data map should be set, not the value of the list Co-authored-by: congziqi --- maputil/orderedmap.go | 2 +- maputil/orderedmap_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/maputil/orderedmap.go b/maputil/orderedmap.go index d16e67c..fc450cd 100644 --- a/maputil/orderedmap.go +++ b/maputil/orderedmap.go @@ -35,7 +35,7 @@ func (om *OrderedMap[K, V]) Set(key K, value V) { defer om.mu.Unlock() if elem, ok := om.index[key]; ok { - elem.Value = value + om.data[key] = value om.order.MoveToBack(elem) return diff --git a/maputil/orderedmap_test.go b/maputil/orderedmap_test.go index 46ecff0..16539d1 100644 --- a/maputil/orderedmap_test.go +++ b/maputil/orderedmap_test.go @@ -19,6 +19,10 @@ func TestOrderedMap_Set_Get(t *testing.T) { assert.Equal(1, val) assert.Equal(true, ok) + om.Set("a", 2) + val, _ = om.Get("a") + assert.Equal(2, val) + val, ok = om.Get("d") assert.Equal(false, ok) assert.Equal(0, val)