Add Set method to Metas and tests

This commit is contained in:
Dan Jones 2026-02-01 18:18:46 -06:00
commit e9c01097a6
2 changed files with 81 additions and 3 deletions

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 {
_, 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
}