From 5030b9328571d4e6058625d754fefe6a29871718 Mon Sep 17 00:00:00 2001 From: zenix Date: Thu, 18 Aug 2022 18:05:52 +0900 Subject: [PATCH] fix: move canInt to dynamic --- pkg/dynamic/can.go | 14 ++++++++++++++ pkg/strategy/drift/output.go | 25 ++----------------------- 2 files changed, 16 insertions(+), 23 deletions(-) create mode 100644 pkg/dynamic/can.go diff --git a/pkg/dynamic/can.go b/pkg/dynamic/can.go new file mode 100644 index 000000000..532a69df1 --- /dev/null +++ b/pkg/dynamic/can.go @@ -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 + } +} diff --git a/pkg/strategy/drift/output.go b/pkg/strategy/drift/output.go index 9cccdc1ed..021b4aa31 100644 --- a/pkg/strategy/drift/output.go +++ b/pkg/strategy/drift/output.go @@ -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())