Skip to content

Commit 8e1b708

Browse files
committed
Misc field type refactoring (quickfixgo#145)
* remove archaic field ctors, add helper converts, embed Time in UTC Timestamp field * generated fields
1 parent 0e0fb7c commit 8e1b708

18 files changed

+130
-157
lines changed

field/fields.go

+58-58
Large diffs are not rendered by default.

fix_boolean.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import (
77
//FIXBoolean is a FIX Boolean value, implements FieldValue.
88
type FIXBoolean bool
99

10-
//NewFIXBoolean returns an initialized FIXBoolean
11-
func NewFIXBoolean(val bool) *FIXBoolean {
12-
b := FIXBoolean(val)
13-
return &b
14-
}
10+
//Bool converts the FIXBoolean value to bool
11+
func (f FIXBoolean) Bool() bool { return bool(f) }
1512

1613
func (f *FIXBoolean) Read(bytes []byte) error {
1714
switch string(bytes) {

fix_boolean_test.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package quickfix
22

33
import (
44
"bytes"
5+
"fmt"
56
"testing"
6-
)
77

8-
func TestBooleanFieldWrite(t *testing.T) {
8+
"github.com/stretchr/testify/assert"
9+
)
910

11+
func TestBooleanWrite(t *testing.T) {
1012
var tests = []struct {
1113
val FIXBoolean
1214
expected []byte
@@ -17,14 +19,11 @@ func TestBooleanFieldWrite(t *testing.T) {
1719

1820
for _, test := range tests {
1921
b := test.val.Write()
20-
21-
if !bytes.Equal(b, test.expected) {
22-
t.Errorf("got %v; want %v", b, test.expected)
23-
}
22+
assert.True(t, bytes.Equal(b, test.expected), fmt.Sprintf("got %v; want %v", b, test.expected))
2423
}
2524
}
2625

27-
func TestFIXBooleanFieldRead(t *testing.T) {
26+
func TestFIXBooleanRead(t *testing.T) {
2827
var tests = []struct {
2928
bytes []byte
3029
expected bool
@@ -39,14 +38,7 @@ func TestFIXBooleanFieldRead(t *testing.T) {
3938
var val FIXBoolean
4039
err := val.Read(test.bytes)
4140

42-
if test.expectError && err == nil {
43-
t.Errorf("Expected error for %v", test.bytes)
44-
} else if !test.expectError && err != nil {
45-
t.Errorf("UnExpected '%v'", err)
46-
}
47-
48-
if val != FIXBoolean(test.expected) {
49-
t.Errorf("got %v want %v", val, test.expected)
50-
}
41+
assert.Equal(t, test.expectError, err != nil)
42+
assert.Equal(t, test.expected, val.Bool())
5143
}
5244
}

fix_float.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import (
88
//FIXFloat is a FIX Float Value, implements FieldValue
99
type FIXFloat float64
1010

11-
//NewFIXFloat returns an initialized FIXFloat
12-
func NewFIXFloat(val float64) *FIXFloat {
13-
f := FIXFloat(val)
14-
return &f
15-
}
11+
//Float64 converts the FIXFloat value to float64
12+
func (f FIXFloat) Float64() float64 { return float64(f) }
1613

1714
func (f *FIXFloat) Read(bytes []byte) error {
1815
val, err := strconv.ParseFloat(string(bytes), 64)

fix_float_test.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package quickfix
22

33
import (
44
"bytes"
5+
"fmt"
56
"testing"
7+
8+
"github.com/stretchr/testify/assert"
69
)
710

811
func TestFloatWrite(t *testing.T) {
@@ -16,9 +19,7 @@ func TestFloatWrite(t *testing.T) {
1619
for _, test := range tests {
1720
b := test.field.Write()
1821

19-
if !bytes.Equal(b, test.val) {
20-
t.Errorf("got %v; want %v", b, test.val)
21-
}
22+
assert.True(t, bytes.Equal(b, test.val), fmt.Sprintf("got %v; want %v", b, test.val))
2223
}
2324
}
2425

@@ -40,15 +41,9 @@ func TestFloatRead(t *testing.T) {
4041

4142
for _, test := range tests {
4243
var field FIXFloat
43-
if err := field.Read(test.bytes); err != nil {
44-
if !test.expectError {
45-
t.Errorf("UnExpected '%v'", err)
46-
}
47-
} else if test.expectError {
48-
t.Errorf("Expected error for %v", test.bytes)
49-
} else if float64(field) != test.value {
50-
t.Errorf("got %v want %v", field, test.value)
51-
}
44+
err := field.Read(test.bytes)
45+
assert.Equal(t, test.expectError, err != nil)
46+
assert.Equal(t, test.value, field.Float64())
5247
}
5348
}
5449

fix_int.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ func parseUInt(d []byte) (n int, err error) {
4646
//FIXInt is a FIX Int Value, implements FieldValue
4747
type FIXInt int
4848

49-
//NewFIXInt returns an initialized FIXInt
50-
func NewFIXInt(val int) *FIXInt {
51-
i := FIXInt(val)
52-
return &i
53-
}
49+
//Int converts the FIXInt value to int
50+
func (f FIXInt) Int() int { return int(f) }
5451

5552
func (f *FIXInt) Read(bytes []byte) error {
5653
i, err := atoi(bytes)

fix_int_test.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@ package quickfix
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

7-
func TestInt_Write(t *testing.T) {
9+
func TestFIXInt_Write(t *testing.T) {
810
field := FIXInt(5)
9-
bytes := field.Write()
1011

11-
if string(bytes) != "5" {
12-
t.Error("Unexpected bytes ", bytes)
13-
}
12+
assert.Equal(t, "5", string(field.Write()))
1413
}
1514

16-
func TestInt_Read(t *testing.T) {
15+
func TestFIXInt_Read(t *testing.T) {
1716
var field FIXInt
1817
err := field.Read([]byte("15"))
19-
20-
if err != nil {
21-
t.Error("Unexpected error", err)
22-
}
23-
24-
if int(field) != 15 {
25-
t.Error("unexpected value", field)
26-
}
18+
assert.Nil(t, err, "Unexpected error")
19+
assert.Equal(t, 15, int(field))
2720

2821
err = field.Read([]byte("blah"))
22+
assert.NotNil(t, err, "Unexpected error")
23+
}
2924

30-
if err == nil {
31-
t.Error("expected error")
32-
}
25+
func TestFIXInt_Int(t *testing.T) {
26+
f := FIXInt(4)
27+
assert.Equal(t, 4, f.Int())
3328
}
3429

35-
func BenchmarkInt_Read(b *testing.B) {
30+
func BenchmarkFIXInt_Read(b *testing.B) {
3631
intBytes := []byte("1500")
3732
var field FIXInt
3833

fix_string.go

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ package quickfix
33
//FIXString is a FIX String Value, implements FieldValue
44
type FIXString string
55

6-
//NewFIXString returns an initialized FIXString
7-
func NewFIXString(val string) *FIXString {
8-
s := FIXString(val)
9-
return &s
10-
}
11-
126
func (f FIXString) String() string {
137
return string(f)
148
}

fix_utc_timestamp.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
//FIXUTCTimestamp is a FIX UTC Timestamp value, implements FieldValue
88
type FIXUTCTimestamp struct {
9-
Value time.Time
9+
time.Time
1010
NoMillis bool
1111
}
1212

@@ -18,12 +18,12 @@ const (
1818
func (f *FIXUTCTimestamp) Read(bytes []byte) error {
1919
var err error
2020
//with millisecs
21-
if f.Value, err = time.Parse(utcTimestampFormat, string(bytes)); err == nil {
21+
if f.Time, err = time.Parse(utcTimestampFormat, string(bytes)); err == nil {
2222
return nil
2323
}
2424

2525
//w/o millisecs
26-
if f.Value, err = time.Parse(utcTimestampNoMillisFormat, string(bytes)); err != nil {
26+
if f.Time, err = time.Parse(utcTimestampNoMillisFormat, string(bytes)); err != nil {
2727
return err
2828
}
2929

@@ -33,8 +33,8 @@ func (f *FIXUTCTimestamp) Read(bytes []byte) error {
3333

3434
func (f FIXUTCTimestamp) Write() []byte {
3535
if f.NoMillis {
36-
return []byte(f.Value.UTC().Format(utcTimestampNoMillisFormat))
36+
return []byte(f.UTC().Format(utcTimestampNoMillisFormat))
3737
}
3838

39-
return []byte(f.Value.UTC().Format(utcTimestampFormat))
39+
return []byte(f.UTC().Format(utcTimestampFormat))
4040
}

fix_utc_timestamp_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package quickfix_test
22

33
import (
4-
"github.com/quickfixgo/quickfix"
54
"testing"
65
"time"
6+
7+
"github.com/quickfixgo/quickfix"
78
)
89

910
func TestFIXUTCTimestampRead(t *testing.T) {
@@ -22,8 +23,8 @@ func TestFIXUTCTimestampRead(t *testing.T) {
2223
t.Errorf("Unexpected error: %v", err)
2324
}
2425

25-
if !f.Value.Equal(test.expectedTime) {
26-
t.Errorf("For Time expected %v got %v", test.expectedTime, f.Value)
26+
if !f.Time.Equal(test.expectedTime) {
27+
t.Errorf("For Time expected %v got %v", test.expectedTime, f.Time)
2728
}
2829

2930
if f.NoMillis != test.expectedNoMillis {

gen/generate-fields/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ func (f {{ .Name }}Field) Tag() quickfix.Tag { return tag.{{ .Name }} }
206206
{{ if eq $base_type "FIXUTCTimestamp" }}
207207
//New{{ .Name }} returns a new {{ .Name }}Field initialized with val
208208
func New{{ .Name }}(val time.Time) {{ .Name }}Field {
209-
return {{ .Name }}Field{ quickfix.FIXUTCTimestamp{ Value: val } }
209+
return {{ .Name }}Field{ quickfix.FIXUTCTimestamp{ Time: val } }
210210
}
211211
212212
//New{{ .Name }}NoMillis returns a new {{ .Name }}Field initialized with val without millisecs
213213
func New{{ .Name }}NoMillis(val time.Time) {{ .Name }}Field {
214-
return {{ .Name }}Field{ quickfix.FIXUTCTimestamp{ Value: val, NoMillis: true } }
214+
return {{ .Name }}Field{ quickfix.FIXUTCTimestamp{ Time: val, NoMillis: true } }
215215
}
216216
217217
{{ else }}

in_session.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (state inSession) doTargetTooLow(session *session, msg Message, rej targetT
230230
sendingTime := new(FIXUTCTimestamp)
231231
msg.Header.GetField(tagSendingTime, sendingTime)
232232

233-
if sendingTime.Value.Before(origSendingTime.Value) {
233+
if sendingTime.Before(origSendingTime.Time) {
234234
session.doReject(msg, sendingTimeAccuracyProblem())
235235
return state.initiateLogout(session, "")
236236
}

marshal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (e encoder) encodeValue(fixTag Tag, v reflect.Value, omitEmpty bool, defaul
4444
case reflect.Struct:
4545
switch t := v.Interface().(type) {
4646
case time.Time:
47-
e.FieldMap.SetField(fixTag, FIXUTCTimestamp{Value: t})
47+
e.FieldMap.SetField(fixTag, FIXUTCTimestamp{Time: t})
4848
}
4949
case reflect.String:
5050
e.FieldMap.SetField(fixTag, FIXString(v.String()))

session.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ func (s *session) insertSendingTime(header Header) {
157157
sendingTime := time.Now().UTC()
158158

159159
if s.sessionID.BeginString >= enum.BeginStringFIX42 {
160-
header.SetField(tagSendingTime, FIXUTCTimestamp{Value: sendingTime})
160+
header.SetField(tagSendingTime, FIXUTCTimestamp{Time: sendingTime})
161161
} else {
162-
header.SetField(tagSendingTime, FIXUTCTimestamp{Value: sendingTime, NoMillis: true})
162+
header.SetField(tagSendingTime, FIXUTCTimestamp{Time: sendingTime, NoMillis: true})
163163
}
164164
}
165165

@@ -434,7 +434,7 @@ func (s *session) checkSendingTime(msg Message) MessageRejectError {
434434
return err
435435
}
436436

437-
if delta := time.Since(sendingTime.Value); delta <= -1*time.Duration(120)*time.Second || delta >= time.Duration(120)*time.Second {
437+
if delta := time.Since(sendingTime.Time); delta <= -1*time.Duration(120)*time.Second || delta >= time.Duration(120)*time.Second {
438438
return sendingTimeAccuracyProblem()
439439
}
440440

session_test.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ func buildMessage() Message {
1414
return builder
1515
}
1616

17+
func newFIXString(val string) *FIXString {
18+
s := FIXString(val)
19+
return &s
20+
}
21+
1722
func TestSession_CheckCorrectCompID(t *testing.T) {
1823
session := session{}
1924
session.sessionID.TargetCompID = "TAR"
@@ -26,19 +31,19 @@ func TestSession_CheckCorrectCompID(t *testing.T) {
2631
rejectReason int
2732
}{
2833
{returnsError: true, rejectReason: rejectReasonRequiredTagMissing},
29-
{senderCompID: NewFIXString("TAR"),
34+
{senderCompID: newFIXString("TAR"),
3035
returnsError: true,
3136
rejectReason: rejectReasonRequiredTagMissing},
32-
{senderCompID: NewFIXString("TAR"),
33-
targetCompID: NewFIXString("JCD"),
37+
{senderCompID: newFIXString("TAR"),
38+
targetCompID: newFIXString("JCD"),
3439
returnsError: true,
3540
rejectReason: rejectReasonCompIDProblem},
36-
{senderCompID: NewFIXString("JCD"),
37-
targetCompID: NewFIXString("SND"),
41+
{senderCompID: newFIXString("JCD"),
42+
targetCompID: newFIXString("SND"),
3843
returnsError: true,
3944
rejectReason: rejectReasonCompIDProblem},
40-
{senderCompID: NewFIXString("TAR"),
41-
targetCompID: NewFIXString("SND"),
45+
{senderCompID: newFIXString("TAR"),
46+
targetCompID: newFIXString("SND"),
4247
returnsError: false},
4348
}
4449

@@ -163,7 +168,7 @@ func TestSession_CheckSendingTime(t *testing.T) {
163168

164169
//sending time too late
165170
sendingTime := time.Now().Add(time.Duration(-200) * time.Second)
166-
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Value: sendingTime})
171+
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Time: sendingTime})
167172
msgBytes, _ = builder.Build()
168173
msg, _ = ParseMessage(msgBytes)
169174

@@ -177,7 +182,7 @@ func TestSession_CheckSendingTime(t *testing.T) {
177182

178183
//future sending time
179184
sendingTime = time.Now().Add(time.Duration(200) * time.Second)
180-
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Value: sendingTime})
185+
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Time: sendingTime})
181186
msgBytes, _ = builder.Build()
182187
msg, _ = ParseMessage(msgBytes)
183188

@@ -191,7 +196,7 @@ func TestSession_CheckSendingTime(t *testing.T) {
191196

192197
//sending time ok
193198
sendingTime = time.Now()
194-
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Value: sendingTime})
199+
builder.Header.SetField(tagSendingTime, FIXUTCTimestamp{Time: sendingTime})
195200
msgBytes, _ = builder.Build()
196201
msg, _ = ParseMessage(msgBytes)
197202

unmarshal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (d decoder) decodeValue(fixTag Tag, t reflect.Type, v reflect.Value) Messag
2121
if err := d.FieldMap.GetField(Tag(fixTag), fieldValue); err != nil {
2222
return err
2323
}
24-
v.Set(reflect.ValueOf(fieldValue.Value))
24+
v.Set(reflect.ValueOf(fieldValue.Time))
2525
}
2626
case reflect.String:
2727
var fieldValue FIXString

0 commit comments

Comments
 (0)