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: Restructure versioning to share types
This restructure allows us to share types between versions, avoids needing to import the versions, and puts the test fixtures in same package. It's a win on all fronts
- Loading branch information
Showing
16 changed files
with
222 additions
and
262 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
// TestVersion1 is an empty and incompatible Version for testing | ||
type TestVersion1 struct{} | ||
|
||
// TestVersion2 is test fixture | ||
type TestVersion2 struct { | ||
ConfigErr bool | ||
ExchErr bool | ||
} | ||
|
||
var ( | ||
errUpgrade = errors.New("do you expect me to talk?") | ||
errDowngrade = errors.New("no, I expect you to die") | ||
) | ||
|
||
// UpgradeConfig errors if v.ConfigErr is true | ||
func (v *TestVersion2) UpgradeConfig(_ context.Context, c []byte) ([]byte, error) { | ||
if v.ConfigErr { | ||
return c, errUpgrade | ||
} | ||
return c, nil | ||
} | ||
|
||
// DowngradeConfig errors if v.ConfigErr is true | ||
func (v *TestVersion2) DowngradeConfig(_ context.Context, c []byte) ([]byte, error) { | ||
if v.ConfigErr { | ||
return c, errDowngrade | ||
} | ||
return c, nil | ||
} | ||
|
||
// Exchanges returns just Juan | ||
func (v *TestVersion2) Exchanges() []string { | ||
return []string{"Juan"} | ||
} | ||
|
||
// UpgradeExchange errors if ExchErr is true | ||
func (v *TestVersion2) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) { | ||
if v.ExchErr { | ||
return e, errUpgrade | ||
} | ||
return e, nil | ||
} | ||
|
||
// DowngradeExchange errors if ExchErr is true | ||
func (v *TestVersion2) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) { | ||
if v.ExchErr { | ||
return e, errDowngrade | ||
} | ||
return e, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Version0 is a baseline version with no changes, so we can downgrade back to nothing | ||
// It does not implement any upgrade interfaces | ||
type Version0 struct { | ||
} | ||
|
||
func init() { | ||
Manager.registerVersion(0, &Version0{}) | ||
} | ||
|
||
// UpgradeConfig is an empty stub | ||
func (v *Version0) UpgradeConfig(_ context.Context, j []byte) ([]byte, error) { | ||
return j, nil | ||
} | ||
|
||
// DowngradeConfig is an empty stub | ||
func (v *Version0) DowngradeConfig(_ context.Context, j []byte) ([]byte, error) { | ||
return j, 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,17 @@ | ||
package v0 | ||
|
||
// Exchange contains a sub-section of exchange config | ||
type Exchange struct { | ||
AvailablePairs string `json:"availablePairs,omitempty"` | ||
EnabledPairs string `json:"enabledPairs,omitempty"` | ||
PairsLastUpdated int64 `json:"pairsLastUpdated,omitempty"` | ||
ConfigCurrencyPairFormat *PairFormat `json:"configCurrencyPairFormat,omitempty"` | ||
RequestCurrencyPairFormat *PairFormat `json:"requestCurrencyPairFormat,omitempty"` | ||
} | ||
|
||
// PairFormat contains pair formatting config | ||
type PairFormat struct { | ||
Uppercase bool `json:"uppercase"` | ||
Delimiter string `json:"delimiter,omitempty"` | ||
Separator string `json:"separator,omitempty"` | ||
} |
This file was deleted.
Oops, something went wrong.
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,59 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/buger/jsonparser" | ||
v0 "github.com/thrasher-corp/gocryptotrader/config/versions/v0" | ||
v1 "github.com/thrasher-corp/gocryptotrader/config/versions/v1" | ||
) | ||
|
||
// Version1 is an ExchangeVersion to upgrade currency pair format for exchanges | ||
type Version1 struct { | ||
} | ||
|
||
func init() { | ||
Manager.registerVersion(1, &Version1{}) | ||
} | ||
|
||
// Exchanges returns all exchanges: "*" | ||
func (v *Version1) Exchanges() []string { return []string{"*"} } | ||
|
||
// UpgradeExchange will upgrade currency pair format | ||
func (v *Version1) 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 := &v1.PairsManager{ | ||
UseGlobalFormat: true, | ||
LastUpdated: d.PairsLastUpdated, | ||
ConfigFormat: d.ConfigCurrencyPairFormat, | ||
RequestFormat: d.RequestCurrencyPairFormat, | ||
Pairs: v1.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 (v *Version1) DowngradeExchange(_ 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,21 @@ | ||
package v1 | ||
|
||
import v0 "github.com/thrasher-corp/gocryptotrader/config/versions/v0" | ||
|
||
// PairsManager contains exchange pair management config | ||
type PairsManager struct { | ||
BypassConfigFormatUpgrades bool `json:"bypassConfigFormatUpgrades"` | ||
RequestFormat *v0.PairFormat `json:"requestFormat,omitempty"` | ||
ConfigFormat *v0.PairFormat `json:"configFormat,omitempty"` | ||
UseGlobalFormat bool `json:"useGlobalFormat,omitempty"` | ||
LastUpdated int64 `json:"lastUpdated,omitempty"` | ||
Pairs FullStore `json:"pairs"` | ||
} | ||
|
||
// FullStore contains a pair store by asset name | ||
type FullStore map[string]struct { | ||
Enabled string `json:"enabled"` | ||
Available string `json:"available"` | ||
RequestFormat *v0.PairFormat `json:"requestFormat,omitempty"` | ||
ConfigFormat *v0.PairFormat `json:"configFormat,omitempty"` | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.