-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
187 lines (160 loc) · 4.56 KB
/
builder.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
package openfigi
import (
"fmt"
"math"
"time"
"golang.org/x/exp/constraints"
)
// ========================= BASE ITEM =========================
type BaseItemBuilder struct {
item BaseItem
}
func (b *BaseItemBuilder) SetExchCode(exchCode string) *BaseItemBuilder {
b.item.ExchCode = exchCode
return b
}
func (b *BaseItemBuilder) SetMicCode(micCode string) *BaseItemBuilder {
b.item.MicCode = micCode
return b
}
func (b *BaseItemBuilder) SetCurrency(currency string) *BaseItemBuilder {
b.item.Currency = currency
return b
}
func (b *BaseItemBuilder) SetMarketSecDes(marketSecDes string) *BaseItemBuilder {
b.item.MarketSecDes = marketSecDes
return b
}
func (b *BaseItemBuilder) SetSecurityType(securityType string) *BaseItemBuilder {
b.item.SecurityType = securityType
return b
}
func (b *BaseItemBuilder) SetSecurityType2(securityType2 string) *BaseItemBuilder {
b.item.SecurityType2 = securityType2
return b
}
func (b *BaseItemBuilder) SetIncludeUnlistedEquities(include bool) *BaseItemBuilder {
b.item.IncludeUnlistedEquities = include
return b
}
func (b *BaseItemBuilder) SetOptionType(optionType string) *BaseItemBuilder {
b.item.OptionType = optionType
return b
}
// Usage:
//
// builder.SetStrike([2]any{nil, 2.0})
func (b *BaseItemBuilder) SetStrike(strike [2]any) *BaseItemBuilder {
strikeRange := intepretRange[float64](strike)
b.item.Strike = &strikeRange
return b
}
// Usage:
//
// builder.SetContractSize([2]any{2.0, nil})
func (b *BaseItemBuilder) SetContractSize(contractSize [2]any) *BaseItemBuilder {
contractSizeRange := intepretRange[float64](contractSize)
b.item.ContractSize = &contractSizeRange
return b
}
// Usage:
//
// builder.SetCoupon([2]any{nil, 2.0})
func (b *BaseItemBuilder) SetCoupon(coupon [2]any) *BaseItemBuilder {
couponRange := intepretRange[float64](coupon)
b.item.Coupon = &couponRange
return b
}
// Usage:
//
// builder.SetExpiration([2]any{"2021-01-01", "2022-01-01"})
func (b *BaseItemBuilder) SetExpiration(expiration [2]any) *BaseItemBuilder {
expirationRange := intepretRange[string](expiration)
b.item.Expiration = &expirationRange
return b
}
// Usage:
//
// builder.SetMaturity([2]any{nil, "2022-01-01"})
func (b *BaseItemBuilder) SetMaturity(maturity [2]any) *BaseItemBuilder {
maturityRange := intepretRange[string](maturity)
b.item.Maturity = &maturityRange
return b
}
func (b *BaseItemBuilder) SetStateCode(stateCode string) *BaseItemBuilder {
b.item.StateCode = stateCode
return b
}
func (b *BaseItemBuilder) Build() (item BaseItem, err error) {
item = b.item
err = item.validate()
return
}
// ========================= MAPPING ITEM =========================
type MappingItemBuilder struct {
BaseItemBuilder
item MappingItem
}
func (m *MappingItemBuilder) Build() (item MappingItem, err error) {
m.item.BaseItem = m.BaseItemBuilder.item
item = m.item
err = m.item.validate()
return
}
// ========================= AUXILIARY FUNC =========================
// Make sure the range is of the right type. Will panic if not.
// If float, nil will be replaced with -Inf or Inf.
// If string, nil will be replaced with "".
func intepretRange[T constraints.Ordered](interval [2]interface{}) interval[T] {
var zero T
switch any(zero).(type) {
case float64:
if interval[0] == nil {
interval[0] = math.Inf(-1)
}
if interval[1] == nil {
interval[1] = math.Inf(1)
}
case string:
if interval[0] == nil {
interval[0] = ""
}
if interval[1] == nil {
interval[1] = ""
}
}
return [2]T{interval[0].(T), interval[1].(T)}
}
// Validate the interval. The bound must be in the right order, no both nils.
func (interval interval[T]) validate() error {
var zero T
switch any(zero).(type) {
case float64:
start, _ := any(interval[0]).(float64)
end, _ := any(interval[1]).(float64)
if math.IsInf(start, -1) && math.IsInf(end, 1) {
return fmt.Errorf("interval cannot be [null, null]")
} else {
if start > end {
return fmt.Errorf("bad interval: %v > %v", start, end)
}
}
case string:
start, _ := any(interval[0]).(string)
end, _ := any(interval[1]).(string)
if start == "" && end == "" {
return fmt.Errorf("interval cannot be [null, null]")
} else {
if s, err := time.Parse(time.DateOnly, start); start != "" && err != nil {
return fmt.Errorf("bad date format: %v", err)
} else if e, err := time.Parse(time.DateOnly, end); end != "" && err != nil {
return fmt.Errorf("bad date format: %v", err)
} else if start != "" && end != "" && s.After(e) {
return fmt.Errorf("bad interval: %v > %v", s, e)
}
}
default:
return fmt.Errorf("unsupported type: %T", zero)
}
return nil
}