feat: Cast int64 for settings (#4700)

This commit is contained in:
Cuteivist 2024-02-19 13:09:02 +01:00 committed by GitHub
parent 9e36df449e
commit 38ee792081
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 3 deletions

View File

@ -425,9 +425,10 @@ var (
}, },
} }
DisplayAssetsBelowBalanceThreshold = SettingField{ DisplayAssetsBelowBalanceThreshold = SettingField{
reactFieldName: "display-assets-below-balance-threshold", reactFieldName: "display-assets-below-balance-threshold",
dBColumnName: "wallet_display_assets_below_balance_threshold", dBColumnName: "wallet_display_assets_below_balance_threshold",
valueHandler: Int64Handler, valueHandler: Int64Handler,
valueCastHandler: Float64ToInt64Handler,
syncProtobufFactory: &SyncProtobufFactory{ syncProtobufFactory: &SyncProtobufFactory{
fromInterface: displayAssetsBelowBalanceThresholdProtobufFactory, fromInterface: displayAssetsBelowBalanceThresholdProtobufFactory,
fromStruct: displayAssetsBelowBalanceThresholdProtobufFactoryStruct, fromStruct: displayAssetsBelowBalanceThresholdProtobufFactoryStruct,

View File

@ -67,3 +67,34 @@ func TestGetFieldFromProtobufType(t *testing.T) {
require.NoError(t, err) 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))
}

View File

@ -255,6 +255,13 @@ func (db *Database) saveSetting(setting SettingField, value interface{}) error {
} }
func (db *Database) parseSaveAndSyncSetting(sf SettingField, value interface{}) (err 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 { if sf.ValueHandler() != nil {
value, err = sf.ValueHandler()(value) value, err = sf.ValueHandler()(value)
if err != nil { if err != nil {

View File

@ -11,6 +11,7 @@ import (
) )
type ValueHandler func(interface{}) (interface{}, error) type ValueHandler func(interface{}) (interface{}, error)
type ValueCastHandler func(interface{}) (interface{}, error)
type SyncSettingProtobufFactoryInterface func(interface{}, uint64, string) (*common.RawMessage, *protobuf.SyncSetting, error) type SyncSettingProtobufFactoryInterface func(interface{}, uint64, string) (*common.RawMessage, *protobuf.SyncSetting, error)
type SyncSettingProtobufFactoryStruct func(Settings, 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{} type SyncSettingProtobufToValue func(setting *protobuf.SyncSetting) interface{}
@ -69,6 +70,7 @@ type SettingField struct {
dBColumnName string dBColumnName string
valueHandler ValueHandler valueHandler ValueHandler
syncProtobufFactory *SyncProtobufFactory syncProtobufFactory *SyncProtobufFactory
valueCastHandler ValueCastHandler
} }
func (s SettingField) GetReactName() string { func (s SettingField) GetReactName() string {
@ -83,6 +85,10 @@ func (s SettingField) ValueHandler() ValueHandler {
return s.valueHandler return s.valueHandler
} }
func (s SettingField) ValueCastHandler() ValueCastHandler {
return s.valueCastHandler
}
func (s SettingField) SyncProtobufFactory() *SyncProtobufFactory { func (s SettingField) SyncProtobufFactory() *SyncProtobufFactory {
return s.syncProtobufFactory return s.syncProtobufFactory
} }

View File

@ -72,3 +72,12 @@ func NodeConfigHandler(value interface{}) (interface{}, error) {
return nodeConfig, nil 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
}