Skip to content

Commit ce2c4f9

Browse files
committed
[Rust] Capture types by ref name when applicable. Issue #496.
1 parent 54ee93c commit ce2c4f9

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/Ir.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,27 @@ private void captureTypes(final List<Token> tokens, final int beginIndex, final
312312
switch (token.signal())
313313
{
314314
case BEGIN_COMPOSITE:
315-
i = captureType(tokens, i, Signal.END_COMPOSITE, token.name());
315+
i = captureType(tokens, i, Signal.END_COMPOSITE, token.name(), token.referencedName());
316316
captureTypes(tokens, typeBeginIndex + 1, i - 1);
317317
break;
318318

319319
case BEGIN_ENUM:
320-
i = captureType(tokens, i, Signal.END_ENUM, token.name());
320+
i = captureType(tokens, i, Signal.END_ENUM, token.name(), token.referencedName());
321321
break;
322322

323323
case BEGIN_SET:
324-
i = captureType(tokens, i, Signal.END_SET, token.name());
324+
i = captureType(tokens, i, Signal.END_SET, token.name(), token.referencedName());
325325
break;
326326
}
327327
}
328328
}
329329

330-
private int captureType(final List<Token> tokens, final int index, final Signal endSignal, final String name)
330+
private int captureType(
331+
final List<Token> tokens,
332+
final int index,
333+
final Signal endSignal,
334+
final String name,
335+
final String referencedName)
331336
{
332337
final List<Token> typeTokens = new ArrayList<>();
333338

@@ -342,7 +347,15 @@ private int captureType(final List<Token> tokens, final int index, final Signal
342347
while (endSignal != token.signal() || !name.equals(token.name()));
343348

344349
updateComponentTokenCounts(typeTokens);
345-
typesByNameMap.put(name, typeTokens);
350+
351+
if (null == referencedName)
352+
{
353+
typesByNameMap.put(name, typeTokens);
354+
}
355+
else
356+
{
357+
typesByNameMap.put(referencedName, typeTokens);
358+
}
346359

347360
return i;
348361
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.ir;
17+
18+
import org.junit.Test;
19+
import uk.co.real_logic.sbe.xml.IrGenerator;
20+
import uk.co.real_logic.sbe.xml.MessageSchema;
21+
import uk.co.real_logic.sbe.xml.ParserOptions;
22+
23+
import static org.junit.Assert.assertNotNull;
24+
import static uk.co.real_logic.sbe.TestUtil.getLocalResource;
25+
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
26+
27+
public class CompositeRefsTest
28+
{
29+
@Test
30+
public void shouldGenerateCorrectIrForCompositeRefs()
31+
throws Exception
32+
{
33+
final MessageSchema schema = parse(getLocalResource("issue496.xml"), ParserOptions.DEFAULT);
34+
final IrGenerator irg = new IrGenerator();
35+
final Ir ir = irg.generate(schema);
36+
37+
assertNotNull(ir.getType("compositeOne"));
38+
assertNotNull(ir.getType("compositeTwo"));
39+
assertNotNull(ir.getType("compositeThree"));
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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="string10" length="10" primitiveType="char"/>
18+
19+
<composite name="compositeThree">
20+
<ref name="field1" type="string10"/>
21+
</composite>
22+
23+
<composite name="compositeTwo">
24+
<ref name="compFieldTwo" type="string10"/>
25+
<ref name="compThree" type="compositeThree"/>
26+
</composite>
27+
28+
<composite name="compositeOne">
29+
<ref name="compFieldOne" type="string10"/>
30+
<ref name="compTwo" type="compositeTwo"/>
31+
</composite>
32+
</types>
33+
34+
<sbe:message name="SomeMessage" id="1">
35+
<field name="id" id="1" type="int64"/>
36+
<field name="comp" id="2" type="compositeOne"/>
37+
</sbe:message>
38+
</sbe:messageSchema>

0 commit comments

Comments
 (0)