Skip to content

Commit

Permalink
add GetInt helper function to dynamic config
Browse files Browse the repository at this point in the history
  • Loading branch information
jkw-statsig committed May 10, 2022
1 parent 94bbdd8 commit 5090e7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ func (d *configBase) GetNumber(key string, fallback float64) float64 {
return fallback
}

// Gets the int64 value at the given key in the DynamicConfig
// Returns the fallback int64 if the item at the given key is not found or not of type int64
func (d *configBase) GetInt(key string, fallback int64) int64 {
if v, ok := d.Value[key]; ok {
switch val := v.(type) {
case int64:
logExposure(d, key)
return val
}
}
return fallback
}

// Gets the boolean value at the given key in the DynamicConfig
// Returns the fallback boolean if the item at the given key is not found or not of type boolean
func (d *configBase) GetBool(key string, fallback bool) bool {
Expand Down
13 changes: 12 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func doValidation(t *testing.T, c *configBase) {
if c.GetBool("Object", false) {
t.Errorf("Failed to use fallback boolean")
}

if c.GetInt("String", 1) != 1 {
t.Errorf("Failed to use fallback int")
}
if c.GetInt("Number", 1) != 1 {
t.Errorf("Failed to use fallback int")
}
if c.GetInt("Int", 1) != 123 {
t.Errorf("Failed to return int")
}
}

func TestBasic(t *testing.T) {
Expand All @@ -57,7 +67,8 @@ func TestBasic(t *testing.T) {
"NestedBool": false,
"NestedNum": 37
},
"Array":[1,2,3]
"Array":[1,2,3],
"Int": 123
}`,
),
&jsonMap,
Expand Down

0 comments on commit 5090e7f

Please sign in to comment.