dynamic: fix compare for fixedpoint and time.Time

This commit is contained in:
c9s 2024-11-07 18:05:19 +08:00
parent bdc89ca579
commit eb91f9d77f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"strconv" "strconv"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
@ -41,7 +42,7 @@ func Compare(a, b interface{}) ([]Diff, error) {
return nil, fmt.Errorf("kind mismatch: %s != %s", raKind, rbKind) return nil, fmt.Errorf("kind mismatch: %s != %s", raKind, rbKind)
} }
if isSimpleType(raKind) { if isSimpleType(ra) {
if compareSimpleValue(ra, rb) { if compareSimpleValue(ra, rb) {
// no changes // no changes
return nil, nil return nil, nil
@ -62,13 +63,8 @@ func Compare(a, b interface{}) ([]Diff, error) {
} }
func compareStruct(a, b reflect.Value) ([]Diff, error) { func compareStruct(a, b reflect.Value) ([]Diff, error) {
if a.Kind() == reflect.Ptr { a = reflect.Indirect(a)
a = a.Elem() b = reflect.Indirect(b)
}
if b.Kind() == reflect.Ptr {
b = b.Elem()
}
if a.Kind() != reflect.Struct { if a.Kind() != reflect.Struct {
return nil, fmt.Errorf("value is not a struct") return nil, fmt.Errorf("value is not a struct")
@ -96,7 +92,7 @@ func compareStruct(a, b reflect.Value) ([]Diff, error) {
continue continue
} }
if isSimpleType(fieldValueA.Kind()) { if isSimpleType(fieldValueA) {
if compareSimpleValue(fieldValueA, fieldValueB) { if compareSimpleValue(fieldValueA, fieldValueB) {
continue continue
} else { } else {
@ -125,7 +121,20 @@ func compareStruct(a, b reflect.Value) ([]Diff, error) {
return diffs, nil return diffs, nil
} }
func isSimpleType(kind reflect.Kind) bool { func isSimpleType(a reflect.Value) bool {
a = reflect.Indirect(a)
aInf := a.Interface()
switch aInf.(type) {
case time.Time:
return true
case fixedpoint.Value:
return true
}
kind := a.Kind()
switch kind { switch kind {
case reflect.Bool, reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint64, reflect.String, reflect.Float64: case reflect.Bool, reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint64, reflect.String, reflect.Float64:
return true return true
@ -169,21 +178,37 @@ func compareSimpleValue(a, b reflect.Value) bool {
// TODO: compare slice // TODO: compare slice
default: default:
// unhandled case ainf := a.Interface()
binf := b.Interface()
switch aa := ainf.(type) {
case fixedpoint.Value:
if bb, ok := binf.(fixedpoint.Value); ok {
return bb.Compare(aa) == 0
}
case time.Time:
if bb, ok := binf.(time.Time); ok {
return bb.Compare(aa) == 0
}
}
// other unhandled cases
} }
return false return false
} }
func convertToStr(val reflect.Value) string { func convertToStr(val reflect.Value) string {
if val.Type() == reflect.TypeOf(fixedpoint.Zero) { val = reflect.Indirect(val)
fp := val.Interface().(fixedpoint.Value)
return fp.String()
}
if val.Kind() == reflect.Ptr { if val.Type() == reflect.TypeOf(fixedpoint.Zero) {
val = val.Elem() inf := val.Interface()
switch aa := inf.(type) {
case fixedpoint.Value:
return aa.String()
case time.Time:
return aa.String()
}
} }
switch val.Kind() { switch val.Kind() {