From 38ee792081564881b13a72e8f08d595f984cc144 Mon Sep 17 00:00:00 2001 From: Cuteivist Date: Mon, 19 Feb 2024 13:09:02 +0100 Subject: [PATCH] feat: Cast int64 for settings (#4700) --- multiaccounts/settings/columns.go | 7 +++--- multiaccounts/settings/columns_test.go | 31 ++++++++++++++++++++++++ multiaccounts/settings/database.go | 7 ++++++ multiaccounts/settings/structs.go | 6 +++++ multiaccounts/settings/value_handlers.go | 9 +++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/multiaccounts/settings/columns.go b/multiaccounts/settings/columns.go index ecd206bee..665cdd8d2 100644 --- a/multiaccounts/settings/columns.go +++ b/multiaccounts/settings/columns.go @@ -425,9 +425,10 @@ var ( }, } DisplayAssetsBelowBalanceThreshold = SettingField{ - reactFieldName: "display-assets-below-balance-threshold", - dBColumnName: "wallet_display_assets_below_balance_threshold", - valueHandler: Int64Handler, + reactFieldName: "display-assets-below-balance-threshold", + dBColumnName: "wallet_display_assets_below_balance_threshold", + valueHandler: Int64Handler, + valueCastHandler: Float64ToInt64Handler, syncProtobufFactory: &SyncProtobufFactory{ fromInterface: displayAssetsBelowBalanceThresholdProtobufFactory, fromStruct: displayAssetsBelowBalanceThresholdProtobufFactoryStruct, diff --git a/multiaccounts/settings/columns_test.go b/multiaccounts/settings/columns_test.go index ed1f8a307..5f1ca6aeb 100644 --- a/multiaccounts/settings/columns_test.go +++ b/multiaccounts/settings/columns_test.go @@ -67,3 +67,34 @@ func TestGetFieldFromProtobufType(t *testing.T) { require.NoError(t, err) } } + +func TestValueHandler(t *testing.T) { + boolSetting := SettingField{ + valueHandler: BoolHandler, + } + + boolValue := true + require.NotNil(t, boolSetting.ValueHandler()) + handledBoolValue, err := boolSetting.ValueHandler()(boolValue) + require.NoError(t, err) + require.True(t, boolValue == handledBoolValue) + + // Incorrect value should be return error + int64Value := int64(5) + handledInt64Value, err := boolSetting.ValueHandler()(int64Value) + require.Error(t, err) + require.True(t, handledInt64Value == int64Value) +} + +func TestValueCastHandler(t *testing.T) { + int64Setting := SettingField{ + valueCastHandler: Float64ToInt64Handler, + } + + float64Value := float64(5) + + require.NotNil(t, int64Setting.ValueCastHandler()) + handledInt64Value, err := int64Setting.ValueCastHandler()(float64Value) + require.NoError(t, err) + require.True(t, handledInt64Value == int64(5)) +} diff --git a/multiaccounts/settings/database.go b/multiaccounts/settings/database.go index cecac073f..035c19579 100644 --- a/multiaccounts/settings/database.go +++ b/multiaccounts/settings/database.go @@ -255,6 +255,13 @@ func (db *Database) saveSetting(setting SettingField, value interface{}) error { } func (db *Database) parseSaveAndSyncSetting(sf SettingField, value interface{}) (err error) { + if sf.ValueCastHandler() != nil { + value, err = sf.ValueCastHandler()(value) + if err != nil { + return err + } + } + if sf.ValueHandler() != nil { value, err = sf.ValueHandler()(value) if err != nil { diff --git a/multiaccounts/settings/structs.go b/multiaccounts/settings/structs.go index c4e31df4c..9e79ca161 100644 --- a/multiaccounts/settings/structs.go +++ b/multiaccounts/settings/structs.go @@ -11,6 +11,7 @@ import ( ) type ValueHandler func(interface{}) (interface{}, error) +type ValueCastHandler func(interface{}) (interface{}, error) type SyncSettingProtobufFactoryInterface func(interface{}, uint64, string) (*common.RawMessage, *protobuf.SyncSetting, error) type SyncSettingProtobufFactoryStruct func(Settings, uint64, string) (*common.RawMessage, *protobuf.SyncSetting, error) type SyncSettingProtobufToValue func(setting *protobuf.SyncSetting) interface{} @@ -69,6 +70,7 @@ type SettingField struct { dBColumnName string valueHandler ValueHandler syncProtobufFactory *SyncProtobufFactory + valueCastHandler ValueCastHandler } func (s SettingField) GetReactName() string { @@ -83,6 +85,10 @@ func (s SettingField) ValueHandler() ValueHandler { return s.valueHandler } +func (s SettingField) ValueCastHandler() ValueCastHandler { + return s.valueCastHandler +} + func (s SettingField) SyncProtobufFactory() *SyncProtobufFactory { return s.syncProtobufFactory } diff --git a/multiaccounts/settings/value_handlers.go b/multiaccounts/settings/value_handlers.go index c2074f93d..98150e89d 100644 --- a/multiaccounts/settings/value_handlers.go +++ b/multiaccounts/settings/value_handlers.go @@ -72,3 +72,12 @@ func NodeConfigHandler(value interface{}) (interface{}, error) { return nodeConfig, nil } + +func Float64ToInt64Handler(value interface{}) (interface{}, error) { + floatValue, ok := value.(float64) + if !ok { + // Ignore if not float64 + return value, nil + } + return int64(floatValue), nil +}