Skip to content

Commit

Permalink
Merge pull request #31 from antonmashko/fix-removing-spaces-from-string
Browse files Browse the repository at this point in the history
fix removing spaces from string
  • Loading branch information
antonmashko authored Sep 14, 2023
2 parents 383813f + a8a6f8b commit e3164d7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,19 @@ func TestJsonConfig_ZeroValue_Ok(t *testing.T) {
t.Errorf("incorrect result: %#v", tc)
}
}

func TestJsonConfig_ValueWithSpace_Ok(t *testing.T) {
json := `{"foo": " bar "}`
tc := struct {
Foo string
}{}
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}

if tc.Foo != " bar " {
t.Errorf("incorrect result: %#v", tc)
}
}
15 changes: 15 additions & 0 deletions parser_primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,18 @@ func TestParse_UnsupportedFieldWithoutPanic_Ok(t *testing.T) {
t.Fatal(err)
}
}

func TestParse_ValueWithSpace_Ok(t *testing.T) {
cfg := struct {
Field1 string `env:"TEST_FIELD_1"`
}{}

os.Setenv("TEST_FIELD_1", " test ")
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}

if cfg.Field1 != " test " {
t.Errorf("incorrect result:%#v", cfg)
}
}
3 changes: 2 additions & 1 deletion type_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (t *primitiveType) define() error {
}

func setFromString(field reflect.Value, value string) error {
oval := value
value = strings.Trim(value, " ")
// native complex types
switch field.Interface().(type) {
Expand Down Expand Up @@ -142,7 +143,7 @@ func setFromString(field reflect.Value, value string) error {
// primitives and collections
switch field.Kind() {
case reflect.String:
field.SetString(value)
field.SetString(oval)
case reflect.Bool:
i, err := strconv.ParseBool(value)
if err != nil {
Expand Down

0 comments on commit e3164d7

Please sign in to comment.