resolve cyclic imports

This commit is contained in:
c9s 2021-02-21 01:01:39 +08:00
parent 6845db6dd3
commit fa4e813729
9 changed files with 37 additions and 41 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
)
@ -133,20 +134,9 @@ func (m BacktestAccountBalanceMap) BalanceMap() types.BalanceMap {
return balances
}
type RedisPersistenceConfig struct {
Host string `json:"host" env:"REDIS_HOST"`
Port string `json:"port" env:"REDIS_PORT"`
Password string `json:"password" env:"REDIS_PASSWORD"`
DB int `json:"db" env:"REDIS_DB"`
}
type JsonPersistenceConfig struct {
Directory string `json:"directory"`
}
type PersistenceConfig struct {
Redis *RedisPersistenceConfig `json:"redis,omitempty" yaml:"redis,omitempty"`
Json *JsonPersistenceConfig `json:"json,omitempty" yaml:"json,omitempty"`
Redis *service.RedisPersistenceConfig `json:"redis,omitempty" yaml:"redis,omitempty"`
Json *service.JsonPersistenceConfig `json:"json,omitempty" yaml:"json,omitempty"`
}
type BuildTargetConfig struct {

View File

@ -21,7 +21,7 @@ type Persistence struct {
Facade *service.PersistenceServiceFacade `json:"-" yaml:"-"`
}
func (p *Persistence) backendService(t string) (service PersistenceService, err error) {
func (p *Persistence) backendService(t string) (service service.PersistenceService, err error) {
switch t {
case "json":
service = p.Facade.Json
@ -67,13 +67,3 @@ func (p *Persistence) Save(val interface{}, subIDs ...string) error {
return store.Save(val)
}
type PersistenceService interface {
NewStore(id string, subIDs ...string) Store
}
type Store interface {
Load(val interface{}) error
Save(val interface{}) error
Reset() error
}

View File

@ -88,7 +88,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
return nil
}
func newNotificationSystem(userConfig *bbgo.Config, persistence bbgo.PersistenceService) (*bbgo.Notifiability, error) {
func newNotificationSystem(userConfig *bbgo.Config, persistence service.PersistenceService) (*bbgo.Notifiability, error) {
notification := &bbgo.Notifiability{
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
@ -180,7 +180,7 @@ func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userCo
}
// configure persistence service, by default we will use memory service
var persistence bbgo.PersistenceService = service.NewMemoryService()
var persistence service.PersistenceService = service.NewMemoryService()
if environ.PersistenceServiceFacade != nil {
if environ.PersistenceServiceFacade.Redis != nil {
persistence = environ.PersistenceServiceFacade.Redis

View File

@ -8,7 +8,7 @@ import (
"github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/service"
)
var log = logrus.WithField("service", "telegram")
@ -27,7 +27,7 @@ func NewSession(key *otp.Key) Session {
//go:generate callbackgen -type Interaction
type Interaction struct {
store bbgo.Store
store service.Store
bot *telebot.Bot
@ -39,7 +39,7 @@ type Interaction struct {
AuthCallbacks []func(user *telebot.User)
}
func NewInteraction(bot *telebot.Bot, store bbgo.Store) *Interaction {
func NewInteraction(bot *telebot.Bot, store service.Store) *Interaction {
interaction := &Interaction{
store: store,
bot: bot,

View File

@ -3,8 +3,6 @@ package service
import (
"reflect"
"strings"
"github.com/c9s/bbgo/pkg/bbgo"
)
type MemoryService struct {
@ -17,7 +15,7 @@ func NewMemoryService() *MemoryService {
}
}
func (s *MemoryService) NewStore(id string, subIDs ...string) bbgo.Store {
func (s *MemoryService) NewStore(id string, subIDs ...string) Store {
key := strings.Join(append([]string{id}, subIDs...), ":")
return &MemoryStore{
Key: key,

View File

@ -0,0 +1,23 @@
package service
type PersistenceService interface {
NewStore(id string, subIDs ...string) Store
}
type Store interface {
Load(val interface{}) error
Save(val interface{}) error
Reset() error
}
type RedisPersistenceConfig struct {
Host string `json:"host" env:"REDIS_HOST"`
Port string `json:"port" env:"REDIS_PORT"`
Password string `json:"password" env:"REDIS_PASSWORD"`
DB int `json:"db" env:"REDIS_DB"`
}
type JsonPersistenceConfig struct {
Directory string `json:"directory"`
}

View File

@ -5,15 +5,13 @@ import (
"io/ioutil"
"os"
"path/filepath"
"github.com/c9s/bbgo/pkg/bbgo"
)
type JsonPersistenceService struct {
Directory string
}
func (s *JsonPersistenceService) NewStore(id string, subIDs ...string) bbgo.Store {
func (s *JsonPersistenceService) NewStore(id string, subIDs ...string) Store {
return &JsonStore{
ID: id,
Directory: filepath.Join(append([]string{s.Directory}, subIDs...)...),

View File

@ -7,15 +7,13 @@ import (
"strings"
"github.com/go-redis/redis/v8"
"github.com/c9s/bbgo/pkg/bbgo"
)
type RedisPersistenceService struct {
redis *redis.Client
}
func NewRedisPersistenceService(config *bbgo.RedisPersistenceConfig) *RedisPersistenceService {
func NewRedisPersistenceService(config *RedisPersistenceConfig) *RedisPersistenceService {
client := redis.NewClient(&redis.Options{
Addr: net.JoinHostPort(config.Host, config.Port),
// Username: "", // username is only for redis 6.0
@ -28,7 +26,7 @@ func NewRedisPersistenceService(config *bbgo.RedisPersistenceConfig) *RedisPersi
}
}
func (s *RedisPersistenceService) NewStore(id string, subIDs ...string) bbgo.Store {
func (s *RedisPersistenceService) NewStore(id string, subIDs ...string) Store {
if len(subIDs) > 0 {
id += ":" + strings.Join(subIDs, ":")
}

View File

@ -5,12 +5,11 @@ import (
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
func TestRedisPersistentService(t *testing.T) {
redisService := NewRedisPersistenceService(&bbgo.RedisPersistenceConfig{
redisService := NewRedisPersistenceService(&RedisPersistenceConfig{
Host: "127.0.0.1",
Port: "6379",
DB: 0,