Skip to content

Commit da547cc

Browse files
authored
[Go] SBE implementation in Go using flyweights (#951)
* [Go] SBE implementation in Go using flyweights #765 To generate flyweights instead of structs, set the following flag: ``` sbe.go.generate.generate.flyweights=true ``` * [Go] Checkstyle fix * [Go] Fix null string return value * [Go] Remove spurious type definition for choices * [Go] Fix formatting in WrapForDecode * [Go] Add returning an array by []byte * [Go] Fix array not present in version * [Go] Add ComputeLength methods * [Go] Fix some compiler warnings and remove unused code * [Go] Draft of OTF decoder * [Go] Add OTF Json Printer * [Go] Fix SbeBlockAndHeaderLength and add constants for composite sizes * [Go] Add IrDecoder for runtime types * [Go] Add enum get method * [Go] Fixes for updating component counts in irdecoder * [Go] Run goimports * [Go] Update with latest changes * [Go] Change the file structure for GOPATH * [Go] Fix copywrite year * [Go] Fix tests for otf This generates flyweights in the sbe-tool build for use in the otf library. * [Go] Add helpers for otf PrimitiveValue
1 parent e34fc03 commit da547cc

File tree

87 files changed

+8428
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+8428
-65
lines changed

.gitignore

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,18 @@ cppbuild/Win32
6565

6666
# golang
6767
gocode/pkg
68-
gocode/src/baseline/*.go
69-
!gocode/src/baseline/*_test.go
70-
gocode/src/baseline-bigendian/*.go
71-
!gocode/src/baseline-bigendian/*_test.go
72-
gocode/src/composite/*.go
73-
!gocode/src/composite/*_test.go
74-
gocode/src/composite_elements/*.go
75-
!gocode/src/composite_elements/*_test.go
76-
gocode/src/since-deprecated/*.go
77-
!gocode/src/since-deprecated/*_test.go
78-
gocode/src/group_with_data*/*.go
79-
!gocode/src/group_with_data*/*_test.go
80-
gocode/src/mktdata/*.go
81-
!gocode/src/mktdata/*_test.go
82-
gocode/src/simple/*.go
83-
!gocode/src/simple/*_test.go
84-
gocode/src/issue*/*.go
85-
!gocode/src/issue*/*_test.go
86-
gocode/src/*/*/*.go
87-
!gocode/src/*/*/*_test.go
88-
gocode/src/example-schema/example-schema*
89-
gocode/src/example-schema/cpu.out
90-
gocode/src/example-socket-clientserver/example-socket-clientserver
91-
gocode/src/extension
92-
gocode/src/extension2
68+
gocode/*/pkg
69+
gocode/struct/src/*/*.go
70+
!gocode/struct/src/*/*_test.go
71+
gocode/struct/src/*/*/*.go
72+
!gocode/struct/src/*/*/*_test.go
73+
gocode/struct/src/example-schema/example-schema*
74+
gocode/struct/src/example-socket-clientserver/example-socket-clientserver
75+
gocode/flyweight/src/*/*.go
76+
!gocode/flyweight/src/*/*_test.go
77+
gocode/flyweight/src/*/*/*.go
78+
!gocode/flyweight/src/*/*/*_test.go
79+
gocode/flyweight/src/otf/code-generation-schema.sbeir
9380

9481
# csharp
9582
csharp/*/bin

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ For convenience on Linux, a gnu Makefile is provided that runs some tests and co
9797
$ cd gocode
9898
# make # test, examples, bench
9999

100+
Go supports both generating Go structs with encode / decode methods, and flyweights like the other languages. Structs are generated by default for compatibility. Set `sbe.go.generate.generate.flyweights=true` to generate flyweights.
101+
100102
Users of golang generated code should see the [user
101103
documentation](https://github.com/real-logic/simple-binary-encoding/wiki/Golang-User-Guide).
102104

build.gradle

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ tasks.register('generateGolangCodecTestComposite', JavaExec) {
700700
mainClass.set('uk.co.real_logic.sbe.SbeTool')
701701
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
702702
systemProperties(
703-
'sbe.output.dir': 'gocode/src',
703+
'sbe.output.dir': 'gocode/struct/src',
704704
'sbe.target.language': 'golang')
705705
args = ['sbe-tool/src/test/resources/composite-elements-schema-rc4.xml']
706706
}
@@ -709,7 +709,7 @@ tasks.register('generateGolangCodecTestBasic', JavaExec) {
709709
mainClass.set('uk.co.real_logic.sbe.SbeTool')
710710
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
711711
systemProperties(
712-
'sbe.output.dir': 'gocode/src/basic',
712+
'sbe.output.dir': 'gocode/struct/src/basic',
713713
'sbe.target.language': 'golang')
714714
args = ['sbe-tool/src/test/resources/basic-types-schema.xml']
715715
}
@@ -718,7 +718,7 @@ tasks.register('generateGolangCodecTestGroup', JavaExec) {
718718
mainClass.set('uk.co.real_logic.sbe.SbeTool')
719719
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
720720
systemProperties(
721-
'sbe.output.dir': 'gocode/src/group',
721+
'sbe.output.dir': 'gocode/struct/src/group',
722722
'sbe.target.language': 'golang')
723723
args = ['sbe-tool/src/test/resources/basic-group-schema.xml']
724724
}
@@ -727,7 +727,7 @@ tasks.register('generateGolangCodecTestVarData', JavaExec) {
727727
mainClass.set('uk.co.real_logic.sbe.SbeTool')
728728
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
729729
systemProperties(
730-
'sbe.output.dir': 'gocode/src/vardata',
730+
'sbe.output.dir': 'gocode/struct/src/vardata',
731731
'sbe.target.language': 'golang')
732732
args = ['sbe-tool/src/test/resources/basic-variable-length-schema.xml']
733733
}
@@ -736,7 +736,7 @@ tasks.register('generateGolangCodecsWithXIncludes', JavaExec) {
736736
mainClass.set('uk.co.real_logic.sbe.SbeTool')
737737
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
738738
systemProperties(
739-
'sbe.output.dir': 'gocode/src',
739+
'sbe.output.dir': 'gocode/struct/src',
740740
'sbe.target.language': 'golang',
741741
'sbe.xinclude.aware': 'true',
742742
'sbe.validation.xsd': validationXsdPath)
@@ -748,7 +748,87 @@ tasks.register('generateGolangCodecsWithXSD', JavaExec) {
748748
mainClass.set('uk.co.real_logic.sbe.SbeTool')
749749
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
750750
systemProperties(
751-
'sbe.output.dir': 'gocode/src',
751+
'sbe.output.dir': 'gocode/struct/src',
752+
'sbe.target.language': 'golang',
753+
'sbe.xinclude.aware': 'true',
754+
'sbe.validation.xsd': validationXsdPath)
755+
args = ['sbe-tool/src/test/resources/group-with-data-schema.xml',
756+
'sbe-tool/src/test/resources/FixBinary.xml',
757+
'sbe-tool/src/test/resources/issue435.xml',
758+
'sbe-tool/src/test/resources/issue472.xml',
759+
'sbe-tool/src/test/resources/issue483.xml',
760+
'sbe-tool/src/test/resources/issue488.xml',
761+
'sbe-tool/src/test/resources/issue560.xml',
762+
'sbe-tool/src/test/resources/issue661.xml',
763+
'sbe-tool/src/test/resources/issue847.xml',
764+
'sbe-tool/src/test/resources/issue848.xml',
765+
'sbe-tool/src/test/resources/issue849.xml',
766+
'sbe-tool/src/test/resources/since-deprecated-test-schema.xml',
767+
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
768+
'gocode/resources/example-composite.xml',
769+
'gocode/resources/group-with-data-extension-schema.xml',
770+
'gocode/resources/simple.xml']
771+
}
772+
773+
tasks.register('generateGolangFlyweightCodecTestComposite', JavaExec) {
774+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
775+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
776+
systemProperties(
777+
'sbe.output.dir': 'gocode/flyweight/src',
778+
'sbe.go.generate.generate.flyweights': 'true',
779+
'sbe.target.language': 'golang')
780+
args = ['sbe-tool/src/test/resources/composite-elements-schema-rc4.xml']
781+
}
782+
783+
tasks.register('generateGolangFlyweightCodecTestBasic', JavaExec) {
784+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
785+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
786+
systemProperties(
787+
'sbe.output.dir': 'gocode/flyweight/src/basic',
788+
'sbe.go.generate.generate.flyweights': 'true',
789+
'sbe.target.language': 'golang')
790+
args = ['sbe-tool/src/test/resources/basic-types-schema.xml']
791+
}
792+
793+
tasks.register('generateGolangFlyweightCodecTestGroup', JavaExec) {
794+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
795+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
796+
systemProperties(
797+
'sbe.output.dir': 'gocode/flyweight/src/group',
798+
'sbe.go.generate.generate.flyweights': 'true',
799+
'sbe.target.language': 'golang')
800+
args = ['sbe-tool/src/test/resources/basic-group-schema.xml']
801+
}
802+
803+
tasks.register('generateGolangFlyweightCodecTestVarData', JavaExec) {
804+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
805+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
806+
systemProperties(
807+
'sbe.output.dir': 'gocode/flyweight/src/vardata',
808+
'sbe.go.generate.generate.flyweights': 'true',
809+
'sbe.target.language': 'golang')
810+
args = ['sbe-tool/src/test/resources/basic-variable-length-schema.xml']
811+
}
812+
813+
tasks.register('generateGolangFlyweightCodecsWithXIncludes', JavaExec) {
814+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
815+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
816+
systemProperties(
817+
'sbe.output.dir': 'gocode/flyweight/src',
818+
'sbe.go.generate.generate.flyweights': 'true',
819+
'sbe.target.language': 'golang',
820+
'sbe.xinclude.aware': 'true',
821+
'sbe.validation.xsd': validationXsdPath)
822+
args = ['sbe-samples/src/main/resources/example-schema.xml',
823+
'sbe-samples/src/main/resources/example-extension-schema.xml']
824+
}
825+
826+
tasks.register('generateGolangFlyweightCodecsWithXSD', JavaExec) {
827+
mainClass.set('uk.co.real_logic.sbe.SbeTool')
828+
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
829+
systemProperties(
830+
'sbe.output.dir': 'gocode/flyweight/src',
831+
'sbe.go.generate.generate.flyweights': 'true',
752832
'sbe.target.language': 'golang',
753833
'sbe.xinclude.aware': 'true',
754834
'sbe.validation.xsd': validationXsdPath)
@@ -780,6 +860,13 @@ tasks.register('generateGolangCodecs') {
780860
'generateGolangCodecTestComposite',
781861
'generateGolangCodecsWithXIncludes',
782862
'generateGolangCodecsWithXSD',
863+
'generateGolangFlyweightCodecTestVarData',
864+
'generateGolangFlyweightCodecTestGroup',
865+
'generateGolangFlyweightCodecTestBasic',
866+
'generateGolangFlyweightCodecTestComposite',
867+
'generateGolangFlyweightCodecsWithXIncludes',
868+
'generateGolangFlyweightCodecsWithXSD',
869+
'generateIrCodecs',
783870
':sbe-all:jar'
784871
}
785872

@@ -868,6 +955,7 @@ tasks.register('generateGolangIrCodecs', JavaExec) {
868955
systemProperties(
869956
'sbe.output.dir': 'sbe-tool/src/main/golang',
870957
'sbe.target.language': 'golang',
958+
'sbe.go.generate.generate.flyweights': true,
871959
'sbe.validation.xsd': validationXsdPath)
872960
args = ['sbe-tool/src/main/resources/sbe-ir.xml']
873961
}

gocode/Makefile

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,99 @@ all: example test # bench
1919
# This target is used to build golang files using parent gradle
2020
# script's invocation of sbe-tool in case it needs to be run again. We
2121
# use this one output file to check that dependency as it's simple
22-
DEP=src/simple/SbeMarshalling.go
22+
DEP=struct/src/simple/SbeMarshalling.go
2323
$(DEP): $(SBE_JAR)
2424
(cd ..; ./gradlew generateGolangCodecs)
25-
(export GOPATH=$(GOPATH) && \
26-
cd src/simple && \
25+
(export GOPATH=$(GOPATH)/struct && \
26+
cd struct/src/simple && \
2727
go build && \
2828
$(SAVE_FORMAT) \
2929
go fmt && \
3030
go test)
3131

3232
# Will regenerate codecs by removing dependencies
3333
clean:
34-
rm -f src/*/SbeMarshalling.go src/*/*/SbeMarshalling.go
34+
rm -f struct/src/*/SbeMarshalling.go struct/src/*/*/SbeMarshalling.go
3535

36-
# Example code and benchmarking
37-
example: src/example-schema/example-schema
38-
src/example-schema/example-schema: $(DEP)
39-
(export GOPATH=$(GOPATH) && \
40-
cd src/example-schema && \
36+
# Example code and benchmarking
37+
example: struct/src/example-schema/example-schema
38+
struct/src/example-schema/example-schema: $(DEP)
39+
(export GOPATH=$(GOPATH)/struct && \
40+
cd struct/src/example-schema && \
4141
go build && \
4242
go fmt && \
4343
./example-schema)
4444

4545
bench: $(DEP)
46-
(export GOPATH=$(GOPATH) && \
47-
cd src/example-schema && \
46+
(export GOPATH=$(GOPATH)/struct && \
47+
cd struct/src/example-schema && \
4848
go test --bench . -cpuprofile=cpu.out && \
4949
go tool pprof -text example-schema.test cpu.out)
5050

51-
src/baseline/SbeMarshalling.go: $(DEP)
51+
struct/src/baseline/SbeMarshalling.go: $(DEP)
5252
$(GOPATH)/sbe-tool -d src -s $(EXAMPLES_SCHEMA)
53-
(export GOPATH=$(GOPATH) && \
54-
cd src/baseline && \
53+
(export GOPATH=$(GOPATH)/struct && \
54+
cd struct/src/baseline && \
5555
go build && \
5656
go fmt && \
5757
go test && \
5858
go install)
5959

6060
# The first set does a make install as there is a test that uses
6161
# multiple packages and needs them in GOPATH The second set work in
62-
# src/foo, and the third need a GOPATH addition as for Java and C++
62+
# struct/src/foo, and the third need a GOPATH addition as for Java and C++
6363
# they generate into the same directory but golang doesn't allow that
6464
test: $(DEP)
65-
(export GOPATH=$(GOPATH) && \
65+
(export GOPATH=$(GOPATH)/struct && \
6666
(for t in baseline extension; do \
67-
export GOPATH=$(GOPATH) && \
68-
cd $(GOPATH)/src/$$t && \
67+
export GOPATH=$(GOPATH)/struct && \
68+
cd $(GOPATH)/struct/src/$$t && \
6969
go build && \
7070
go fmt && \
7171
go test && \
7272
go install \
7373
;done))
74-
(export GOPATH=$(GOPATH) && \
74+
(export GOPATH=$(GOPATH)/struct && \
7575
(for t in baseline-bigendian mktdata group_with_data group_with_data_extension composite_elements composite since-deprecated simple issue435 issue472 issue483 issue488 issue560 issue847 issue848 issue849 issue505 test973; do \
76-
cd $(GOPATH)/src/$$t && \
76+
cd $(GOPATH)/struct/src/$$t && \
7777
go build && \
7878
go fmt && \
7979
go test \
8080
;done))
8181
(for t in vardata group basic; do \
82-
export GOPATH=$(GOPATH)/$$t && \
83-
cd $(GOPATH)/src/$$t/'SBE tests' && \
82+
export GOPATH=$(GOPATH)/struct/$$t && \
83+
cd $(GOPATH)/struct/src/$$t/'SBE tests' && \
8484
go build && \
8585
go fmt && \
8686
go test \
8787
;done)
88+
(export GOPATH=$(GOPATH)/flyweight && \
89+
(for t in baseline extension; do \
90+
export GOPATH=$(GOPATH)/flyweight && \
91+
cd $(GOPATH)/flyweight/src/$$t && \
92+
go build && \
93+
go fmt && \
94+
go test && \
95+
go install \
96+
;done))
97+
(export GOPATH=$(GOPATH)/flyweight && \
98+
(for t in baseline-bigendian mktdata group_with_data group_with_data_extension composite_elements composite since-deprecated simple issue435 issue472 issue483 issue488 issue560 issue847 issue848 issue849; do \
99+
cd $(GOPATH)/flyweight/src/$$t && \
100+
go build && \
101+
go fmt && \
102+
go test \
103+
;done))
104+
(for t in vardata group basic; do \
105+
export GOPATH=$(GOPATH)/flyweight/$$t && \
106+
cd $(GOPATH)/flyweight/src/$$t/'SBE tests' && \
107+
go build && \
108+
go fmt && \
109+
go test \
110+
;done)
111+
(export SBE_JAR=$(SBE_JAR) && \
112+
export GO111MODULE=on && \
113+
(cd $(GOPATH)/flyweight/src/otf && \
114+
go generate && \
115+
go build && \
116+
go fmt && \
117+
go test))

gocode/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ There is now a
55
[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

8+
Go supports both generating Go structs with encode / decode methods, and flyweights like the other languages.
9+
Structs are generated by default for compatibility. Set `sbe.go.generate.generate.flyweights=true` to generate flyweights.
10+
811
Code Layout
912
-----------
1013
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).
14+
[here](https://github.com/real-logic/simple-binary-encoding/tree/master/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang). There is both a struct and a flyweight generator.
1215

1316
Golang code used for testing resides in the top-level
1417
[gocode directory](https://github.com/real-logic/simple-binary-encoding/tree/master/gocode).
@@ -24,11 +27,12 @@ the build environment, example and test code are structured into a top
2427
level gocode directory hierarchy.
2528

2629
Code is generated into this structure with pre-existing test code in place.
30+
There are tests for both the struct and flyweight styles of generated code.
2731

2832
For the example, the code is generated into a library and the matching
2933
example code lives in it's own directory at the same level. For
3034
example, the example-schema generates the baseline library code into
31-
`gocode/src/baseline` and example code lives in `gocode/src/example-schema`.
35+
`gocode/src/struct/baseline` and example code lives in `gocode/src/example-schema`.
3236

3337
To use this layout you should `set GOPATH=/path/to/gocode` or use the
3438
supplied Makefile which does this for you. For the tests you will need
@@ -60,5 +64,3 @@ generator processes.
6064
Roadmap
6165
=======
6266
* Windows developer support (currently tested on Linux/MacOS)
63-
* Further Unicode support
64-
* Testing/Bug fixes

0 commit comments

Comments
 (0)