Skip to content

Commit

Permalink
feat(settings): get type functions (#62)
Browse files Browse the repository at this point in the history
* feat(settings): get type functions

* test: ensure full test coverage
  • Loading branch information
ekristen committed Jun 21, 2024
1 parent 7df3698 commit e192598
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (s *Settings) Set(key string, value *Setting) {

type Setting map[string]interface{}

// Get returns the value of a key in the Setting
// Deprecated: use GetBool, GetString, or GetInt instead
func (s *Setting) Get(key string) interface{} {
value, ok := (*s)[key]
if !ok {
Expand All @@ -49,6 +51,37 @@ func (s *Setting) Get(key string) interface{} {
}
}

// GetBool returns the boolean value of a key in the Setting
func (s *Setting) GetBool(key string) bool {
value, ok := (*s)[key]
if !ok {
return false
}

return value.(bool)
}

// GetString returns the string value of a key in the Setting
func (s *Setting) GetString(key string) string {
value, ok := (*s)[key]
if !ok {
return ""
}

return value.(string)
}

// GetInt returns the integer value of a key in the Setting
func (s *Setting) GetInt(key string) int {
value, ok := (*s)[key]
if !ok {
return -1
}

return value.(int)
}

// Set sets a key value pair in the Setting
func (s *Setting) Set(key string, value interface{}) {
(*s)[key] = value
}
21 changes: 21 additions & 0 deletions pkg/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@ func TestSettings_GetNil(t *testing.T) {
set := s.Get("key")
assert.Nil(t, set)
}

func TestSetting_GetString(t *testing.T) {
s := Setting{}
s.Set("TestSetting", "test")
assert.Equal(t, "test", s.GetString("TestSetting"))
assert.Equal(t, "", s.GetString("InvalidSetting"))
}

func TestSetting_GetInt(t *testing.T) {
s := Setting{}
s.Set("TestSetting", 123)
assert.Equal(t, 123, s.GetInt("TestSetting"))
assert.Equal(t, -1, s.GetInt("InvalidSetting"))
}

func TestSetting_GetBool(t *testing.T) {
s := Setting{}
s.Set("TestSetting", true)
assert.Equal(t, true, s.GetBool("TestSetting"))
assert.Equal(t, false, s.GetBool("InvalidSetting"))
}

0 comments on commit e192598

Please sign in to comment.