Skip to content

Commit 41b79d3

Browse files
committed
config: inline toString() (more or less)
for string, numbers and boolean it's the same behaviour; now it could stringify other types that before were yielding only "", but shouldn't matter much here. Also, no need to special case strings since it's not a performance-sensitive part, and the gained readability matters.
1 parent 0a2511d commit 41b79d3

2 files changed

Lines changed: 3 additions & 35 deletions

File tree

config/load_test.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ remote:
277277
t.Fatalf("location = %q", got["remote"]["location"])
278278
}
279279
if got["remote"]["port"] != "22" {
280-
t.Fatalf("port = %q (toString should convert int)", got["remote"]["port"])
280+
t.Fatalf("port = %q (should be converted from int)", got["remote"]["port"])
281281
}
282282
if got["remote"]["ssl"] != "true" {
283-
t.Fatalf("ssl = %q (toString should convert bool)", got["remote"]["ssl"])
283+
t.Fatalf("ssl = %q (should be converted from bool)", got["remote"]["ssl"])
284284
}
285285
}
286286

@@ -322,27 +322,6 @@ func TestLoadJSONBad(t *testing.T) {
322322
}
323323
}
324324

325-
func TestToString(t *testing.T) {
326-
cases := []struct {
327-
in any
328-
want string
329-
}{
330-
{"hello", "hello"},
331-
{42, "42"},
332-
{int64(7), "7"},
333-
{3.14, "3.14"},
334-
{true, "true"},
335-
{false, "false"},
336-
{nil, ""},
337-
{[]int{1, 2}, ""},
338-
}
339-
for _, c := range cases {
340-
if got := toString(c.in); got != c.want {
341-
t.Errorf("toString(%v) = %q, want %q", c.in, got, c.want)
342-
}
343-
}
344-
}
345-
346325
func TestLoadFileYAMLWithLocation(t *testing.T) {
347326
rd := strings.NewReader(`
348327
remote:

config/loadfile.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ import (
1212
"go.yaml.in/yaml/v3"
1313
)
1414

15-
func toString(v any) string {
16-
switch t := v.(type) {
17-
case string:
18-
return t
19-
case int, int64, float64, bool:
20-
return fmt.Sprintf("%v", t)
21-
default:
22-
return ""
23-
}
24-
}
25-
2615
func loadINI(rd io.Reader) (map[string]map[string]string, error) {
2716
cfg, err := ini.Load(rd)
2817
if err != nil {
@@ -60,7 +49,7 @@ func loadYAML(rd io.Reader) (map[string]map[string]string, error) {
6049
}
6150
result[section] = make(map[string]string)
6251
for k, v := range sectionMap {
63-
result[section][k] = toString(v)
52+
result[section][k] = fmt.Sprint(v)
6453
}
6554
}
6655

0 commit comments

Comments
 (0)