beam/data: convenience Message type for chained manipulation
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
parent
ccdcd9ff0c
commit
dbbde543b1
2 changed files with 100 additions and 0 deletions
47
beam/data/message.go
Normal file
47
beam/data/message.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
)
|
||||
|
||||
type Message string
|
||||
|
||||
func Empty() Message {
|
||||
return Message(Encode(nil))
|
||||
}
|
||||
|
||||
func (m Message) Add(k, v string) Message {
|
||||
data, err := Decode(string(m))
|
||||
if err != nil {
|
||||
return m
|
||||
}
|
||||
if values, exists := data[k]; exists {
|
||||
data[k] = append(values, v)
|
||||
} else {
|
||||
data[k] = []string{v}
|
||||
}
|
||||
return Message(Encode(data))
|
||||
}
|
||||
|
||||
func (m Message) Set(k string, v ...string) Message {
|
||||
data, err := Decode(string(m))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return m
|
||||
}
|
||||
data[k] = v
|
||||
return Message(Encode(data))
|
||||
}
|
||||
|
||||
func (m Message) Del(k string) Message {
|
||||
data, err := Decode(string(m))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return m
|
||||
}
|
||||
delete(data, k)
|
||||
return Message(Encode(data))
|
||||
}
|
||||
|
||||
func (m Message) String() string {
|
||||
return string(m)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue