Skip to content

Commit

Permalink
Config: Restructure versioning to share types
Browse files Browse the repository at this point in the history
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
gbjk committed Nov 10, 2024
1 parent c12ef71 commit c728241
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 262 deletions.
57 changes: 57 additions & 0 deletions config/versions/fixtures_test.go
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
}
13 changes: 0 additions & 13 deletions config/versions/import.go

This file was deleted.

22 changes: 0 additions & 22 deletions config/versions/testfixtures/v9997/fixture.go

This file was deleted.

55 changes: 0 additions & 55 deletions config/versions/testfixtures/v9998/fixture.go

This file was deleted.

4 changes: 0 additions & 4 deletions config/versions/testfixtures/v9999/fixture.go

This file was deleted.

24 changes: 24 additions & 0 deletions config/versions/v0.go
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
}
17 changes: 17 additions & 0 deletions config/versions/v0/types.go
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"`
}
20 changes: 0 additions & 20 deletions config/versions/v0/version.go

This file was deleted.

59 changes: 59 additions & 0 deletions config/versions/v1.go
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
}
21 changes: 21 additions & 0 deletions config/versions/v1/types.go
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"`
}
84 changes: 0 additions & 84 deletions config/versions/v1/version.go

This file was deleted.

Loading

0 comments on commit c728241

Please sign in to comment.