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
9 changed files
with
157 additions
and
48 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
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,73 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strconv" | ||
"time" | ||
|
||
"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 use a constant for Duration. Whilst defaults are still written to config, we need to manage default upgrades discretely. | ||
var defaultFuturesTrackingSeekDuration = strconv.Itoa(int(time.Hour) * 24 * 365) | ||
|
||
var defaultConfig = []byte(`{ | ||
"enabled": true, | ||
"verbose": false, | ||
"activelyTrackFuturesPositions": true, | ||
"futuresTrackingSeekDuration": ` + defaultFuturesTrackingSeekDuration + `, | ||
"cancelOrdersOnShutdown": false, | ||
"respectOrderHistoryLimits": true | ||
}`) | ||
|
||
// UpgradeConfig handles upgrading config for OrderManager: | ||
// * Sets OrderManager config to defaults if it doesn't exist or enabled is null | ||
// * Sets respectOrderHistoryLimits to true if it doesn't exist or is null | ||
// * Sets futuresTrackingSeekDuration to positive if it's negative | ||
func (v *Version4) UpgradeConfig(_ context.Context, e []byte) ([]byte, error) { | ||
_, valueType, _, err := jsonparser.Get(e, "orderManager", "enabled") | ||
switch { | ||
case errors.Is(err, jsonparser.KeyPathNotFoundError), valueType == jsonparser.Null: | ||
return jsonparser.Set(e, defaultConfig, "orderManager") | ||
case err != nil: | ||
return e, err | ||
} | ||
|
||
_, valueType, _, err = jsonparser.Get(e, "orderManager", "respectOrderHistoryLimits") | ||
if errors.Is(err, jsonparser.KeyPathNotFoundError) || valueType == jsonparser.Null { | ||
if e, err = jsonparser.Set(e, []byte(`true`), "orderManager", "respectOrderHistoryLimits"); err != nil { | ||
return e, err | ||
} | ||
} | ||
|
||
if i, err := jsonparser.GetInt(e, "orderManager", "futuresTrackingSeekDuration"); err != nil { | ||
if e, err = jsonparser.Set(e, []byte(defaultFuturesTrackingSeekDuration), "orderManager", "futuresTrackingSeekDuration"); err != nil { | ||
return e, err | ||
} | ||
} else if i < 0 { | ||
if e, err = jsonparser.Set(e, []byte(strconv.Itoa(0-int(i))), "orderManager", "futuresTrackingSeekDuration"); err != nil { | ||
return e, err | ||
} | ||
} | ||
return e, nil | ||
} | ||
|
||
// DowngradeConfig just reverses the futuresTrackingSeekDuration to negative, and leaves everything else alone | ||
func (v *Version4) DowngradeConfig(_ context.Context, e []byte) ([]byte, error) { | ||
if i, err := jsonparser.GetInt(e, "orderManager", "futuresTrackingSeekDuration"); err == nil && i > 0 { | ||
if e, err = jsonparser.Set(e, []byte(strconv.Itoa(0-int(i))), "orderManager", "futuresTrackingSeekDuration"); err != nil { | ||
return e, err | ||
} | ||
} | ||
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,66 @@ | ||
package versions | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
rOHLT = `"respectOrderHistoryLimits":true,` | ||
rOHLN = `"respectOrderHistoryLimits":null,` | ||
expDef = `{"orderManager":{"enabled":true,"verbose":false,"activelyTrackFuturesPositions":true,"futuresTrackingSeekDuration":31536000000000000,"cancelOrdersOnShutdown":false,"respectOrderHistoryLimits":true}}` | ||
expUser1 = `{"orderManager":{"enabled":false,"verbose":true,"activelyTrackFuturesPositions":false,"futuresTrackingSeekDuration":47000,"cancelOrdersOnShutdown":true,"respectOrderHistoryLimits":true}}` | ||
expUser2 = strings.Replace(expUser1, `mits":true`, `mits":false`, 1) | ||
) | ||
|
||
var tests = []struct { | ||
name string | ||
in string | ||
out string | ||
err error | ||
}{ | ||
{name: "Bad input should error", err: jsonparser.KeyPathNotFoundError}, | ||
{name: "Missing orderManager should use the defaults", in: "{}", out: expDef}, | ||
{name: "Enabled null should use defaults", in: strings.Replace(expDef, "true", "null", 1), out: expDef}, | ||
{name: "RespectOrderHistoryLimits should be added if missing", in: strings.Replace(expUser1, `,"respectOrderHistoryLimits":true`, "", 1), out: expUser1}, | ||
{name: "RespectOrderHistoryLimits null should default true", in: strings.Replace(expUser1, `mits":true`, `mits":null`, 1), out: expUser1}, | ||
{name: "FutureTracking should be reversed", in: strings.Replace(expUser1, "47", "-47", 1), out: expUser1}, | ||
{name: "Configured orderManager should be left alone", in: expUser2, out: expUser2}, | ||
} | ||
|
||
func TestVersion4Upgrade(t *testing.T) { | ||
for _, tt := range tests { | ||
_ = t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
out, err := new(Version4).UpgradeConfig(context.Background(), []byte(tt.in)) | ||
if tt.err != nil { | ||
require.ErrorIs(t, err, tt.err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
b := new(bytes.Buffer) | ||
require.NoError(t, json.Compact(b, out), "json.Compact must not error") | ||
require.Equal(t, tt.out, b.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestVersion4Downgrade(t *testing.T) { | ||
t.Parallel() | ||
|
||
expReversed := strings.Replace(expUser1, "47", "-47", 1) | ||
out, err := new(Version4).DowngradeConfig(context.Background(), []byte(expUser1)) | ||
require.NoError(t, err) | ||
assert.Equal(t, expReversed, string(out), "Downgrade should just reverse the futuresTrackingSeekDuration") | ||
|
||
out, err = new(Version4).DowngradeConfig(context.Background(), []byte(expReversed)) | ||
require.NoError(t, err) | ||
assert.Equal(t, expReversed, string(out), "Downgrade should leave an already negative futuresTrackingSeekDuration alone") | ||
} |
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
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