Files
gocryptotrader/config/versions/v4.go
Gareth Kirwan 16d2d9f35a Config: AssetEnabled upgrade (#1735)
* Config: Move assetEnabled upgrade to Version management

* Assets: Do not error on asset not enabled, or disabled

This became more messy with Disabling something that's defaulted to
disabled.
Taking an idealogical stance against erroring that what you want to have
done is already done.

* CurrencyManager: Set AssetEnabled when StorePairs(enabled)

* RPCServer: Fix tests expecting StoreAssetPairFormat to enable the asset

Also assertifies

* Bitfinex: Fix tests for MarginFunding subs

* GCTWrapper: Improve TestMain clarity

* BTSE: Add futures to testconfig

* Exchanges: Rename StoreAssetPairStore

Previously we were calling it "Format", but accepting everything from
the PairStore.
We were also defaulting to turning the Asset on.

Now callers need to get their AssetEnabled set as they want it, so
there's no magic

This change also moves responsibility for error wrapping outside to the
caller.

* Config: AssetEnabled upgrade should respect assetTypes

Previously we ignored the field and just turned on everything.
I think that was because we couldn't get at the old value.
In either case, we have the option to do better, and respect the
assetEnabled value

* Config: Improve exchange config version upgrade error messages
2025-03-17 21:47:37 +11:00

87 lines
2.9 KiB
Go

package versions
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"github.com/buger/jsonparser"
)
var (
errUpgradingAssetTypes = errors.New("error upgrading assetTypes")
errUpgradingCurrencyPairs = errors.New("error upgrading currencyPairs.pairs")
)
// Version4 is an Exchange upgrade to move currencyPairs.assetTypes to currencyPairs.pairs.*.assetEnabled
type Version4 struct {
}
func init() {
Manager.registerVersion(4, &Version4{})
}
// Exchanges returns all exchanges: "*"
func (v *Version4) Exchanges() []string { return []string{"*"} }
// UpgradeExchange sets AssetEnabled: true for all assets listed in assetTypes, and false for any with no field
func (v *Version4) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
toEnable := map[string]bool{}
assetTypesFn := func(asset []byte, valueType jsonparser.ValueType, _ int, _ error) {
if valueType == jsonparser.String {
toEnable[string(asset)] = true
}
}
_, err := jsonparser.ArrayEach(e, assetTypesFn, "currencyPairs", "assetTypes")
if err != nil && !errors.Is(err, jsonparser.KeyPathNotFoundError) {
return e, fmt.Errorf("%w: %w", errUpgradingAssetTypes, err)
}
assetEnabledFn := func(assetBytes []byte, v []byte, _ jsonparser.ValueType, _ int) (err error) {
asset := string(assetBytes)
if toEnable[asset] {
e, err = jsonparser.Set(e, []byte(`true`), "currencyPairs", "pairs", asset, "assetEnabled")
} else {
var vT jsonparser.ValueType
_, vT, _, err = jsonparser.Get(v, "assetEnabled")
switch {
case vT == jsonparser.Null, errors.Is(err, jsonparser.KeyPathNotFoundError):
e, err = jsonparser.Set(e, []byte(`false`), "currencyPairs", "pairs", asset, "assetEnabled")
case err == nil && vT != jsonparser.Boolean:
err = fmt.Errorf("assetEnabled: %w (`%s`)", jsonparser.UnknownValueTypeError, vT)
}
}
if err != nil {
err = fmt.Errorf("%w for asset `%s`", err, asset)
}
return err
}
if err = jsonparser.ObjectEach(bytes.Clone(e), assetEnabledFn, "currencyPairs", "pairs"); err != nil {
return e, fmt.Errorf("%w: %w", errUpgradingCurrencyPairs, err)
}
e = jsonparser.Delete(e, "currencyPairs", "assetTypes")
return e, err
}
// DowngradeExchange moves AssetEnabled assets into AssetType field
func (v *Version4) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
assetTypes := []string{}
assetEnabledFn := func(asset []byte, v []byte, _ jsonparser.ValueType, _ int) error {
if b, err := jsonparser.GetBoolean(v, "assetEnabled"); err == nil {
if b {
assetTypes = append(assetTypes, fmt.Sprintf("%q", asset))
}
e = jsonparser.Delete(e, "currencyPairs", "pairs", string(asset), "assetEnabled")
}
return nil
}
if err := jsonparser.ObjectEach(bytes.Clone(e), assetEnabledFn, "currencyPairs", "pairs"); err != nil {
return e, err
}
return jsonparser.Set(e, []byte(`[`+strings.Join(assetTypes, ",")+`]`), "currencyPairs", "assetTypes")
}