Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue8 - GetInt() would fail trying to get an int from json #9

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ type Options struct {
Feeder Feeder // Feeder is the feeder that is going to feed the Config instance.
Env string // Env is the file path that locates the environment file.
}

//NotFoundError happens when you try to access a key which is not defined in the configuration files.
type NotFoundError struct{
type NotFoundError struct {
key string
}

func (n *NotFoundError) Error() string {
return fmt.Sprintf("value not found for the key %s", n.key)
}

//TypeError happens when you try to access a key using a helper function that casts value to a type which can't be done.
type TypeError struct {
value interface{}
value interface{}
wanted string
}

func (t *TypeError) Error() string {
return fmt.Sprintf("value %s (%T) is not %s", t.value, t.value, t.wanted)
}

// Config keeps all the Config instance data.
type Config struct {
env struct {
Expand Down Expand Up @@ -221,8 +226,13 @@ func (c *Config) GetInt(key string) (int, error) {
return 0, err
}

if v, ok := v.(int); ok {
return v, nil
switch val := v.(type) {
case int:
return val, nil
case float64:
return int(val), nil
case string:
return strconv.Atoi(val)
}

return 0, &TypeError{value: v, wanted: "int"}
Expand Down
30 changes: 27 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package config_test

import (
json2 "encoding/json"
"github.com/golobby/config"
"github.com/golobby/config/feeder"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"

"github.com/golobby/config"
"github.com/golobby/config/feeder"
"github.com/stretchr/testify/assert"
)

func Test_Config_Set_Get_With_A_Simple_Key_String_Value(t *testing.T) {
Expand Down Expand Up @@ -406,3 +407,26 @@ func Test_Config_Reload_It_Should_Reload_The_Feeders(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "new-value", v)
}

// https://github.com/golobby/config/issues/8
func Test_GetInt_From_JSON(t *testing.T) {
c, err := config.New(config.Options{
Feeder: feeder.Json{Path: "feeder/test/issue8.json"},
})
assert.NoError(t, err)

keys := []string{
"int",
"strInt",
}

for _, key := range keys {
v, err := c.GetInt(key)
if err != nil {
t.Errorf(
"\nkey: %v \nv: %v \nerr: %v",
key, v, err.Error(),
)
}
}
}
4 changes: 4 additions & 0 deletions feeder/test/issue8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"int": 3,
"strInt": "3"
}