Skip to content

Commit dd6063f

Browse files
authored
[Java] Support valueRef with camelCase enum type identifiers (#963)
The type identifier was previously used as-is (e.g. `carFilterType`) in the generated code for uses of `valueRef` while generated Java enums always start with a capital letter (`CarFilterType`). See the added test in ValueRefWithLowerCaseEnum.java.
1 parent 3a7d445 commit dd6063f

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3074,7 +3074,8 @@ private void generateEnumDecoder(
30743074

30753075
if (fieldToken.isConstantEncoding())
30763076
{
3077-
final String enumValueStr = fieldToken.encoding().constValue().toString();
3077+
final String enumValueStr = formatClassName(
3078+
fieldToken.encoding().constValue().toString());
30783079

30793080
new Formatter(sb).format(
30803081
"\n" +
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2013-2023 Real Logic Limited.
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+
* https://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.jupiter.api.Test;
22+
import uk.co.real_logic.sbe.Tests;
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+
import uk.co.real_logic.sbe.xml.XmlSchemaParser;
28+
29+
import java.io.InputStream;
30+
31+
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.hamcrest.core.StringContains.containsString;
33+
34+
class ValueRefWithLowerCaseEnum
35+
{
36+
private static final Class<?> BUFFER_CLASS = MutableDirectBuffer.class;
37+
private static final String BUFFER_NAME = BUFFER_CLASS.getName();
38+
private static final Class<DirectBuffer> READ_ONLY_BUFFER_CLASS = DirectBuffer.class;
39+
private static final String READ_ONLY_BUFFER_NAME = READ_ONLY_BUFFER_CLASS.getName();
40+
41+
@Test
42+
void shouldGenerateConstCharArrayMethods() throws Exception
43+
{
44+
try (InputStream in = Tests.getLocalResource("value-ref-with-lower-case-enum.xml"))
45+
{
46+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
47+
final MessageSchema schema = XmlSchemaParser.parse(in, options);
48+
final IrGenerator irg = new IrGenerator();
49+
final Ir ir = irg.generate(schema);
50+
51+
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
52+
outputManager.setPackageName(ir.applicableNamespace());
53+
final JavaGenerator generator = new JavaGenerator(
54+
ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, outputManager);
55+
56+
generator.generate();
57+
final String sources = outputManager.getSources().toString();
58+
59+
// Prior to the fix the generated body of the following method was
60+
// `return engineType.gas;`
61+
final String expected =
62+
" public EngineType engineType()\n" +
63+
" {\n" +
64+
" return EngineType.gas;\n" +
65+
" }";
66+
assertThat(sources, containsString(expected));
67+
}
68+
}
69+
}
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="issue505"
4+
id="505"
5+
version="0"
6+
semanticVersion="1.0"
7+
description="issue 505 test case">
8+
<types>
9+
<composite name="messageHeader" description="header">
10+
<type name="blockLength" primitiveType="uint16"/>
11+
<type name="templateId" primitiveType="uint16"/>
12+
<type name="schemaId" primitiveType="uint16"/>
13+
<type name="version" primitiveType="uint16"/>
14+
</composite>
15+
16+
<enum name="engineType" encodingType="uint8">
17+
<validValue name="gas">0</validValue>
18+
</enum>
19+
</types>
20+
21+
<sbe:message name="SomeMessage" id="1">
22+
<field name="engineType" type="engineType" id="1" presence="constant" valueRef="engineType.gas"/>
23+
</sbe:message>
24+
</sbe:messageSchema>

0 commit comments

Comments
 (0)