| 1 | package control |
| 2 | |
| 3 | // KeyVal is a simple key-value struct. In cases where Val can be nil, an empty |
| 4 | // string represents that unless ValSetAndEmpty is true. |
| 5 | type KeyVal struct { |
| 6 | // Key is the always-present key |
| 7 | Key string |
| 8 | |
| 9 | // Val is the value. If it's an empty string and nils are accepted/supported |
| 10 | // where this is used, it means nil unless ValSetAndEmpty is true. |
| 11 | Val string |
| 12 | |
| 13 | // ValSetAndEmpty is true when Val is an empty string, the associated |
| 14 | // command supports nils, and Val should NOT be treated as nil. False |
| 15 | // otherwise. |
| 16 | ValSetAndEmpty bool |
| 17 | } |
| 18 | |
| 19 | // NewKeyVal creates a new key-value pair. |
| 20 | func NewKeyVal(key string, val string) *KeyVal { |
| 21 | return &KeyVal{Key: key, Val: val} |
| 22 | } |
| 23 | |
| 24 | // KeyVals creates multiple new key-value pairs from the given strings. The |
| 25 | // provided set of strings must have a length that is a multiple of 2. |
| 26 | func KeyVals(keysAndVals ...string) []*KeyVal { |
| 27 | if len(keysAndVals)%2 != 0 { |
| 28 | panic("Expected multiple of 2") |
| 29 | } |
| 30 | ret := make([]*KeyVal, len(keysAndVals)/2) |
| 31 | for i := 0; i < len(ret); i++ { |
| 32 | ret[i] = NewKeyVal(keysAndVals[i*2], keysAndVals[i*2+1]) |
| 33 | } |
| 34 | return ret |
| 35 | } |
| 36 | |
| 37 | // ValSet returns true if Val is either non empty or ValSetAndEmpty is true. |
| 38 | func (k *KeyVal) ValSet() bool { |
| 39 | return len(k.Val) > 0 || k.ValSetAndEmpty |
| 40 | } |