forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
147 lines (115 loc) · 3.86 KB
/
settings.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
package quickfix
import (
"bufio"
"fmt"
"io"
"regexp"
"github.com/quickfixgo/quickfix/config"
)
//The Settings type represents a collection of global and session settings.
type Settings struct {
globalSettings *SessionSettings
sessionSettings map[SessionID]*SessionSettings
}
//Init initializes or resets a Settings instance
func (s *Settings) Init() {
s.globalSettings = NewSessionSettings()
s.sessionSettings = make(map[SessionID]*SessionSettings)
}
func (s *Settings) lazyInit() {
if s.globalSettings == nil {
s.Init()
}
}
//NewSettings creates a Settings instance
func NewSettings() *Settings {
s := &Settings{}
s.Init()
return s
}
func sessionIDFromSessionSettings(globalSettings *SessionSettings, sessionSettings *SessionSettings) SessionID {
sessionID := SessionID{}
for _, settings := range []*SessionSettings{globalSettings, sessionSettings} {
if settings.HasSetting(config.BeginString) {
sessionID.BeginString, _ = settings.Setting(config.BeginString)
}
if settings.HasSetting(config.TargetCompID) {
sessionID.TargetCompID, _ = settings.Setting(config.TargetCompID)
}
if settings.HasSetting(config.SenderCompID) {
sessionID.SenderCompID, _ = settings.Setting(config.SenderCompID)
}
if settings.HasSetting(config.SessionQualifier) {
sessionID.Qualifier, _ = settings.Setting(config.SessionQualifier)
}
}
return sessionID
}
//ParseSettings creates and initializes a Settings instance with config parsed from a Reader.
//Returns error if the config is has parse errors
func ParseSettings(reader io.Reader) (*Settings, error) {
s := NewSettings()
scanner := bufio.NewScanner(reader)
blankRegEx := regexp.MustCompile(`^\s*$`)
commentRegEx := regexp.MustCompile(`^#.*`)
defaultRegEx := regexp.MustCompile(`^\[(?i)DEFAULT\]\s*$`)
sessionRegEx := regexp.MustCompile(`^\[(?i)SESSION\]\s*$`)
settingRegEx := regexp.MustCompile(`^([^=]*)=(.*)$`)
var settings *SessionSettings
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
switch {
case commentRegEx.MatchString(line) || blankRegEx.MatchString(line):
continue
case defaultRegEx.MatchString(line):
settings = s.GlobalSettings()
case sessionRegEx.MatchString(line):
if settings != nil && settings != s.GlobalSettings() {
if _, err := s.AddSession(settings); err != nil {
return nil, err
}
}
settings = NewSessionSettings()
case settingRegEx.MatchString(line):
parts := settingRegEx.FindStringSubmatch(line)
settings.Set(parts[1], parts[2])
default:
return s, fmt.Errorf("error parsing line %v", lineNumber)
}
}
if err := scanner.Err(); err != nil {
return s, err
}
if settings == nil || settings == s.GlobalSettings() {
return s, fmt.Errorf("no sessions declared")
}
_, err := s.AddSession(settings)
return s, err
}
//GlobalSettings are default setting inherited by all session settings.
func (s *Settings) GlobalSettings() *SessionSettings {
s.lazyInit()
return s.globalSettings
}
//SessionSettings return all session settings overlaying globalsettings.
func (s *Settings) SessionSettings() map[SessionID]*SessionSettings {
allSessionSettings := make(map[SessionID]*SessionSettings)
for sessionID, settings := range s.sessionSettings {
cloneSettings := s.globalSettings.clone()
cloneSettings.overlay(settings)
allSessionSettings[sessionID] = cloneSettings
}
return allSessionSettings
}
//AddSession adds Session Settings to Settings instance. Returns an error if session settings with duplicate sessionID has already been added
func (s *Settings) AddSession(sessionSettings *SessionSettings) (SessionID, error) {
s.lazyInit()
sessionID := sessionIDFromSessionSettings(s.GlobalSettings(), sessionSettings)
if _, dup := s.sessionSettings[sessionID]; dup {
return sessionID, fmt.Errorf("duplicate session configured for %v", sessionID)
}
s.sessionSettings[sessionID] = sessionSettings
return sessionID, nil
}