forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_settings.go
97 lines (75 loc) · 2.16 KB
/
session_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
package quickfix
import (
"errors"
"fmt"
"strconv"
)
//SessionSettings maps session settings to values with typed accessors.
type SessionSettings struct {
settings map[string]string
}
//Init initializes or resets SessionSettings
func (s *SessionSettings) Init() {
s.settings = make(map[string]string)
}
//NewSessionSettings returns a newly initialized SessionSettings instance
func NewSessionSettings() *SessionSettings {
s := &SessionSettings{}
s.Init()
return s
}
//Set assigns a value to a setting on SessionSettings.
func (s *SessionSettings) Set(setting string, val string) {
//lazy init
if s.settings == nil {
s.Init()
}
s.settings[setting] = val
}
//HasSetting returns true if a setting is set, false if not
func (s *SessionSettings) HasSetting(setting string) bool {
_, ok := s.settings[setting]
return ok
}
//Setting is a settings string accessor. Returns an error if the setting is missing.
func (s *SessionSettings) Setting(setting string) (string, error) {
val, ok := s.settings[setting]
if !ok {
return val, errors.New("setting not found")
}
return val, nil
}
//IntSetting returns the requested setting parsed as an int. Returns an errror if the setting is not set or cannot be parsed as an int.
func (s *SessionSettings) IntSetting(setting string) (int, error) {
stringVal, err := s.Setting(setting)
if err != nil {
return 0, err
}
return strconv.Atoi(stringVal)
}
//BoolSetting returns the requested setting parsed as a boolean. Returns an errror if the setting is not set or cannot be parsed as a bool.
func (s SessionSettings) BoolSetting(setting string) (bool, error) {
stringVal, err := s.Setting(setting)
if err != nil {
return false, err
}
if stringVal == "Y" || stringVal == "y" {
return true, nil
}
if stringVal == "N" || stringVal == "n" {
return false, nil
}
return false, fmt.Errorf("%v cannot be parsed as a bool", stringVal)
}
func (s *SessionSettings) overlay(overlay *SessionSettings) {
for key, val := range overlay.settings {
s.settings[key] = val
}
}
func (s *SessionSettings) clone() *SessionSettings {
sClone := NewSessionSettings()
for k, v := range s.settings {
sClone.settings[k] = v
}
return sClone
}