Skip to content

Commit

Permalink
Added semantic version as a static public member (#931)
Browse files Browse the repository at this point in the history
* Added semantic version as a static public member accessible in the generated stubs.

* Added generated java ir codecs.

* Added constexpr in static c++ semanticVersion.

* Fixing cpp codeGenTest.

* Adjustments based on the pr comments.

* Adjustments based on the pr comments.
  • Loading branch information
renanleonellocastro committed Mar 12, 2023
1 parent 7186909 commit 0385eb7
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,7 @@ private CharSequence generateMessageFlyweightFunctions(
final String schemaVersionType = cTypeName(ir.headerStructure().schemaVersionType());
final String semanticType = token.encoding().semanticType() == null ? "" : token.encoding().semanticType();
final String messageHeaderStruct = formatScopedName(scope, "messageHeader");
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

return String.format("\n" +
"SBE_ONE_DEF uint64_t %10$s_sbe_position(\n" +
Expand Down Expand Up @@ -1974,6 +1975,11 @@ private CharSequence generateMessageFlyweightFunctions(
" return %8$s;\n" +
"}\n\n" +

"SBE_ONE_DEF const char* %10$s_sbe_semantic_version(void)\n" +
"{\n" +
" return \"%12$s\";\n" +
"}\n\n" +

"SBE_ONE_DEF const char *%10$s_sbe_semantic_type(void)\n" +
"{\n" +
" return \"%9$s\";\n" +
Expand Down Expand Up @@ -2093,7 +2099,8 @@ private CharSequence generateMessageFlyweightFunctions(
generateLiteral(ir.headerStructure().schemaVersionType(), Integer.toString(ir.version())),
semanticType,
structName,
messageHeaderStruct);
messageHeaderStruct,
semanticVersion);
}

private CharSequence generateFieldFunctions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void generate() throws IOException
generateDisplay(sb, msgToken.name(), fields, groups, varData);
sb.append(generateMessageLength(groups, varData, BASE_INDENT));
sb.append("};\n");
sb.append(generateStaticVariablesInitialization(className));
sb.append(CppUtil.closingBraces(ir.namespaces().length)).append("#endif\n");
out.append(sb);
}
Expand Down Expand Up @@ -1973,6 +1974,7 @@ private CharSequence generateMessageFlyweightCode(final String className, final
final String schemaVersionType = cppTypeName(ir.headerStructure().schemaVersionType());
final String semanticType = token.encoding().semanticType() == null ? "" : token.encoding().semanticType();
final String headerType = ir.headerStructure().tokens().get(0).name();
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

return String.format(
"private:\n" +
Expand All @@ -1992,7 +1994,8 @@ private CharSequence generateMessageFlyweightCode(final String className, final
" static const %1$s SBE_BLOCK_LENGTH = %2$s;\n" +
" static const %3$s SBE_TEMPLATE_ID = %4$s;\n" +
" static const %5$s SBE_SCHEMA_ID = %6$s;\n" +
" static const %7$s SBE_SCHEMA_VERSION = %8$s;\n\n" +
" static const %7$s SBE_SCHEMA_VERSION = %8$s;\n" +
" static const char* SBE_SEMANTIC_VERSION;\n\n" +

" enum MetaAttribute\n" +
" {\n" +
Expand Down Expand Up @@ -2039,6 +2042,11 @@ private CharSequence generateMessageFlyweightCode(final String className, final
" return %8$s;\n" +
" }\n\n" +

" SBE_NODISCARD static const char *sbeSemanticVersion() SBE_NOEXCEPT\n" +
" {\n" +
" return \"%13$s\";\n" +
" }\n\n" +

" SBE_NODISCARD static SBE_CONSTEXPR const char *sbeSemanticType() SBE_NOEXCEPT\n" +
" {\n" +
" return \"%9$s\";\n" +
Expand Down Expand Up @@ -2151,7 +2159,8 @@ private CharSequence generateMessageFlyweightCode(final String className, final
semanticType,
className,
generateConstructorsAndOperators(className),
formatClassName(headerType));
formatClassName(headerType),
semanticVersion);
}

private void generateFields(
Expand Down Expand Up @@ -3184,4 +3193,15 @@ private CharSequence generateMessageLength(final List<Token> groups, final List<

return sb;
}

private CharSequence generateStaticVariablesInitialization(final String className)
{
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

return String.format(
"\n" +
"const char* %1$s::SBE_SEMANTIC_VERSION = \"%2$s\";\n\n",
className,
semanticVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1163,13 +1163,15 @@ private CharSequence generateMessageFlyweightCode(final String className, final
final String schemaIdType = cSharpTypeName(ir.headerStructure().schemaIdType());
final String schemaVersionType = cSharpTypeName(ir.headerStructure().schemaVersionType());
final String semanticType = token.encoding().semanticType() == null ? "" : token.encoding().semanticType();
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

return String.format(
indent + INDENT + "public const %1$s BlockLength = %2$s;\n" +
indent + INDENT + "public const %3$s TemplateId = %4$s;\n" +
indent + INDENT + "public const %5$s SchemaId = %6$s;\n" +
indent + INDENT + "public const %7$s SchemaVersion = %8$s;\n" +
indent + INDENT + "public const string SemanticType = \"%9$s\";\n\n" +
indent + INDENT + "public const string SemanticType = \"%9$s\";\n" +
indent + INDENT + "public const string SemanticVersion = \"%11$s\";\n\n" +
indent + INDENT + "private readonly %10$s _parentMessage;\n" +
indent + INDENT + "private DirectBuffer _buffer;\n" +
indent + INDENT + "private int _offset;\n" +
Expand Down Expand Up @@ -1238,7 +1240,8 @@ private CharSequence generateMessageFlyweightCode(final String className, final
schemaVersionType,
generateLiteral(ir.headerStructure().schemaVersionType(), Integer.toString(ir.version())),
semanticType,
className);
className,
semanticVersion);
}

private CharSequence generateFields(final List<Token> tokens, final String indent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,7 @@ private void generateMessageCode(
final String templateIdType = golangTypeName(ir.headerStructure().templateIdType());
final String schemaIdType = golangTypeName(ir.headerStructure().schemaIdType());
final String schemaVersionType = golangTypeName(ir.headerStructure().schemaVersionType());
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

generateEncodeDecode(sb, typeName, tokens, true, true);

Expand All @@ -2117,6 +2118,9 @@ private void generateMessageCode(
"}\n" +
"\nfunc (*%1$s) SbeSemanticType() (semanticType []byte) {\n" +
"\treturn []byte(\"%10$s\")\n" +
"}\n" +
"\nfunc (*%1$s) SbeSemanticVersion() (semanticVersion string) {\n" +
"\treturn \"%11$s\"\n" +
"}\n",
typeName,
blockLengthType,
Expand All @@ -2127,7 +2131,8 @@ private void generateMessageCode(
generateLiteral(ir.headerStructure().schemaIdType(), Integer.toString(ir.id())),
schemaVersionType,
generateLiteral(ir.headerStructure().schemaVersionType(), Integer.toString(ir.version())),
semanticType));
semanticType,
semanticVersion));
}

// Used for groups which need to know the schema's definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2611,10 +2611,12 @@ private CharSequence generateFixedFlyweightCode(
final String schemaIdAccessorType = shouldGenerateInterfaces ? "int" : schemaIdType;
final String schemaVersionType = javaTypeName(ir.headerStructure().schemaVersionType());
final String schemaVersionAccessorType = shouldGenerateInterfaces ? "int" : schemaVersionType;
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

return String.format(
" public static final %5$s SCHEMA_ID = %6$s;\n" +
" public static final %7$s SCHEMA_VERSION = %8$s;\n" +
" public static final String SEMANTIC_VERSION = \"%11$s\";\n" +
" public static final int ENCODED_LENGTH = %2$d;\n" +
" public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.%4$s;\n\n" +
" private int offset;\n" +
Expand Down Expand Up @@ -2657,7 +2659,8 @@ private CharSequence generateFixedFlyweightCode(
schemaVersionType,
generateLiteral(ir.headerStructure().schemaVersionType(), Integer.toString(ir.version())),
schemaIdAccessorType,
schemaVersionAccessorType);
schemaVersionAccessorType,
semanticVersion);
}

private CharSequence generateDecoderFlyweightCode(final String className, final Token token)
Expand Down Expand Up @@ -2735,6 +2738,7 @@ private CharSequence generateFlyweightCode(
final String schemaVersionType = javaTypeName(headerStructure.schemaVersionType());
final String schemaVersionAccessorType = shouldGenerateInterfaces ? "int" : schemaVersionType;
final String semanticType = token.encoding().semanticType() == null ? "" : token.encoding().semanticType();
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();
final String actingFields = codecType == CodecType.ENCODER ?
"" :
" int actingBlockLength;\n" +
Expand All @@ -2745,6 +2749,7 @@ private CharSequence generateFlyweightCode(
" public static final %3$s TEMPLATE_ID = %4$s;\n" +
" public static final %5$s SCHEMA_ID = %6$s;\n" +
" public static final %7$s SCHEMA_VERSION = %8$s;\n" +
" public static final String SEMANTIC_VERSION = \"%19$s\";\n" +
" public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.%14$s;\n\n" +
" private final %9$s parentMessage = this;\n" +
" private %11$s buffer;\n" +
Expand Down Expand Up @@ -2815,7 +2820,8 @@ private CharSequence generateFlyweightCode(
blockLengthAccessorType,
templateIdAccessorType,
schemaIdAccessorType,
schemaVersionAccessorType);
schemaVersionAccessorType,
semanticVersion);
}

private CharSequence generateEncoderFlyweightCode(final String className, final Token token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ public void generate() throws IOException
final String templateIdType = rustTypeName(ir.headerStructure().templateIdType());
final String schemaIdType = rustTypeName(ir.headerStructure().schemaIdType());
final String schemaVersionType = schemaVersionType();
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();
indent(out, 0, "pub const SBE_BLOCK_LENGTH: %s = %d;\n", blockLengthType, msgToken.encodedLength());
indent(out, 0, "pub const SBE_TEMPLATE_ID: %s = %d;\n", templateIdType, msgToken.id());
indent(out, 0, "pub const SBE_SCHEMA_ID: %s = %d;\n", schemaIdType, ir.id());
indent(out, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n\n", schemaVersionType, ir.version());
indent(out, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n", schemaVersionType, ir.version());
indent(out, 0, "pub const SBE_SEMANTIC_VERSION: &str = \"%s\";\n\n", semanticVersion);

MessageCoderDef.generateEncoder(ir, out, msgToken, fields, groups, varData);
MessageCoderDef.generateDecoder(ir, out, msgToken, fields, groups, varData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class FrameCodecDecoder
public static final int TEMPLATE_ID = 1;
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

private final FrameCodecDecoder parentMessage = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class FrameCodecEncoder
public static final int TEMPLATE_ID = 1;
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

private final FrameCodecEncoder parentMessage = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class MessageHeaderDecoder
{
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final int ENCODED_LENGTH = 8;
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class MessageHeaderEncoder
{
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final int ENCODED_LENGTH = 8;
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class TokenCodecDecoder
public static final int TEMPLATE_ID = 2;
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

private final TokenCodecDecoder parentMessage = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class TokenCodecEncoder
public static final int TEMPLATE_ID = 2;
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

private final TokenCodecEncoder parentMessage = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class VarDataEncodingDecoder
{
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final int ENCODED_LENGTH = -1;
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class VarDataEncodingEncoder
{
public static final int SCHEMA_ID = 1;
public static final int SCHEMA_VERSION = 0;
public static final String SEMANTIC_VERSION = "";
public static final int ENCODED_LENGTH = -1;
public static final java.nio.ByteOrder BYTE_ORDER = java.nio.ByteOrder.LITTLE_ENDIAN;

Expand Down
1 change: 1 addition & 0 deletions sbe-tool/src/test/c/CodeGenTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ TEST_F(CodeGenTest, shouldReturnCorrectValuesForCarStaticFields)
EXPECT_EQ(CGT(car_sbe_schema_id)(), 6u);
EXPECT_EQ(CGT(car_sbe_schema_version)(), 0u);
EXPECT_EQ(std::string(CGT(car_sbe_semantic_type)()), std::string(""));
EXPECT_EQ(CGT(car_sbe_semantic_version)(), "5.2");
}

TEST_F(CodeGenTest, shouldBeAbleToEncodeMessageHeaderCorrectly)
Expand Down
1 change: 1 addition & 0 deletions sbe-tool/src/test/cpp/CodeGenTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ TEST_F(CodeGenTest, shouldReturnCorrectValuesForCarStaticFields)
EXPECT_EQ(Car::sbeSchemaId(), 6u);
EXPECT_EQ(Car::sbeSchemaVersion(), 0u);
EXPECT_EQ(std::string(Car::sbeSemanticType()), std::string(""));
EXPECT_EQ(std::string(Car::sbeSemanticVersion()), std::string("5.2"));
}

TEST_F(CodeGenTest, shouldBeAbleToEncodeMessageHeaderCorrectly)
Expand Down

0 comments on commit 0385eb7

Please sign in to comment.