Skip to content

Commit

Permalink
fixup! Config: Restructure versioning to share types
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 13, 2024
1 parent 57842fd commit bdb8964
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 74 deletions.
17 changes: 0 additions & 17 deletions config/versions/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,3 @@ func (v *TestVersion2) DowngradeExchange(_ context.Context, e []byte) ([]byte, e
}
return e, nil
}

// TestVersion3 is a Disabled fixture
type TestVersion3 struct {
}

// Disabled implements the DisabledVersion interface
func (v *TestVersion3) Disabled() {}

// UpgradeConfig implements the ConfigdVersion interface
func (v *TestVersion3) UpgradeConfig(_ context.Context, c []byte) ([]byte, error) {
return c, nil
}

// DowngradeConfig implements the ConfigdVersion interface
func (v *TestVersion3) DowngradeConfig(_ context.Context, c []byte) ([]byte, error) {
return c, nil
}
43 changes: 0 additions & 43 deletions config/versions/import.go

This file was deleted.

4 changes: 4 additions & 0 deletions config/versions/v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
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
Expand Down
4 changes: 4 additions & 0 deletions config/versions/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
type Version1 struct {
}

func init() {
Manager.registerVersion(1, &Version1{})
}

// Exchanges returns all exchanges: "*"
func (v *Version1) Exchanges() []string { return []string{"*"} }

Expand Down
4 changes: 4 additions & 0 deletions config/versions/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
// Version2 is an ExchangeVersion to change the name of GDAX to CoinbasePro
type Version2 struct{}

func init() {
Manager.registerVersion(2, &Version2{})
}

const (
from = "GDAX"
to = "CoinbasePro"
Expand Down
11 changes: 2 additions & 9 deletions config/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ versions handles config upgrades and downgrades
- Versions must be registered in import.go
- Versions must implement ExchangeVersion or ConfigVersion, and may implement both
- Versions implementing DisabledVersion will be silently ignored, but must be the highest version numbers to avoid errVersionSequence
*/
package versions

Expand All @@ -35,11 +33,6 @@ var (
errApplyingVersion = errors.New("error applying version")
)

// DisabledVersion allows authors to rollback changes easily during development
type DisabledVersion interface {
Disabled()
}

// ConfigVersion is a version that affects the general configuration
type ConfigVersion interface {
UpgradeConfig(context.Context, []byte) ([]byte, error)
Expand Down Expand Up @@ -173,11 +166,10 @@ func exchangeDeploy(ctx context.Context, patch ExchangeVersion, method func(Exch
// Versions should be added sequentially without gaps, in import.go init
// Any errors will also added to the registry for reporting later
func (m *manager) registerVersion(ver int, v any) {
log.Println("Registering version")
m.m.Lock()
defer m.m.Unlock()
switch v.(type) {
case DisabledVersion:
return
case ExchangeVersion, ConfigVersion:
default:
m.errors = common.AppendError(m.errors, fmt.Errorf("%w: %v", errVersionIncompatible, ver))
Expand All @@ -187,6 +179,7 @@ func (m *manager) registerVersion(ver int, v any) {
m.errors = common.AppendError(m.errors, fmt.Errorf("%w: %v", errVersionSequence, ver))
return
}
log.Println("Registering version: done")
m.versions = append(m.versions, v)
}

Expand Down
5 changes: 0 additions & 5 deletions config/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func TestRegisterVersion(t *testing.T) {
m.registerVersion(2, &TestVersion2{})
assert.ErrorIs(t, m.errors, errVersionSequence)
assert.ErrorContains(t, m.errors, ": 2")

m.errors = nil
m.registerVersion(3, &TestVersion3{})
assert.NoError(t, m.errors)
assert.Len(t, m.versions, 1, "Disabled Versions should not be registered")
}

func TestLatest(t *testing.T) {
Expand Down

0 comments on commit bdb8964

Please sign in to comment.