From e9c01097a6f3fd898a3ffae40013e7874bb2d57b Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 1 Feb 2026 18:18:46 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Add=20Set=20method=20to=20Metas?= =?UTF-8?q?=20and=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/meta_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ models/metas.go | 24 +++++++++++++++--- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/models/meta_test.go b/models/meta_test.go index 6fa6141..29e32a1 100644 --- a/models/meta_test.go +++ b/models/meta_test.go @@ -142,6 +142,66 @@ func TestMetasAppendTo(t *testing.T) { assert.Equal(t, Meta{"foo", 42}, (*ms)[0]) } +func TestMetasSet(t *testing.T) { + tests := []struct { + name string + initial Metas + key string + value any + expected Metas + }{ + { + name: "Set new key", + initial: Metas{}, + key: "foo", + value: 42, + expected: Metas{{"foo", 42}}, + }, + { + name: "Update existing key", + initial: Metas{{"foo", 1}}, + key: "foo", + value: 42, + expected: Metas{{"foo", 42}}, + }, + { + name: "Update existing key with different type", + initial: Metas{{"foo", "hello"}}, + key: "foo", + value: 42, + expected: Metas{{"foo", 42}}, + }, + { + name: "Set multiple new keys", + initial: Metas{}, + key: "bar", + value: true, + expected: Metas{{"bar", true}}, + }, + { + name: "Update one of multiple existing keys", + initial: Metas{{"foo", 1}, {"bar", "hello"}}, + key: "foo", + value: 42, + expected: Metas{{"foo", 42}, {"bar", "hello"}}, + }, + { + name: "Set new key when others exist", + initial: Metas{{"foo", 1}, {"bar", "hello"}}, + key: "baz", + value: false, + expected: Metas{{"foo", 1}, {"bar", "hello"}, {"baz", false}}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := tt.initial.Set(tt.key, tt.value) + assert.ElementsMatch(t, tt.expected, result) + }) + } +} + func TestMetasGet(t *testing.T) { ms := Metas{{"foo", 42}, {"bar", "hello"}} diff --git a/models/metas.go b/models/metas.go index 9ed7006..cf4c23e 100644 --- a/models/metas.go +++ b/models/metas.go @@ -72,10 +72,28 @@ func (ms *Metas) AppendTo(k string, v any) { // Returns the value of the Meta with the given key, and a bool indicating if it was found func (ms Metas) Get(key string) (any, bool) { - for _, m := range ms { + _, m, ok := ms.findVal(key) + return m.Value, ok +} + +// Set sets the key to the given value. +// If key is already in the ms, Set updates the value of the found Meta. +// If key is not in ms, then we append a new Meta and return the modified Metas. +func (ms Metas) Set(key string, value any) Metas { + idx, m, found := ms.findVal(key) + if !found { + return ms.Append(key, value) + } + m.Value = value + ms[idx] = m + return ms +} + +func (ms Metas) findVal(key string) (idx int, m Meta, found bool) { + for idx, m = range ms { if m.Key == key { - return m.Value, true + return idx, m, true } } - return nil, false + return 0, Meta{}, false } From a1add58c34f285de9d05777ca506b858770ce8cf Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 1 Feb 2026 21:14:12 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9D=20Update=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 074a24e..aa2abe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [0.0.9] - 2026-02-01 + +- ✨ Add Set method to Metas type + ## [0.0.8] - 2026-02-01 - ✨ Add Get method to Metas type