Skip to content

Commit e141b82

Browse files
committed
Added methods to handle booleans
1 parent 285f54f commit e141b82

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

properties.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,23 @@ func (m Map) Equals(other Map) bool {
293293
return reflect.DeepEqual(m, other)
294294
}
295295

296+
// GetBoolean returns true if the map contains the specified key and the value
297+
// equals to the string "true", in any other case returns false.
298+
func (m Map) GetBoolean(key string) bool {
299+
value, ok := m[key]
300+
return ok && value == "true"
301+
}
302+
303+
// SetBoolean sets the specified key to the string "true" of "false" if the value
304+
// is respectively true or false.
305+
func (m Map) SetBoolean(key string, value bool) {
306+
if value {
307+
m[key] = "true"
308+
} else {
309+
m[key] = "false"
310+
}
311+
}
312+
296313
// MergeMapsOfProperties merge the map-of-Maps (obtained from the method FirstLevelOf()) into the
297314
// target map-of-Maps.
298315
func MergeMapsOfProperties(target map[string]Map, sources ...map[string]Map) map[string]Map {

properties_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,22 @@ func TestPropertiesBroken(t *testing.T) {
152152

153153
require.Error(t, err)
154154
}
155+
156+
func TestGetSetBoolean(t *testing.T) {
157+
m := Map{
158+
"a": "true",
159+
"b": "false",
160+
"c": "hello",
161+
}
162+
m.SetBoolean("e", true)
163+
m.SetBoolean("f", false)
164+
165+
require.True(t, m.GetBoolean("a"))
166+
require.False(t, m.GetBoolean("b"))
167+
require.False(t, m.GetBoolean("c"))
168+
require.False(t, m.GetBoolean("d"))
169+
require.True(t, m.GetBoolean("e"))
170+
require.False(t, m.GetBoolean("f"))
171+
require.Equal(t, "true", m["e"])
172+
require.Equal(t, "false", m["f"])
173+
}

0 commit comments

Comments
 (0)