Merge pull request #109 from sosedoff/bigint-to-string
Convert bigints that exceed js number limits to strings
This commit is contained in:
commit
e662e907be
@ -193,6 +193,8 @@ func (client *Client) query(query string, args ...interface{}) (*Result, error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.PrepareBigints()
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Row []interface{}
|
type Row []interface{}
|
||||||
@ -14,6 +16,31 @@ type Result struct {
|
|||||||
Rows []Row `json:"rows"`
|
Rows []Row `json:"rows"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Due to big int number limitations in javascript, numbers should be encoded
|
||||||
|
// as strings so they could be properly loaded on the frontend.
|
||||||
|
func (res *Result) PrepareBigints() {
|
||||||
|
for i, row := range res.Rows {
|
||||||
|
for j, col := range row {
|
||||||
|
if col == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch reflect.TypeOf(col).Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
val := col.(int64)
|
||||||
|
if val < -9007199254740991 || val > 9007199254740991 {
|
||||||
|
res.Rows[i][j] = strconv.FormatInt(col.(int64), 10)
|
||||||
|
}
|
||||||
|
case reflect.Float64:
|
||||||
|
val := col.(float64)
|
||||||
|
if val < -999999999999999 || val > 999999999999999 {
|
||||||
|
res.Rows[i][j] = strconv.FormatFloat(val, 'e', -1, 64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (res *Result) Format() []map[string]interface{} {
|
func (res *Result) Format() []map[string]interface{} {
|
||||||
var items []map[string]interface{}
|
var items []map[string]interface{}
|
||||||
|
|
||||||
|
@ -7,6 +7,27 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_PrepareBigints(t *testing.T) {
|
||||||
|
result := Result{
|
||||||
|
Columns: []string{"value"},
|
||||||
|
Rows: []Row{
|
||||||
|
Row{int(1234)},
|
||||||
|
Row{int64(9223372036854775807)},
|
||||||
|
Row{int64(-9223372036854775808)},
|
||||||
|
Row{float64(9223372036854775808.9223372036854775808)},
|
||||||
|
Row{float64(999999999999999.9)},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result.PrepareBigints()
|
||||||
|
|
||||||
|
assert.Equal(t, 1234, result.Rows[0][0])
|
||||||
|
assert.Equal(t, "9223372036854775807", result.Rows[1][0])
|
||||||
|
assert.Equal(t, "-9223372036854775808", result.Rows[2][0])
|
||||||
|
assert.Equal(t, "9.223372036854776e+18", result.Rows[3][0])
|
||||||
|
assert.Equal(t, "9.999999999999999e+14", result.Rows[4][0])
|
||||||
|
}
|
||||||
|
|
||||||
func Test_CSV(t *testing.T) {
|
func Test_CSV(t *testing.T) {
|
||||||
result := Result{
|
result := Result{
|
||||||
Columns: []string{"id", "name", "email"},
|
Columns: []string{"id", "name", "email"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user