Support all numeric type conversions (#102)

Fixes #101
This commit is contained in:
Cameron Moore
2016-09-20 02:04:39 -05:00
committed by Thomas Pelletier
parent 31055c2ff0
commit 67b7b944a8
2 changed files with 31 additions and 1 deletions
+10
View File
@@ -627,7 +627,17 @@ func TestToTomlValue(t *testing.T) {
Value interface{}
Expect string
}{
{int(1), "1"},
{int8(2), "2"},
{int16(3), "3"},
{int32(4), "4"},
{int64(12345), "12345"},
{uint(10), "10"},
{uint8(20), "20"},
{uint16(30), "30"},
{uint32(40), "40"},
{uint64(50), "50"},
{float32(12.456), "12.456"},
{float64(123.45), "123.45"},
{bool(true), "true"},
{"hello world", "\"hello world\""},
+21 -1
View File
@@ -45,8 +45,28 @@ func encodeTomlString(value string) string {
func toTomlValue(item interface{}, indent int) string {
tab := strings.Repeat(" ", indent)
switch value := item.(type) {
case int:
return tab + strconv.FormatInt(int64(value), 10)
case int8:
return tab + strconv.FormatInt(int64(value), 10)
case int16:
return tab + strconv.FormatInt(int64(value), 10)
case int32:
return tab + strconv.FormatInt(int64(value), 10)
case int64:
return tab + strconv.FormatInt(value, 10)
case uint:
return tab + strconv.FormatUint(uint64(value), 10)
case uint8:
return tab + strconv.FormatUint(uint64(value), 10)
case uint16:
return tab + strconv.FormatUint(uint64(value), 10)
case uint32:
return tab + strconv.FormatUint(uint64(value), 10)
case uint64:
return tab + strconv.FormatUint(value, 10)
case float32:
return tab + strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return tab + strconv.FormatFloat(value, 'f', -1, 64)
case string:
@@ -65,7 +85,7 @@ func toTomlValue(item interface{}, indent int) string {
}
return result + tab + "]"
default:
panic(fmt.Sprintf("unsupported value type: %v", value))
panic(fmt.Sprintf("unsupported value type %T: %v", value, value))
}
}