-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathconfig.go
708 lines (557 loc) · 19.8 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package bbgo
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"runtime"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
)
// DefaultFeeRate set the fee rate for most cases
// BINANCE uses 0.1% for both maker and taker
//
// for BNB holders, it's 0.075% for both maker and taker
//
// MAX uses 0.050% for maker and 0.15% for taker
var DefaultFeeRate = fixedpoint.NewFromFloat(0.075 * 0.01)
type PnLReporterConfig struct {
AverageCostBySymbols datatype.StringSlice `json:"averageCostBySymbols" yaml:"averageCostBySymbols"`
Of datatype.StringSlice `json:"of" yaml:"of"`
When datatype.StringSlice `json:"when" yaml:"when"`
}
// ExchangeStrategyMount wraps the SingleExchangeStrategy with the ExchangeSession name for mounting
type ExchangeStrategyMount struct {
// Mounts contains the ExchangeSession name to mount
Mounts []string `json:"mounts"`
// Strategy is the strategy we loaded from config
Strategy SingleExchangeStrategy `json:"strategy"`
}
func (m *ExchangeStrategyMount) Map() (map[string]interface{}, error) {
strategyID := m.Strategy.ID()
var params map[string]interface{}
out, err := json.Marshal(m.Strategy)
if err != nil {
return nil, err
}
if err := json.Unmarshal(out, ¶ms); err != nil {
return nil, err
}
return map[string]interface{}{
"on": m.Mounts,
strategyID: params,
}, nil
}
type SlackNotification struct {
DefaultChannel string `json:"defaultChannel,omitempty" yaml:"defaultChannel,omitempty"`
ErrorChannel string `json:"errorChannel,omitempty" yaml:"errorChannel,omitempty"`
}
type SlackNotificationRouting struct {
Trade string `json:"trade,omitempty" yaml:"trade,omitempty"`
Order string `json:"order,omitempty" yaml:"order,omitempty"`
SubmitOrder string `json:"submitOrder,omitempty" yaml:"submitOrder,omitempty"`
PnL string `json:"pnL,omitempty" yaml:"pnL,omitempty"`
}
type TelegramNotification struct {
Broadcast bool `json:"broadcast" yaml:"broadcast"`
}
type NotificationSwitches struct {
Trade bool `json:"trade" yaml:"trade"`
Position bool `json:"position" yaml:"position"`
OrderUpdate bool `json:"orderUpdate" yaml:"orderUpdate"`
SubmitOrder bool `json:"submitOrder" yaml:"submitOrder"`
}
type NotificationConfig struct {
Slack *SlackNotification `json:"slack,omitempty" yaml:"slack,omitempty"`
Telegram *TelegramNotification `json:"telegram,omitempty" yaml:"telegram,omitempty"`
Switches *NotificationSwitches `json:"switches" yaml:"switches"`
}
type LoggingConfig struct {
Trade bool `json:"trade,omitempty"`
Order bool `json:"order,omitempty"`
Balance bool `json:"balance,omitempty"`
FilledOrderOnly bool `json:"filledOrder,omitempty"`
Fields map[string]interface{} `json:"fields,omitempty"`
}
type Session struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ExchangeName string `json:"exchange" yaml:"exchange"`
EnvVarPrefix string `json:"envVarPrefix" yaml:"envVarPrefix"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Secret string `json:"secret,omitempty" yaml:"secret,omitempty"`
PublicOnly bool `json:"publicOnly,omitempty" yaml:"publicOnly"`
Margin bool `json:"margin,omitempty" yaml:"margin,omitempty"`
IsolatedMargin bool `json:"isolatedMargin,omitempty" yaml:"isolatedMargin,omitempty"`
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"`
}
//go:generate go run github.com/dmarkham/enumer -type=BacktestFeeMode -transform=snake -trimprefix BacktestFeeMode -yaml -json
type BacktestFeeMode int
const (
// BackTestFeeModeQuoteFee is designed for clean position but which also counts the fee in the quote balance.
// buy order = quote currency fee
// sell order = quote currency fee
BacktestFeeModeQuote BacktestFeeMode = iota // quote
// BackTestFeeModeNativeFee is the default crypto exchange fee mode.
// buy order = base currency fee
// sell order = quote currency fee
BacktestFeeModeNative // BackTestFeeMode = "native"
// BackTestFeeModeFeeToken is the mode which calculates fee from the outside of the balances.
// the fee will not be included in the balances nor the profit.
BacktestFeeModeToken // BackTestFeeMode = "token"
)
type Backtest struct {
StartTime types.LooseFormatTime `json:"startTime,omitempty" yaml:"startTime,omitempty"`
EndTime *types.LooseFormatTime `json:"endTime,omitempty" yaml:"endTime,omitempty"`
// RecordTrades is an option, if set to true, back-testing should record the trades into database
RecordTrades bool `json:"recordTrades,omitempty" yaml:"recordTrades,omitempty"`
// Deprecated:
// Account is deprecated, use Accounts instead
Account map[string]BacktestAccount `json:"account" yaml:"account"`
FeeMode BacktestFeeMode `json:"feeMode" yaml:"feeMode"`
Accounts map[string]BacktestAccount `json:"accounts" yaml:"accounts"`
Symbols []string `json:"symbols" yaml:"symbols"`
Sessions []string `json:"sessions" yaml:"sessions"`
// sync 1 second interval KLines
SyncSecKLines bool `json:"syncSecKLines,omitempty" yaml:"syncSecKLines,omitempty"`
}
func (b *Backtest) GetAccount(n string) BacktestAccount {
accountConfig, ok := b.Accounts[n]
if ok {
return accountConfig
}
accountConfig, ok = b.Account[n]
if ok {
return accountConfig
}
return DefaultBacktestAccount
}
type BacktestAccount struct {
MakerFeeRate fixedpoint.Value `json:"makerFeeRate,omitempty" yaml:"makerFeeRate,omitempty"`
TakerFeeRate fixedpoint.Value `json:"takerFeeRate,omitempty" yaml:"takerFeeRate,omitempty"`
Balances BacktestAccountBalanceMap `json:"balances" yaml:"balances"`
}
var DefaultBacktestAccount = BacktestAccount{
MakerFeeRate: fixedpoint.MustNewFromString("0.050%"),
TakerFeeRate: fixedpoint.MustNewFromString("0.075%"),
Balances: BacktestAccountBalanceMap{
"USDT": fixedpoint.NewFromFloat(10000),
},
}
type BA BacktestAccount
func (b *BacktestAccount) UnmarshalYAML(value *yaml.Node) error {
bb := &BA{MakerFeeRate: DefaultFeeRate, TakerFeeRate: DefaultFeeRate}
if err := value.Decode(bb); err != nil {
return err
}
*b = BacktestAccount(*bb)
return nil
}
func (b *BacktestAccount) UnmarshalJSON(input []byte) error {
bb := &BA{MakerFeeRate: DefaultFeeRate, TakerFeeRate: DefaultFeeRate}
if err := json.Unmarshal(input, bb); err != nil {
return err
}
*b = BacktestAccount(*bb)
return nil
}
type BacktestAccountBalanceMap map[string]fixedpoint.Value
func (m BacktestAccountBalanceMap) BalanceMap() types.BalanceMap {
balances := make(types.BalanceMap)
for currency, value := range m {
balances[currency] = types.Balance{
Currency: currency,
Available: value,
Locked: fixedpoint.Zero,
}
}
return balances
}
type PersistenceConfig struct {
Redis *service.RedisPersistenceConfig `json:"redis,omitempty" yaml:"redis,omitempty"`
Json *service.JsonPersistenceConfig `json:"json,omitempty" yaml:"json,omitempty"`
}
type BuildTargetConfig struct {
Name string `json:"name" yaml:"name"`
Arch string `json:"arch" yaml:"arch"`
OS string `json:"os" yaml:"os"`
LDFlags datatype.StringSlice `json:"ldflags,omitempty" yaml:"ldflags,omitempty"`
GCFlags datatype.StringSlice `json:"gcflags,omitempty" yaml:"gcflags,omitempty"`
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
}
type BuildConfig struct {
BuildDir string `json:"buildDir,omitempty" yaml:"buildDir,omitempty"`
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
Targets []BuildTargetConfig `json:"targets,omitempty" yaml:"targets,omitempty"`
}
func GetNativeBuildTargetConfig() BuildTargetConfig {
return BuildTargetConfig{
Name: "bbgow",
Arch: runtime.GOARCH,
OS: runtime.GOOS,
}
}
type SyncSymbol struct {
Symbol string `json:"symbol" yaml:"symbol"`
Session string `json:"session" yaml:"session"`
}
func (ss *SyncSymbol) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
var s string
if err = unmarshal(&s); err == nil {
aa := strings.SplitN(s, ":", 2)
if len(aa) > 1 {
ss.Session = aa[0]
ss.Symbol = aa[1]
} else {
ss.Symbol = aa[0]
}
return nil
}
type localSyncSymbol SyncSymbol
var ssNew localSyncSymbol
if err = unmarshal(&ssNew); err == nil {
*ss = SyncSymbol(ssNew)
return nil
}
return err
}
func categorizeSyncSymbol(slice []SyncSymbol) (map[string][]string, []string) {
var rest []string
var m = make(map[string][]string)
for _, ss := range slice {
if len(ss.Session) > 0 {
m[ss.Session] = append(m[ss.Session], ss.Symbol)
} else {
rest = append(rest, ss.Symbol)
}
}
return m, rest
}
type SyncConfig struct {
// Sessions to sync, if ignored, all defined sessions will sync
Sessions []string `json:"sessions,omitempty" yaml:"sessions,omitempty"`
// Symbols is the list of session:symbol pair to sync, if ignored, symbols wlll be discovered by your existing crypto balances
// Valid formats are: {session}:{symbol}, {symbol} or in YAML object form {symbol: "BTCUSDT", session:"max" }
Symbols []SyncSymbol `json:"symbols,omitempty" yaml:"symbols,omitempty"`
// DepositHistory is for syncing deposit history
DepositHistory bool `json:"depositHistory" yaml:"depositHistory"`
// WithdrawHistory is for syncing withdraw history
WithdrawHistory bool `json:"withdrawHistory" yaml:"withdrawHistory"`
// RewardHistory is for syncing reward history
RewardHistory bool `json:"rewardHistory" yaml:"rewardHistory"`
// MarginHistory is for syncing margin related history: loans, repays, interests and liquidations
MarginHistory bool `json:"marginHistory" yaml:"marginHistory"`
MarginAssets []string `json:"marginAssets" yaml:"marginAssets"`
// Since is the date where you want to start syncing data
Since *types.LooseFormatTime `json:"since,omitempty"`
// UserDataStream is for real-time sync with websocket user data stream
UserDataStream *struct {
Trades bool `json:"trades,omitempty" yaml:"trades,omitempty"`
FilledOrders bool `json:"filledOrders,omitempty" yaml:"filledOrders,omitempty"`
} `json:"userDataStream,omitempty" yaml:"userDataStream,omitempty"`
}
type GoogleSpreadSheetServiceConfig struct {
JsonTokenFile string `json:"jsonTokenFile" yaml:"jsonTokenFile"`
SpreadSheetID string `json:"spreadSheetId" yaml:"spreadSheetId"`
}
type ServiceConfig struct {
GoogleSpreadSheetService *GoogleSpreadSheetServiceConfig `json:"googleSpreadSheet" yaml:"googleSpreadSheet"`
}
type DatabaseConfig struct {
Driver string `json:"driver" yaml:"driver"`
DSN string `json:"dsn" yaml:"dsn"`
ExtraMigrationPackages []string `json:"extraMigrationPackages" yaml:"extraMigrationPackages"`
}
type EnvironmentConfig struct {
DisableDefaultKLineSubscription bool `json:"disableDefaultKLineSubscription"`
DisableHistoryKLinePreload bool `json:"disableHistoryKLinePreload"`
// DisableStartUpBalanceQuery disables the balance query in the startup process
// which initializes the session.Account with the QueryAccount method.
DisableStartupBalanceQuery bool `json:"disableStartupBalanceQuery"`
DisableSessionTradeBuffer bool `json:"disableSessionTradeBuffer"`
DisableMarketDataStore bool `json:"disableMarketDataStore"`
MaxSessionTradeBufferSize int `json:"maxSessionTradeBufferSize"`
SyncBufferPeriod *types.Duration `json:"syncBufferPeriod"`
}
type Config struct {
Build *BuildConfig `json:"build,omitempty" yaml:"build,omitempty"`
// Imports is deprecated
// Deprecated: use BuildConfig instead
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
Backtest *Backtest `json:"backtest,omitempty" yaml:"backtest,omitempty"`
Sync *SyncConfig `json:"sync,omitempty" yaml:"sync,omitempty"`
Notifications *NotificationConfig `json:"notifications,omitempty" yaml:"notifications,omitempty"`
Persistence *PersistenceConfig `json:"persistence,omitempty" yaml:"persistence,omitempty"`
Service *ServiceConfig `json:"services,omitempty" yaml:"services,omitempty"`
DatabaseConfig *DatabaseConfig `json:"database,omitempty" yaml:"database,omitempty"`
Environment *EnvironmentConfig `json:"environment,omitempty" yaml:"environment,omitempty"`
Sessions map[string]*ExchangeSession `json:"sessions,omitempty" yaml:"sessions,omitempty"`
RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"`
Logging *LoggingConfig `json:"logging,omitempty"`
ExchangeStrategies []ExchangeStrategyMount `json:"-" yaml:"-"`
CrossExchangeStrategies []CrossExchangeStrategy `json:"-" yaml:"-"`
PnLReporters []PnLReporterConfig `json:"reportPnL,omitempty" yaml:"reportPnL,omitempty"`
}
func (c *Config) Map() (map[string]interface{}, error) {
text, err := json.Marshal(c)
if err != nil {
return nil, err
}
var data map[string]interface{}
err = json.Unmarshal(text, &data)
if err != nil {
return nil, err
}
// convert strategy config back to the DSL format
var exchangeStrategies []map[string]interface{}
for _, m := range c.ExchangeStrategies {
params, err := m.Map()
if err != nil {
return nil, err
}
exchangeStrategies = append(exchangeStrategies, params)
}
if len(exchangeStrategies) > 0 {
data["exchangeStrategies"] = exchangeStrategies
}
var crossExchangeStrategies []map[string]interface{}
for _, st := range c.CrossExchangeStrategies {
strategyID := st.ID()
var params Stash
out, err := json.Marshal(st)
if err != nil {
return nil, err
}
if err := json.Unmarshal(out, ¶ms); err != nil {
return nil, err
}
crossExchangeStrategies = append(crossExchangeStrategies, map[string]interface{}{
strategyID: params,
})
}
if len(crossExchangeStrategies) > 0 {
data["crossExchangeStrategies"] = crossExchangeStrategies
}
return data, err
}
func (c *Config) YAML() ([]byte, error) {
m, err := c.Map()
if err != nil {
return nil, err
}
var buf bytes.Buffer
var enc = yaml.NewEncoder(&buf)
enc.SetIndent(2)
err = enc.Encode(m)
return buf.Bytes(), err
}
func (c *Config) GetSignature() string {
var s string
var ps []string
// for single exchange strategy
if len(c.ExchangeStrategies) == 1 && len(c.CrossExchangeStrategies) == 0 {
mount := c.ExchangeStrategies[0].Mounts[0]
ps = append(ps, mount)
strategy := c.ExchangeStrategies[0].Strategy
id := strategy.ID()
ps = append(ps, id)
if symbol, ok := dynamic.LookupSymbolField(reflect.ValueOf(strategy)); ok {
ps = append(ps, symbol)
}
}
startTime := c.Backtest.StartTime.Time()
ps = append(ps, startTime.Format("2006-01-02"))
if c.Backtest.EndTime != nil {
endTime := c.Backtest.EndTime.Time()
ps = append(ps, endTime.Format("2006-01-02"))
}
s = strings.Join(ps, "_")
return s
}
type Stash map[string]interface{}
func loadStash(config []byte) (Stash, error) {
stash := make(Stash)
if err := yaml.Unmarshal(config, stash); err != nil {
return nil, err
}
return stash, nil
}
func LoadBuildConfig(configFile string) (*Config, error) {
var config Config
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(content, &config); err != nil {
return nil, err
}
// for backward compatible
if config.Build == nil {
if len(config.Imports) > 0 {
config.Build = &BuildConfig{
BuildDir: "build",
Imports: config.Imports,
Targets: []BuildTargetConfig{
{Name: "bbgow-amd64-darwin", Arch: "amd64", OS: "darwin"},
{Name: "bbgow-amd64-linux", Arch: "amd64", OS: "linux"},
},
}
}
}
return &config, nil
}
// Load parses the config
func Load(configFile string, loadStrategies bool) (*Config, error) {
var config Config
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(content, &config); err != nil {
return nil, err
}
// for backward compatible
if config.Build == nil {
config.Build = &BuildConfig{
BuildDir: "build",
Imports: config.Imports,
Targets: []BuildTargetConfig{
{Name: "bbgow-amd64-darwin", Arch: "amd64", OS: "darwin"},
{Name: "bbgow-amd64-linux", Arch: "amd64", OS: "linux"},
},
}
}
stash, err := loadStash(content)
if err != nil {
return nil, err
}
if loadStrategies {
if err := loadExchangeStrategies(&config, stash); err != nil {
return nil, err
}
if err := loadCrossExchangeStrategies(&config, stash); err != nil {
return nil, err
}
}
return &config, nil
}
func loadCrossExchangeStrategies(config *Config, stash Stash) (err error) {
exchangeStrategiesConf, ok := stash["crossExchangeStrategies"]
if !ok {
return nil
}
if len(LoadedCrossExchangeStrategies) == 0 {
return errors.New("no cross exchange strategy is registered")
}
configList, ok := exchangeStrategiesConf.([]interface{})
if !ok {
return errors.New("expecting list in crossExchangeStrategies")
}
for _, entry := range configList {
configStash, ok := entry.(Stash)
if !ok {
return fmt.Errorf("strategy config should be a map, given: %T %+v", entry, entry)
}
for id, conf := range configStash {
// look up the real struct type
if st, ok := LoadedCrossExchangeStrategies[id]; ok {
val, err := reUnmarshal(conf, st)
if err != nil {
return err
}
config.CrossExchangeStrategies = append(config.CrossExchangeStrategies, val.(CrossExchangeStrategy))
}
}
}
return nil
}
func NewStrategyFromMap(id string, conf interface{}) (SingleExchangeStrategy, error) {
if st, ok := LoadedExchangeStrategies[id]; ok {
val, err := reUnmarshal(conf, st)
if err != nil {
return nil, err
}
return val.(SingleExchangeStrategy), nil
}
return nil, fmt.Errorf("strategy %s not found", id)
}
func loadExchangeStrategies(config *Config, stash Stash) (err error) {
exchangeStrategiesConf, ok := stash["exchangeStrategies"]
if !ok {
exchangeStrategiesConf, ok = stash["strategies"]
if !ok {
return nil
}
}
if len(LoadedExchangeStrategies) == 0 {
return errors.New("no exchange strategy is registered")
}
configList, ok := exchangeStrategiesConf.([]interface{})
if !ok {
return errors.New("expecting list in exchangeStrategies")
}
for _, entry := range configList {
configStash, ok := entry.(Stash)
if !ok {
return fmt.Errorf("strategy config should be a map, given: %T %+v", entry, entry)
}
var mounts []string
if val, ok := configStash["on"]; ok {
switch tv := val.(type) {
case []string:
mounts = append(mounts, tv...)
case string:
mounts = append(mounts, tv)
case []interface{}:
for _, f := range tv {
s, ok := f.(string)
if !ok {
return fmt.Errorf("%+v (%T) is not a string", f, f)
}
mounts = append(mounts, s)
}
default:
return fmt.Errorf("unexpected mount type: %T value: %+v", val, val)
}
}
for id, conf := range configStash {
// look up the real struct type
if _, ok := LoadedExchangeStrategies[id]; ok {
st, err := NewStrategyFromMap(id, conf)
if err != nil {
return err
}
config.ExchangeStrategies = append(config.ExchangeStrategies, ExchangeStrategyMount{
Mounts: mounts,
Strategy: st,
})
} else if id != "on" && id != "off" {
// Show error when we didn't find the Strategy
return fmt.Errorf("strategy %s in config not found", id)
}
}
}
return nil
}
func reUnmarshal(conf interface{}, tpe interface{}) (interface{}, error) {
// get the type "*Strategy"
rt := reflect.TypeOf(tpe)
// allocate new object from the given type
val := reflect.New(rt)
// now we have &(*Strategy) -> **Strategy
valRef := val.Interface()
plain, err := json.Marshal(conf)
if err != nil {
return nil, err
}
if err := json.Unmarshal(plain, valRef); err != nil {
return nil, errors.Wrapf(err, "json parsing error, given payload: %s", plain)
}
return val.Elem().Interface(), nil
}