From 2af030ab57d1d84ac9a1d22552dc9d83b16951c4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 9 May 2014 17:01:27 -0700 Subject: [PATCH] beam/data: Message.GetOne() returns the last value set at a key This is a convenience for callers which are only interested in one value per key. Similar to how HTTP headers allow multiple keys per value, but are often used to store and retrieve only one value. Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- pkg/beam/data/message.go | 10 ++++++++++ pkg/beam/data/message_test.go | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/beam/data/message.go b/pkg/beam/data/message.go index 193fb7b241..0ebe90295a 100644 --- a/pkg/beam/data/message.go +++ b/pkg/beam/data/message.go @@ -72,6 +72,16 @@ func (m Message) Get(k string) []string { return v } +// GetOne returns the last value added at the key k, +// or an empty string if there is no value. +func (m Message) GetOne(k string) string { + var v string + if vals := m.Get(k); len(vals) > 0 { + v = vals[len(vals)-1] + } + return v +} + func (m Message) Pretty() string { data, err := Decode(string(m)) if err != nil { diff --git a/pkg/beam/data/message_test.go b/pkg/beam/data/message_test.go index 7685769069..7224f33d11 100644 --- a/pkg/beam/data/message_test.go +++ b/pkg/beam/data/message_test.go @@ -51,3 +51,11 @@ func TestSetDelMessage(t *testing.T) { t.Fatalf("'%v' != '%v'", output, expectedOutput) } } + +func TestGetOne(t *testing.T) { + m := Empty().Set("shadok words", "ga", "bu", "zo", "meu") + val := m.GetOne("shadok words") + if val != "meu" { + t.Fatalf("%#v", val) + } +}