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

35
config/versions/v3/v3.go Normal file
View File

@@ -0,0 +1,35 @@
package v3
import (
"context"
"time"
"github.com/buger/jsonparser"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Version is an ExchangeVersion to remove the publishPeriod from the exchange's orderbook config
type Version struct{}
// Exchanges returns all exchanges: "*"
func (*Version) Exchanges() []string { return []string{"*"} }
// UpgradeExchange will remove the publishPeriod from the exchange's orderbook config
func (*Version) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
e = jsonparser.Delete(e, "orderbook", "publishPeriod")
return e, nil
}
const defaultOrderbookPublishPeriod = time.Second * 10
// DowngradeExchange will downgrade the exchange's config by setting the default orderbook publish period
func (*Version) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
if _, _, _, err := jsonparser.Get(e, "orderbook"); err != nil {
return e, nil //nolint:nilerr // No error, just return the original config
}
out, err := json.Marshal(defaultOrderbookPublishPeriod)
if err != nil {
return e, err
}
return jsonparser.Set(e, out, "orderbook", "publishPeriod")
}

View File

@@ -0,0 +1,43 @@
package v3_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
v3 "github.com/thrasher-corp/gocryptotrader/config/versions/v3"
)
func TestUpgradeExchange(t *testing.T) {
t.Parallel()
got, err := (&v3.Version{}).UpgradeExchange(context.Background(), nil)
require.NoError(t, err)
require.Nil(t, got)
payload := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false,"publishPeriod": 10000000000}}`)
expected := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false}}`)
got, err = (&v3.Version{}).UpgradeExchange(context.Background(), payload)
require.NoError(t, err)
require.Equal(t, expected, got)
}
func TestDowngradeExchange(t *testing.T) {
t.Parallel()
got, err := (&v3.Version{}).DowngradeExchange(context.Background(), nil)
require.NoError(t, err)
require.Nil(t, got)
payload := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false}}`)
expected := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false,"publishPeriod":10000000000}}`)
got, err = (&v3.Version{}).DowngradeExchange(context.Background(), payload)
require.NoError(t, err)
require.Equal(t, expected, got)
}
func TestExchanges(t *testing.T) {
t.Parallel()
assert := require.New(t)
assert.Equal([]string{"*"}, (&v3.Version{}).Exchanges())
}