bine/control/keyval.go

41 lines
1.2 KiB
Go
Raw Permalink Normal View History

2018-05-11 19:43:58 +00:00
package control
2018-05-14 20:36:29 +00:00
// KeyVal is a simple key-value struct. In cases where Val can be nil, an empty
// string represents that unless ValSetAndEmpty is true.
2018-05-11 19:43:58 +00:00
type KeyVal struct {
2018-05-14 19:47:05 +00:00
// Key is the always-present key
2018-05-11 19:43:58 +00:00
Key string
2018-05-14 19:47:05 +00:00
2018-05-14 20:36:29 +00:00
// Val is the value. If it's an empty string and nils are accepted/supported
// where this is used, it means nil unless ValSetAndEmpty is true.
2018-05-11 19:43:58 +00:00
Val string
2018-05-14 19:47:05 +00:00
2018-05-14 20:36:29 +00:00
// ValSetAndEmpty is true when Val is an empty string, the associated
// command supports nils, and Val should NOT be treated as nil. False
// otherwise.
2018-05-11 19:43:58 +00:00
ValSetAndEmpty bool
}
2018-05-14 19:47:05 +00:00
// NewKeyVal creates a new key-value pair.
2018-05-11 19:43:58 +00:00
func NewKeyVal(key string, val string) *KeyVal {
return &KeyVal{Key: key, Val: val}
}
2018-05-14 20:36:29 +00:00
// KeyVals creates multiple new key-value pairs from the given strings. The
// provided set of strings must have a length that is a multiple of 2.
2018-05-11 19:43:58 +00:00
func KeyVals(keysAndVals ...string) []*KeyVal {
if len(keysAndVals)%2 != 0 {
panic("Expected multiple of 2")
}
ret := make([]*KeyVal, len(keysAndVals)/2)
for i := 0; i < len(ret); i++ {
ret[i] = NewKeyVal(keysAndVals[i*2], keysAndVals[i*2+1])
}
return ret
}
2018-05-14 19:47:05 +00:00
// ValSet returns true if Val is either non empty or ValSetAndEmpty is true.
2018-05-11 19:43:58 +00:00
func (k *KeyVal) ValSet() bool {
return len(k.Val) > 0 || k.ValSetAndEmpty
}