-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
htsec.go
114 lines (100 loc) · 2.78 KB
/
htsec.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
package htsec
import (
"log"
"github.com/deb-sig/double-entry-generator/pkg/config"
"github.com/deb-sig/double-entry-generator/pkg/ir"
"github.com/deb-sig/double-entry-generator/pkg/util"
)
type Htsec struct {
}
func (h Htsec) GetAllCandidateAccounts(cfg *config.Config) map[string]bool {
// uniqMap will be used to create the concepts.
uniqMap := make(map[string]bool)
if cfg.Htsec == nil || len(cfg.Htsec.Rules) == 0 {
return uniqMap
}
for _, r := range cfg.Htsec.Rules {
if r.CashAccount != nil {
uniqMap[*r.CashAccount] = true
}
if r.PositionAccount != nil {
uniqMap[*r.PositionAccount] = true
}
if r.PnlAccount != nil {
uniqMap[*r.PnlAccount] = true
}
if r.CommissionAccount != nil {
uniqMap[*r.CommissionAccount] = true
}
}
uniqMap[cfg.DefaultCashAccount] = true
uniqMap[cfg.DefaultPositionAccount] = true
uniqMap[cfg.DefaultCommissionAccount] = true
uniqMap[cfg.DefaultPnlAccount] = true
return uniqMap
}
func (h Htsec) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provider string) (string, string, map[ir.Account]string, []string) {
if cfg.Htsec == nil || len(cfg.Htsec.Rules) == 0 {
return "", "", map[ir.Account]string{
ir.CashAccount: cfg.DefaultCashAccount,
ir.PositionAccount: cfg.DefaultPositionAccount,
ir.CommissionAccount: cfg.DefaultCommissionAccount,
ir.PnlAccount: cfg.DefaultPnlAccount,
}, nil
}
cashAccount := cfg.DefaultCashAccount
positionAccount := cfg.DefaultPositionAccount
commissionAccount := cfg.DefaultCommissionAccount
pnlAccount := cfg.DefaultPnlAccount
var err error
for _, r := range cfg.Htsec.Rules {
match := true
// get separator
sep := ","
if r.Separator != nil {
sep = *r.Separator
}
matchFunc := util.SplitFindContains
if r.FullMatch {
matchFunc = util.SplitFindEquals
}
if r.Type != nil {
match = matchFunc(*r.Type, o.TypeOriginal, sep, match)
}
if r.Item != nil {
match = matchFunc(*r.Item, o.Item, sep, match)
}
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
}
}
if match {
if r.CashAccount != nil {
cashAccount = *r.CashAccount
}
if r.PositionAccount != nil {
positionAccount = *r.PositionAccount
}
if r.CommissionAccount != nil {
commissionAccount = *r.CommissionAccount
}
if r.PnlAccount != nil {
pnlAccount = *r.PnlAccount
}
}
}
return "", "", map[ir.Account]string{
ir.CashAccount: cashAccount,
ir.PositionAccount: positionAccount,
ir.CommissionAccount: commissionAccount,
ir.PnlAccount: pnlAccount,
}, nil
}