fix: move canInt to dynamic

This commit is contained in:
zenix 2022-08-18 18:05:52 +09:00
parent a958d4d092
commit 5030b93285
2 changed files with 16 additions and 23 deletions

14
pkg/dynamic/can.go Normal file
View File

@ -0,0 +1,14 @@
package dynamic
import "reflect"
// For backward compatibility of reflect.Value.CanInt in go1.17
func CanInt(v reflect.Value) bool {
k := v.Type().Kind()
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
default:
return false
}
}

View File

@ -6,32 +6,11 @@ import (
"reflect"
"unsafe"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/strategy"
"github.com/jedib0t/go-pretty/v6/table"
)
type jsonStruct struct {
key string
json string
tp string
value interface{}
}
type jsonArr []jsonStruct
func (a jsonArr) Len() int { return len(a) }
func (a jsonArr) Less(i, j int) bool { return a[i].key < a[j].key }
func (a jsonArr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func canInt(v reflect.Value) bool {
k := v.Type().Kind()
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
default:
return false
}
}
func (s *Strategy) ParamDump(f io.Writer, seriesLength ...int) {
length := 1
if len(seriesLength) > 0 && seriesLength[0] > 0 {
@ -81,7 +60,7 @@ func (s *Strategy) ParamDump(f io.Writer, seriesLength ...int) {
}
} else if canString {
fmt.Fprintf(f, "%s: %s\n", fieldName, stringFunc.Call(nil)[0].String())
} else if canInt(field) {
} else if dynamic.CanInt(field) {
fmt.Fprintf(f, "%s: %d\n", fieldName, field.Int())
} else if field.CanConvert(reflect.TypeOf(float64(0))) {
fmt.Fprintf(f, "%s: %.4f\n", fieldName, field.Float())