🔀 Merge branch 'rel/0.0.9' into stable

This commit is contained in:
Dan Jones 2026-02-01 21:14:59 -06:00
commit 26da173dc7
3 changed files with 85 additions and 3 deletions

View file

@ -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

View file

@ -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"}}

View file

@ -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 {
if m.Key == key {
return m.Value, true
}
}
return nil, false
_, 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 idx, m, true
}
}
return 0, Meta{}, false
}