diff --git a/types.go b/types.go index 4408897..2e21369 100644 --- a/types.go +++ b/types.go @@ -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 { diff --git a/types_test.go b/types_test.go index 154a77c..72cdc57 100644 --- a/types_test.go +++ b/types_test.go @@ -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) { @@ -57,7 +67,8 @@ func TestBasic(t *testing.T) { "NestedBool": false, "NestedNum": 37 }, - "Array":[1,2,3] + "Array":[1,2,3], + "Int": 123 }`, ), &jsonMap,