Skip to content

Commit fbed816

Browse files
committed
[golang] Issue #435 References inside messageHeader not working
Generate the MessageHeader as though it was a composite as it's not compulsorarily standardized in the standard. Provide an optimized separate MessageHeader for use when people follow the recommended MessageHeader as most people will do just that. Fix <ref tags> to use the underlying type find token.acceptableTypeName() rather than token.name(). In golang references had previously been worked around for sets/choices/composites but not MessageHeaders. This makes them all work the same way and more properly but it does introduce some monor changes to generated types. Fix the tests, examples and benchmarks to reflect these changes, adding an issue435.xml for testing with test code to match. Fix the java interop test which used the sbe-samples example-schema which has now been modified. This also causes the bigendian test to be revised. Documentation updates to match.
1 parent e123949 commit fbed816

File tree

19 files changed

+247
-89
lines changed

19 files changed

+247
-89
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ gocode/src/mktdata/*.go
7878
!gocode/src/mktdata/*_test.go
7979
gocode/src/simple/*.go
8080
!gocode/src/simple/*_test.go
81+
gocode/src/issue*/*.go
82+
!gocode/src/issue*/*_test.go
8183
gocode/src/*/*/*.go
8284
!gocode/src/*/*/*_test.go
8385
gocode/src/example-schema/example-schema*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ tests and containes some examples
125125

126126
Users of golang generated code should see the [user
127127
documentation](https://github.com/real-logic/simple-binary-encoding/wiki/Golang-User-Guide). Developers
128-
wishing to enhance golang generator should see the [developer
128+
wishing to enhance the golang generator should see the [developer
129129
documentation](https://github.com/real-logic/simple-binary-encoding/blob/master/gocode/README.md)
130130

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ task(generateGolangCodecsWithXSD, type:JavaExec) {
508508
'gocode/resources/example-extension-2-schema.xml',
509509
'gocode/resources/group-with-data-extension-schema.xml',
510510
'gocode/resources/simple.xml',
511+
'gocode/resources/issue435.xml',
511512
'sbe-samples/src/main/resources/example-schema.xml',
512513
'sbe-samples/src/main/resources/example-extension-schema.xml' ]
513514
}
@@ -566,4 +567,4 @@ task runBenchmarks(type: Exec) {
566567
task uploadToMavenCentral {
567568
dependsOn 'sbe-tool:uploadArchives', 'sbe-samples:uploadArchives', 'sbe-benchmarks:uploadArchives',
568569
'sbe-all:uploadArchives', 'sbe-all:uploadShadow'
569-
}
570+
}

gocode/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ src/example-schema/example-schema: $(DEP)
3838
go fmt && \
3939
./example-schema)
4040

41-
bench: $(DEP) src/example-schema/example-schema.test
41+
bench: $(DEP)
4242
(export GOPATH=$(GOPATH) && \
4343
cd src/example-schema && \
4444
go test --bench . -cpuprofile=cpu.out && \
@@ -68,7 +68,7 @@ test: $(DEP)
6868
go install \
6969
;done))
7070
(export GOPATH=$(GOPATH) && \
71-
(for t in mktdata group_with_data group_with_data_extension composite_element composite simple; do \
71+
(for t in mktdata group_with_data group_with_data_extension composite_elements composite simple issue435; do \
7272
cd $(GOPATH)/src/$$t && \
7373
go build && \
7474
go fmt && \

gocode/README.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
Overview
22
========
33

4-
There is now a [user
5-
guide]https://github.com/real-logic/simple-binary-encoding/wiki/Golang-User-Guide
4+
There is now a
5+
[user guide](https://github.com/real-logic/simple-binary-encoding/wiki/Golang-User-Guide)
66
and this document is for development of the SBE golang generator.
77

88
Code Layout
99
-----------
1010
The Java code that performs the generation of golang code is
11-
[here]https://github.com/real-logic/simple-binary-encoding/tree/master/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang.
11+
[here](https://github.com/real-logic/simple-binary-encoding/tree/master/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang).
1212

13-
Golang code used for testing resides in the top-level [gocode
14-
directory]https://github.com/real-logic/simple-binary-encoding/tree/master/gocode
13+
Golang code used for testing resides in the top-level
14+
[gocode directory](https://github.com/real-logic/simple-binary-encoding/tree/master/gocode).
1515

1616
Building and testing
1717
--------------------
18-
At some point teh golang build will be better integrated into
18+
At some point the golang build will be better integrated into
1919
gradle. For the time being some instructions/procedures are encoded
2020
into a gnu Makefile in the top level gocode directory
2121

@@ -50,23 +50,15 @@ test code for them.
5050
Design choices
5151
--------------
5252
Most of the design choice rationale is contained in the user guide
53-
however, Some design decisions are based around the structure of
54-
sbe-tool itself. sbe-tool parses the XML into an internal
55-
representation (IR) and then passes this to the language specific
56-
generator. As a result some information on the xml structure has been
57-
lost.
53+
however, some design decisions are based around the structure of
54+
sbe-tool itself.
5855

59-
The first example of this include the use of the `<ref>` tag which if
60-
used to the same underlying type in a composite will create two
61-
differently named definitions of the type in the IR.
62-
63-
A second example occurs where a field references an aliased primitive
64-
type definition in which case we lose the aliased type name in the IR
65-
as it is unreferenced.
56+
sbe-tool parses the XML into an internal representation (IR) and then
57+
passes this to the language specific generator. It is this IR which a
58+
generator processes.
6659

6760
Roadmap
6861
=======
6962
* Windows developer support (currently tested on Linux/MacOS)
70-
* Unnecessary code removal (e.g., GroupSizeEncoding)
7163
* Further Unicode support
7264
* Testing/Bug fixes

gocode/resources/example-bigendian.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<type name="VehicleCode" primitiveType="char" length="6" characterEncoding="ASCII"/>
2828
<type name="Ron" primitiveType="uint8" minValue="90" maxValue="110"/>
2929
<type name="someNumbers" primitiveType="uint32" length="5"/>
30+
<type name="Percentage" primitiveType="int8" minValue="0" maxValue="100"/>
3031
<composite name="Booster">
3132
<enum name="BoostType" encodingType="char">
3233
<validValue name="TURBO">T</validValue>
@@ -42,6 +43,8 @@
4243
<type name="maxRpm" primitiveType="uint16" presence="constant">9000</type>
4344
<type name="manufacturerCode" primitiveType="char" length="3"/>
4445
<type name="fuel" primitiveType="char" presence="constant">Petrol</type>
46+
<ref name="efficiency" type="Percentage"/>
47+
<ref name="boosterEnabled" type="BooleanType"/>
4548
<ref name="booster" type="Booster"/>
4649
</composite>
4750
<enum name="BooleanType" encodingType="uint8">

gocode/resources/example-extension-2-schema.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
<type name="VehicleCode" primitiveType="char" length="6" characterEncoding="ASCII"/>
2929
<type name="Ron" primitiveType="uint8" minValue="90" maxValue="110"/>
3030
<type name="someNumbers" primitiveType="uint32" length="5"/>
31-
<composite name="Booster">
31+
<type name="Percentage" primitiveType="int8" minValue="0" maxValue="100"/>
32+
<composite name="Booster">
3233
<enum name="BoostType" encodingType="char">
3334
<validValue name="TURBO">T</validValue>
3435
<validValue name="SUPERCHARGER">S</validValue>
@@ -43,6 +44,8 @@
4344
<type name="maxRpm" primitiveType="uint16" presence="constant">9000</type>
4445
<type name="manufacturerCode" primitiveType="char" length="3"/>
4546
<type name="fuel" primitiveType="char" presence="constant">Petrol</type>
47+
<ref name="efficiency" type="Percentage"/>
48+
<ref name="boosterEnabled" type="BooleanType"/>
4649
<ref name="booster" type="Booster"/>
4750
</composite>
4851
<enum name="BooleanType" encodingType="uint8">

gocode/resources/issue435.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
3+
package="issue435"
4+
id="435"
5+
version="0"
6+
semanticVersion="1.0"
7+
description="issue 435 test case"
8+
byteOrder="bigEndian">
9+
<types>
10+
<set name="setRef" encodingType="uint8">
11+
<choice name="One">0</choice>
12+
<choice name="Two">1</choice>
13+
</set>
14+
<enum name="enumRef" encodingType="uint8">
15+
<validValue name="One">0</validValue>
16+
<validValue name="Two">1</validValue>
17+
</enum>
18+
<composite name="messageHeader" description="Message identifiers and length of message root">
19+
<type name="blockLength" primitiveType="uint16"/>
20+
<type name="templateId" primitiveType="uint16"/>
21+
<type name="schemaId" primitiveType="uint16"/>
22+
<type name="version" primitiveType="uint16"/>
23+
<ref name="s" type="setRef"/>
24+
</composite>
25+
<composite name="exampleRef">
26+
<ref name="e" type="enumRef"/>
27+
</composite>
28+
</types>
29+
<sbe:message name="issue435" id="1" description="issue 435 test">
30+
<field name="example" type="exampleRef" id="10"/>
31+
</sbe:message>
32+
</sbe:messageSchema>

gocode/src/baseline-bigendian/Car_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestDecodeJavaBuffer(t *testing.T) {
117117
// made by editing example-schgema to be bigendian and running
118118
// with the example with -Dsbe.encoding.filename
119119
// and then decoded using od -tu1
120-
data := []byte{0, 47, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 210, 7, 221, 1, 65, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 97, 98, 99, 100, 101, 102, 6, 7, 208, 4, 49, 50, 51, 78, 200, 0, 6, 0, 3, 0, 30, 66, 15, 153, 154, 0, 0, 0, 11, 85, 114, 98, 97, 110, 32, 67, 121, 99, 108, 101, 0, 55, 66, 68, 0, 0, 0, 0, 0, 14, 67, 111, 109, 98, 105, 110, 101, 100, 32, 67, 121, 99, 108, 101, 0, 75, 66, 32, 0, 0, 0, 0, 0, 13, 72, 105, 103, 104, 119, 97, 121, 32, 67, 121, 99, 108, 101, 0, 1, 0, 2, 95, 0, 6, 0, 3, 0, 30, 64, 128, 0, 0, 0, 60, 64, 240, 0, 0, 0, 100, 65, 67, 51, 51, 99, 0, 6, 0, 3, 0, 30, 64, 115, 51, 51, 0, 60, 64, 227, 51, 51, 0, 100, 65, 60, 204, 205, 0, 0, 0, 5, 72, 111, 110, 100, 97, 0, 0, 0, 9, 67, 105, 118, 105, 99, 32, 86, 84, 105, 0, 0, 0, 6, 97, 98, 99, 100, 101, 102}
120+
data := []byte{0, 49, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 210, 7, 221, 1, 65, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 97, 98, 99, 100, 101, 102, 6, 7, 208, 4, 49, 50, 51, 35, 1, 78, 200, 0, 6, 0, 3, 0, 30, 66, 15, 153, 154, 0, 0, 0, 11, 85, 114, 98, 97, 110, 32, 67, 121, 99, 108, 101, 0, 55, 66, 68, 0, 0, 0, 0, 0, 14, 67, 111, 109, 98, 105, 110, 101, 100, 32, 67, 121, 99, 108, 101, 0, 75, 66, 32, 0, 0, 0, 0, 0, 13, 72, 105, 103, 104, 119, 97, 121, 32, 67, 121, 99, 108, 101, 0, 1, 0, 2, 95, 0, 6, 0, 3, 0, 30, 64, 128, 0, 0, 0, 60, 64, 240, 0, 0, 0, 100, 65, 67, 51, 51, 99, 0, 6, 0, 3, 0, 30, 64, 115, 51, 51, 0, 60, 64, 227, 51, 51, 0, 100, 65, 60, 204, 205, 0, 0, 0, 5, 72, 111, 110, 100, 97, 0, 0, 0, 9, 67, 105, 118, 105, 99, 32, 86, 84, 105, 0, 0, 0, 6, 97, 98, 99, 100, 101, 102}
121121

122122
buf := bytes.NewBuffer(data)
123123
m := NewSbeGoMarshaller()

gocode/src/baseline/Car_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestEncodeDecodeCar(t *testing.T) {
2020
optionalExtras[OptionalExtrasChoice.SportsPack] = true
2121

2222
var engine Engine
23-
engine = Engine{2000, 4, 0, manufacturerCode, [6]byte{}, EngineBooster{BoostType.NITROUS, 200}}
23+
engine = Engine{2000, 4, 0, manufacturerCode, [6]byte{}, 42, BooleanType.T, EngineBooster{BoostType.NITROUS, 200}}
2424

2525
manufacturer := []uint8("Honda")
2626
model := []uint8("Civic VTi")
@@ -115,12 +115,12 @@ func TestDecodeJavaBuffer(t *testing.T) {
115115

116116
// The byte array is from the java example for interop test made by
117117
// running with -Dsbe.encoding.filename and then decoded using od -tu1
118-
data := []byte{47, 0, 1, 0, 1, 0, 0, 0, 210, 4, 0, 0, 0, 0, 0, 0, 221, 7, 1, 65, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 97, 98, 99, 100, 101, 102, 6, 208, 7, 4, 49, 50, 51, 78, 200, 6, 0, 3, 0, 30, 0, 154, 153, 15, 66, 11, 0, 0, 0, 85, 114, 98, 97, 110, 32, 67, 121, 99, 108, 101, 55, 0, 0, 0, 68, 66, 14, 0, 0, 0, 67, 111, 109, 98, 105, 110, 101, 100, 32, 67, 121, 99, 108, 101, 75, 0, 0, 0, 32, 66, 13, 0, 0, 0, 72, 105, 103, 104, 119, 97, 121, 32, 67, 121, 99, 108, 101, 1, 0, 2, 0, 95, 6, 0, 3, 0, 30, 0, 0, 0, 128, 64, 60, 0, 0, 0, 240, 64, 100, 0, 51, 51, 67, 65, 99, 6, 0, 3, 0, 30, 0, 51, 51, 115, 64, 60, 0, 51, 51, 227, 64, 100, 0, 205, 204, 60, 65, 5, 0, 0, 0, 72, 111, 110, 100, 97, 9, 0, 0, 0, 67, 105, 118, 105, 99, 32, 86, 84, 105, 6, 0, 0, 0, 97, 98, 99, 100, 101, 102, 0, 0, 0, 0, 0, 0, 0, 0}
118+
data := []byte{49, 0, 1, 0, 1, 0, 0, 0, 210, 4, 0, 0, 0, 0, 0, 0, 221, 7, 1, 65, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 97, 98, 99, 100, 101, 102, 6, 208, 7, 4, 49, 50, 51, 35, 1, 78, 200, 6, 0, 3, 0, 30, 0, 154, 153, 15, 66, 11, 0, 0, 0, 85, 114, 98, 97, 110, 32, 67, 121, 99, 108, 101, 55, 0, 0, 0, 68, 66, 14, 0, 0, 0, 67, 111, 109, 98, 105, 110, 101, 100, 32, 67, 121, 99, 108, 101, 75, 0, 0, 0, 32, 66, 13, 0, 0, 0, 72, 105, 103, 104, 119, 97, 121, 32, 67, 121, 99, 108, 101, 1, 0, 2, 0, 95, 6, 0, 3, 0, 30, 0, 0, 0, 128, 64, 60, 0, 0, 0, 240, 64, 100, 0, 51, 51, 67, 65, 99, 6, 0, 3, 0, 30, 0, 51, 51, 115, 64, 60, 0, 51, 51, 227, 64, 100, 0, 205, 204, 60, 65, 5, 0, 0, 0, 72, 111, 110, 100, 97, 9, 0, 0, 0, 67, 105, 118, 105, 99, 32, 86, 84, 105, 6, 0, 0, 0, 97, 98, 99, 100, 101, 102}
119119

120120
buf := bytes.NewBuffer(data)
121121
m := NewSbeGoMarshaller()
122122

123-
var hdr MessageHeader
123+
var hdr SbeGoMessageHeader
124124
if err := hdr.Decode(m, buf); err != nil {
125125
t.Logf("Failed to decode message header", err)
126126
t.Fail()

0 commit comments

Comments
 (0)