Config: Refactor version packages (#1887)

* Config: Move config versions to separate pacakges

* Config: Move version tests to blackbox texts

* Config: Protect registerVersion from overflow

* Config: Protect against version already registered
This commit is contained in:
Gareth Kirwan
2025-04-22 04:13:01 +02:00
committed by GitHub
parent 545fa9d01a
commit 1bf3433d61
19 changed files with 254 additions and 153 deletions

53
config/versions/v1/v1.go Normal file
View File

@@ -0,0 +1,53 @@
package v1
import (
"context"
"github.com/buger/jsonparser"
v0 "github.com/thrasher-corp/gocryptotrader/config/versions/v0"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Version is an ExchangeVersion to upgrade currency pair format for exchanges
type Version struct{}
// Exchanges returns all exchanges: "*"
func (*Version) Exchanges() []string { return []string{"*"} }
// UpgradeExchange will upgrade currency pair format
func (*Version) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
if _, d, _, err := jsonparser.Get(e, "currencyPairs"); err == nil && d == jsonparser.Object {
return e, nil
}
d := &v0.Exchange{}
if err := json.Unmarshal(e, d); err != nil {
return e, err
}
p := &PairsManager{
UseGlobalFormat: true,
LastUpdated: d.PairsLastUpdated,
ConfigFormat: d.ConfigCurrencyPairFormat,
RequestFormat: d.RequestCurrencyPairFormat,
Pairs: FullStore{
"spot": {
Available: d.AvailablePairs,
Enabled: d.EnabledPairs,
},
},
}
j, err := json.Marshal(p)
if err != nil {
return e, err
}
for _, f := range []string{"pairsLastUpdated", "configCurrencyPairFormat", "requestCurrencyPairFormat", "assetTypes", "availablePairs", "enabledPairs"} {
e = jsonparser.Delete(e, f)
}
return jsonparser.Set(e, j, "currencyPairs")
}
// DowngradeExchange doesn't do anything for v1; There's no downgrade path since the original state is lossy and v1 was before versioning
func (*Version) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
return e, nil
}

View File

@@ -0,0 +1,37 @@
package v1_test
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "github.com/thrasher-corp/gocryptotrader/config/versions/v1"
)
func TestExchanges(t *testing.T) {
t.Parallel()
assert.Equal(t, []string{"*"}, new(v1.Version).Exchanges())
}
func TestUpgradeExchange(t *testing.T) {
t.Parallel()
v := &v1.Version{}
in := []byte(`{"name":"Wibble","pairsLastUpdated":1566798411,"assetTypes":"spot","configCurrencyPairFormat":{"uppercase":true,"delimiter":"_"},"requestCurrencyPairFormat":{"uppercase":false,"delimiter":"_","separator":"-"},"enabledPairs":"LTC_BTC","availablePairs":"LTC_BTC,ETH_BTC,BTC_USD"}`)
exp := []byte(`{"name":"Wibble","currencyPairs":{"bypassConfigFormatUpgrades":false,"requestFormat":{"uppercase":false,"delimiter":"_","separator":"-"},"configFormat":{"uppercase":true,"delimiter":"_"},"useGlobalFormat":true,"lastUpdated":1566798411,"pairs":{"spot":{"enabled":"LTC_BTC","available":"LTC_BTC,ETH_BTC,BTC_USD"}}}}`)
out, err := v.UpgradeExchange(context.Background(), in)
require.NoError(t, err)
require.NotEmpty(t, out)
assert.Equal(t, string(exp), string(out))
}
func TestDowngradeExchange(t *testing.T) {
t.Parallel()
in := []byte("just leave me alone, mkay?")
out, err := new(v1.Version).DowngradeExchange(context.Background(), bytes.Clone(in))
require.NoError(t, err)
assert.Equal(t, out, in)
}