Skip to content

Commit e8b28ab

Browse files
committed
[Rust] Deal with case of length=1 on char arrays with presence constant. Issue #505.
1 parent fadc324 commit e8b28ab

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/EncodedDataType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ private PrimitiveValue processConstantChar(final Node node, final String lengthA
282282

283283
if (valueLength == 1)
284284
{
285-
if (null == lengthAttr)
285+
if (null == lengthAttr || length == 1)
286286
{
287287
primitiveValue = PrimitiveValue.parse(nodeValue, primitiveType, characterEncoding);
288288
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2017 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package uk.co.real_logic.sbe.generation.java;
17+
18+
import org.agrona.DirectBuffer;
19+
import org.agrona.MutableDirectBuffer;
20+
import org.agrona.generation.StringWriterOutputManager;
21+
import org.junit.Test;
22+
import uk.co.real_logic.sbe.TestUtil;
23+
import uk.co.real_logic.sbe.ir.Ir;
24+
import uk.co.real_logic.sbe.xml.IrGenerator;
25+
import uk.co.real_logic.sbe.xml.MessageSchema;
26+
import uk.co.real_logic.sbe.xml.ParserOptions;
27+
28+
import static org.hamcrest.core.StringContains.containsString;
29+
import static org.junit.Assert.assertThat;
30+
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
31+
32+
public class ConstantCharArrayTest
33+
{
34+
private static final Class<?> BUFFER_CLASS = MutableDirectBuffer.class;
35+
private static final String BUFFER_NAME = BUFFER_CLASS.getName();
36+
private static final Class<DirectBuffer> READ_ONLY_BUFFER_CLASS = DirectBuffer.class;
37+
private static final String READ_ONLY_BUFFER_NAME = READ_ONLY_BUFFER_CLASS.getName();
38+
39+
@Test
40+
public void shouldGenerateConstCharArrayMethods() throws Exception
41+
{
42+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
43+
final MessageSchema schema = parse(TestUtil.getLocalResource("issue505.xml"), options);
44+
final IrGenerator irg = new IrGenerator();
45+
final Ir ir = irg.generate(schema);
46+
47+
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
48+
outputManager.setPackageName(ir.applicableNamespace());
49+
final JavaGenerator generator = new JavaGenerator(
50+
ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, outputManager);
51+
52+
generator.generate();
53+
final String sources = outputManager.getSources().toString();
54+
55+
final String expectedOne =
56+
" public byte sourceOne()\n" +
57+
" {\n" +
58+
" return (byte)67;\n" +
59+
" }";
60+
assertThat(sources, containsString(expectedOne));
61+
62+
final String expectedTwo =
63+
" public byte sourceTwo()\n" +
64+
" {\n" +
65+
" return (byte)68;\n" +
66+
" }";
67+
assertThat(sources, containsString(expectedTwo));
68+
69+
final String expectedThree =
70+
" public String sourceThree()\n" +
71+
" {\n" +
72+
" return \"EF\";\n" +
73+
" }";
74+
assertThat(sources, containsString(expectedThree));
75+
76+
final String expectedFour =
77+
" public String sourceFour()\n" +
78+
" {\n" +
79+
" return \"GH\";\n" +
80+
" }";
81+
assertThat(sources, containsString(expectedFour));
82+
}
83+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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="header">
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+
17+
<type name="idSourceOne" presence="constant" length="1" primitiveType="char">C</type>
18+
<type name="idSourceTwo" presence="constant" primitiveType="char">D</type>
19+
<type name="idSourceThree" presence="constant" primitiveType="char">EF</type>
20+
<type name="idSourceFour" presence="constant" length="2" primitiveType="char">GH</type>
21+
</types>
22+
23+
<sbe:message name="SomeMessage" id="1">
24+
<field name="sourceOne" id="1" type="idSourceOne"/>
25+
<field name="sourceTwo" id="2" type="idSourceTwo"/>
26+
<field name="sourceThree" id="3" type="idSourceThree"/>
27+
<field name="sourceFour" id="4" type="idSourceFour" />
28+
</sbe:message>
29+
</sbe:messageSchema>

0 commit comments

Comments
 (0)