Skip to content

Commit

Permalink
Add config upgrade and downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
shazbert committed Jan 13, 2025
1 parent 6bc0bb6 commit 789da79
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions config/versions/v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package versions

import (
"context"
"encoding/json"
"time"

"github.com/buger/jsonparser"
)

// Version3 is an ExchangeVersion to remove the orderbook and publishPeriod from the exchange config
type Version3 struct {
}

func init() {
Manager.registerVersion(3, &Version3{})
}

// // Exchanges returns all exchanges: "*"

Check failure on line 19 in config/versions/v3.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported method Version3.Exchanges should be of the form "Exchanges ..." (revive)
func (v *Version3) Exchanges() []string { return []string{"*"} }

// UpgradeExchange will remove the orderbook and publishPeriod from the exchange config
func (v *Version3) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
e = jsonparser.Delete(e, "orderbook", "publishPeriod")
return e, nil
}

var defaultOrderbookPublishPeriod = time.Second * 10

// DowngradeExchange will downgrade the exchange config with the default orderbook publish period
func (v *Version3) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
if _, _, _, err := jsonparser.Get(e, "orderbook"); err != nil {
return e, nil

Check failure on line 33 in config/versions/v3.go

View workflow job for this annotation

GitHub Actions / lint

error is not nil (line 32) but it returns nil (nilerr)
}
out, err := json.Marshal(defaultOrderbookPublishPeriod)
if err != nil {
return e, err
}
return jsonparser.Set(e, out, "orderbook", "publishPeriod")
}
41 changes: 41 additions & 0 deletions config/versions/v3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package versions

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestVersion3UpgradeExchange(t *testing.T) {
t.Parallel()

got, err := (&Version3{}).UpgradeExchange(nil, nil)

Check failure on line 12 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
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 = (&Version3{}).UpgradeExchange(nil, payload)

Check failure on line 18 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
require.NoError(t, err)
require.Equal(t, got, expected)

Check failure on line 20 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

expected-actual: need to reverse actual and expected values (testifylint)
}

func TestVersion3DowngradeExchange(t *testing.T) {
t.Parallel()

got, err := (&Version3{}).DowngradeExchange(nil, nil)

Check failure on line 26 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
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 = (&Version3{}).DowngradeExchange(nil, payload)

Check failure on line 32 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
require.NoError(t, err)
require.Equal(t, got, expected)

Check failure on line 34 in config/versions/v3_test.go

View workflow job for this annotation

GitHub Actions / lint

expected-actual: need to reverse actual and expected values (testifylint)
}

func TestVersion3Exchanges(t *testing.T) {
t.Parallel()
assert := require.New(t)
assert.Equal([]string{"*"}, (&Version3{}).Exchanges())
}

0 comments on commit 789da79

Please sign in to comment.