Skip to content

Commit

Permalink
Support all numeric type conversions (#102)
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
moorereason authored and pelletier committed Sep 20, 2016
1 parent 31055c2 commit 67b7b94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 10 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\""},
Expand Down
22 changes: 21 additions & 1 deletion tomltree_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
}
}

Expand Down

0 comments on commit 67b7b94

Please sign in to comment.