Skip to content

Commit e5c515d

Browse files
authored
Merge pull request #491 from MarketFactory/issue488-golang
Issue #488. Don't deference a null characterEncoding
2 parents abd5b4c + 250f455 commit e5c515d

File tree

6 files changed

+86
-38
lines changed

6 files changed

+86
-38
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ task(generateGolangCodecsWithXSD, type: JavaExec) {
524524
'sbe-tool/src/test/resources/issue435.xml',
525525
'sbe-tool/src/test/resources/issue472.xml',
526526
'sbe-tool/src/test/resources/issue483.xml',
527+
'sbe-tool/src/test/resources/issue488.xml',
527528
'sbe-tool/src/test/resources/since-deprecated-test-schema.xml',
528529
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
529530
'gocode/resources/example-composite.xml',

gocode/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test: $(DEP)
6868
go install \
6969
;done))
7070
(export GOPATH=$(GOPATH) && \
71-
(for t in baseline-bigendian mktdata group_with_data group_with_data_extension composite_elements composite since-deprecated simple issue435 issue472; do \
71+
(for t in baseline-bigendian mktdata group_with_data group_with_data_extension composite_elements composite since-deprecated simple issue435 issue472 issue483 issue488; do \
7272
cd $(GOPATH)/src/$$t && \
7373
go build && \
7474
go fmt && \

gocode/src/issue483/Issue483_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import (
77
func TestPresence(t *testing.T) {
88
issue483 := new(Issue483)
99

10-
if issue483.UnsetMetaAttribute(4) != "required" {
11-
t.Log("Unset attribute's presence should be 'required'")
12-
t.Fail()
13-
}
10+
if issue483.UnsetMetaAttribute(4) != "required" {
11+
t.Log("Unset attribute's presence should be 'required'")
12+
t.Fail()
13+
}
1414

15-
if issue483.RequiredMetaAttribute(4) != "required" {
16-
t.Log("Required attribute's presence should be 'required'")
17-
t.Fail()
18-
}
15+
if issue483.RequiredMetaAttribute(4) != "required" {
16+
t.Log("Required attribute's presence should be 'required'")
17+
t.Fail()
18+
}
1919

20-
if issue483.ConstantMetaAttribute(4) != "constant" {
21-
t.Log("Constant attribute's presence should be 'constant'")
22-
t.Fail()
23-
}
20+
if issue483.ConstantMetaAttribute(4) != "constant" {
21+
t.Log("Constant attribute's presence should be 'constant'")
22+
t.Fail()
23+
}
2424

25-
if issue483.OptionalMetaAttribute(4) != "optional" {
26-
t.Log("Optional attribute's presence should be 'optional'")
27-
t.Fail()
28-
}
25+
if issue483.OptionalMetaAttribute(4) != "optional" {
26+
t.Log("Optional attribute's presence should be 'optional'")
27+
t.Fail()
28+
}
2929
}

gocode/src/issue488/Issue488_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package issue488
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPresence(t *testing.T) {
8+
issue488 := new(Issue488)
9+
v := new(VarDataEncoding)
10+
issue488.VarData = make([]uint8, 3)
11+
issue488.VarData[0] = v.VarDataMinValue()
12+
issue488.VarData[1] = 42
13+
issue488.VarData[2] = v.VarDataMaxValue()
14+
if err := issue488.RangeCheck(0, 0); err != nil {
15+
t.Log("RangeCheck failed", err)
16+
t.Fail()
17+
}
18+
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang/GolangGenerator.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,34 @@ private void generateCharacterEncodingRangeCheck(
210210
final String varName,
211211
final Token token)
212212
{
213-
switch (token.encoding().characterEncoding())
213+
final String characterEncoding = token.encoding().characterEncoding();
214+
215+
if (null != characterEncoding)
214216
{
215-
case "ASCII":
216-
this.imports.add("fmt");
217-
sb.append(String.format(
218-
"\tfor idx, ch := range %1$s {\n" +
219-
"\t\tif ch > 127 {\n" +
220-
"\t\t\treturn fmt.Errorf(\"%1$s[%%d]=%%d" +
221-
" failed ASCII validation\", idx, ch)\n" +
222-
"\t\t}\n" +
223-
"\t}\n",
224-
varName));
225-
break;
217+
switch (token.encoding().characterEncoding())
218+
{
219+
case "ASCII":
220+
this.imports.add("fmt");
221+
sb.append(String.format(
222+
"\tfor idx, ch := range %1$s {\n" +
223+
"\t\tif ch > 127 {\n" +
224+
"\t\t\treturn fmt.Errorf(\"%1$s[%%d]=%%d" +
225+
" failed ASCII validation\", idx, ch)\n" +
226+
"\t\t}\n" +
227+
"\t}\n",
228+
varName));
229+
break;
226230

227-
case "UTF-8":
228-
this.imports.add("errors");
229-
this.imports.add("unicode/utf8");
230-
sb.append(String.format(
231-
"\tif !utf8.Valid(%1$s[:]) {\n" +
232-
"\t\treturn errors.New(\"%1$s failed UTF-8 validation\")\n" +
233-
"\t}\n",
234-
varName));
235-
break;
231+
case "UTF-8":
232+
this.imports.add("errors");
233+
this.imports.add("unicode/utf8");
234+
sb.append(String.format(
235+
"\tif !utf8.Valid(%1$s[:]) {\n" +
236+
"\t\treturn errors.New(\"%1$s failed UTF-8 validation\")\n" +
237+
"\t}\n",
238+
varName));
239+
break;
240+
}
236241
}
237242
}
238243

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
3+
package="issue488"
4+
id="488"
5+
version="0"
6+
semanticVersion="1.0"
7+
description="issue 488 test case"
8+
byteOrder="bigEndian">
9+
<types>
10+
<composite name="messageHeader" description="Message identifiers and length of message root">
11+
<type name="blockLength" primitiveType="uint16"/>
12+
<type name="templateId" primitiveType="uint16"/>
13+
<type name="schemaId" primitiveType="uint16"/>
14+
<type name="version" primitiveType="uint16"/>
15+
</composite>
16+
<composite name="varDataEncoding">
17+
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
18+
<type name="varData" primitiveType="uint8" length="0"/>
19+
</composite>
20+
</types>
21+
<sbe:message name="issue488" id="1" description="issue 483 test">
22+
<data name="varData" type="varDataEncoding" id="2"/>
23+
</sbe:message>
24+
</sbe:messageSchema>

0 commit comments

Comments
 (0)