forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config: Move OrderManager upgrade to Version management
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/buger/jsonparser" | ||
) | ||
|
||
// Version4 implements ConfigVersion | ||
type Version4 struct { | ||
} | ||
|
||
func init() { | ||
Manager.registerVersion(4, &Version4{}) | ||
} | ||
|
||
// defaultConfig contains the stateless V4 representation of orderbookManager | ||
// Note: Do not be tempted to an constant for Duration. Whilst defaults are still written to config, we need to manage default upgrades discretely | ||
var defaultConfig = []byte(`{ | ||
"enabled": true, | ||
"verbose": false, | ||
"activelyTrackFuturesPositions": true, | ||
"futuresTrackingSeekDuration": 31536000000000000, | ||
"respectOrderHistoryLimits": true, | ||
"cancelOrdersOnShutdown": false | ||
}`) | ||
|
||
// UpgradeConfig sets OrderManager config to defaults if it doesn't exist, and sets respectOrderHistoryLimits to true if it doesn't exist | ||
func (v *Version4) UpgradeConfig(_ context.Context, e []byte) ([]byte, error) { | ||
_, _, _, err := jsonparser.Get(e, "orderManager") //nolint:dogsled // Ignored return values really not needed | ||
switch { | ||
case errors.Is(err, jsonparser.KeyPathNotFoundError): | ||
return jsonparser.Set(e, defaultConfig, "orderManager") | ||
case err == nil: | ||
_, err = jsonparser.GetBoolean(e, "orderManager", "respectOrderHistoryLimits") | ||
if errors.Is(err, jsonparser.KeyPathNotFoundError) { | ||
return jsonparser.Set(e, []byte(`true`), "orderManager", "respectOrderHistoryLimits") | ||
} | ||
} | ||
return e, err | ||
} | ||
|
||
// DowngradeConfig doesn't need to do anything for this version, since it's a lossy downgrade | ||
func (v *Version4) DowngradeConfig(_ context.Context, e []byte) ([]byte, error) { | ||
return e, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package versions | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestVersion4Upgrade(t *testing.T) { | ||
t.Parallel() | ||
|
||
out, err := new(Version4).UpgradeConfig(context.Background(), []byte(`{}`)) | ||
require.NoError(t, err) | ||
b := new(bytes.Buffer) | ||
require.NoError(t, json.Compact(b, out), "json.Compact must not error") | ||
exp := `{"orderManager":{"enabled":true,"verbose":false,"activelyTrackFuturesPositions":true,"futuresTrackingSeekDuration":31536000000000000,"respectOrderHistoryLimits":true,"cancelOrdersOnShutdown":false}}` | ||
require.Equal(t, exp, b.String()) | ||
|
||
in := []byte(`{"orderManager":{"enabled":false,"verbose":true,"activelyTrackFuturesPositions":true,"futuresTrackingSeekDuration":3600000,"cancelOrdersOnShutdown":false}}`) | ||
out, err = new(Version4).UpgradeConfig(context.Background(), in) | ||
require.NoError(t, err) | ||
exp = `{"orderManager":{"enabled":false,"verbose":true,"activelyTrackFuturesPositions":true,"futuresTrackingSeekDuration":3600000,"cancelOrdersOnShutdown":false,"respectOrderHistoryLimits":true}}` | ||
require.Equal(t, exp, string(out)) | ||
} | ||
|
||
func TestVersion4Downgrade(t *testing.T) { | ||
t.Parallel() | ||
in := []byte(`{"orderManager":{"enabled":false,"verbose":true}}`) | ||
out, err := new(Version4).DowngradeConfig(context.Background(), in) | ||
require.NoError(t, err) | ||
assert.Equal(t, string(in), string(out), "Downgrade should not touch the config") | ||
} |