Skip to content

Commit

Permalink
Fix CI for cpp generator (#432)
Browse files Browse the repository at this point in the history
We wanted to publish C++ SDK as soon as possible, so we postponed fixing
the continuous integration for the generator.

In this patch, we fix all the minor glitches, and the CI now passes.
  • Loading branch information
mristin authored Jan 19, 2024
1 parent 89df0e1 commit aa42369
Show file tree
Hide file tree
Showing 40 changed files with 5,411 additions and 5,603 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Call the generator with the appropriate target:
usage: aas-core-codegen [-h] --model_path MODEL_PATH --snippets_dir
SNIPPETS_DIR --output_dir OUTPUT_DIR --target
{csharp,golang,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context}
{csharp,cpp,golang,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context}
[--version]
Generate implementations and schemas based on an AAS meta-model.
Expand All @@ -153,7 +153,7 @@ Call the generator with the appropriate target:
specific code snippets
--output_dir OUTPUT_DIR
path to the generated code
--target {csharp,golang,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context}
--target {csharp,cpp,golang,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context}
target language or schema
--version show the current version and exit
Expand Down
61 changes: 31 additions & 30 deletions aas_core_codegen/cpp/aas_common/_generate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Generate C++ code of common functions by including the code directly."""

# pylint: disable=line-too-long

import io
from typing import List

Expand All @@ -17,7 +19,6 @@
INDENT2 as II,
INDENT3 as III,
INDENT4 as IIII,
INDENT5 as IIIII
)


Expand All @@ -27,32 +28,34 @@ def _generate_concatenate_definitions_for_2_parts_and_above() -> List[Stripped]:
>>> print(_generate_concatenate_definitions_for_2_parts_and_above()[0])
/**
* Concatenate two strings.
* Concatenate 2 strings.
*
* \\param part0 1st part of the concatenation
* \\param part1 2nd part of the concatenation
* \\return a concatenation of the 2 parts
*/
std::wstring Concat(
const std::wstring& part0,
const std::wstring& part1
std::string Concat(
const std::string& part0,
const std::string& part1
);
"""
concat_funcs = [] # type: List[Stripped]
for string_type in ['std::string', 'std::wstring']:
for string_type in ["std::string", "std::wstring"]:
for count in range(2, 65):
param_descriptions = [] # type: List[str]
for i in range(0, count):
ordinal: str

if i % 10 == 1:
ordinal = f"{i}st"
elif i % 10 == 2:
ordinal = f"{i}nd"
elif i % 10 == 3:
ordinal = f"{i}rd"
ordinal_i = i + 1

if ordinal_i % 10 == 1:
ordinal = f"{ordinal_i}st"
elif ordinal_i % 10 == 2:
ordinal = f"{ordinal_i}nd"
elif ordinal_i % 10 == 3:
ordinal = f"{ordinal_i}rd"
else:
ordinal = f"{i}th"
ordinal = f"{ordinal_i}th"

param_descriptions.append(
f" * \\param part{i} {ordinal} part of the concatenation"
Expand Down Expand Up @@ -86,15 +89,15 @@ def _generate_concatenate_implementations_for_2_parts_and_above() -> List[Stripp
Generate the implementation of ``concat`` functions.
>>> print(_generate_concatenate_implementations_for_2_parts_and_above()[0])
std::wstring Concat(
const std::wstring& part0,
const std::wstring& part1
std::string Concat(
const std::string& part0,
const std::string& part1
) {
size_t size = 0;
size += part0.size();
size += part1.size();
<BLANKLINE>
std::wstring result;
std::string result;
result.reserve(size);
<BLANKLINE>
result.append(part0);
Expand All @@ -104,7 +107,7 @@ def _generate_concatenate_implementations_for_2_parts_and_above() -> List[Stripp
}
"""
concat_funcs = [] # type: List[Stripped]
for string_type in ['std::string', 'std::wstring']:
for string_type in ["std::string", "std::wstring"]:
for count in range(2, 65):
args_definition = ",\n".join(
f"{I}const {string_type}& part{i}" for i in range(0, count)
Expand Down Expand Up @@ -163,7 +166,6 @@ def generate_header(library_namespace: Stripped) -> str:
{I});
}}"""
)

] # type: List[Stripped]
for i in range(1, 16):
typenames_joined = ",\n".join(
Expand Down Expand Up @@ -204,7 +206,7 @@ def generate_header(library_namespace: Stripped) -> str:
),
cpp_common.WARNING,
Stripped(
f"""\
"""\
#pragma warning(push, 0)
#include <algorithm>
#include <functional>
Expand Down Expand Up @@ -254,12 +256,12 @@ def generate_header(library_namespace: Stripped) -> str:
namespace {cpp_common.COMMON_NAMESPACE} {{"""
),
Stripped(
f"""\
// Please keep in sync with the preprocessing directives above in the include block.
"""\
// Please keep in sync with the preprocessing directives above in the include block.
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
// Standard library provides std::optional in C++17 and above.
using std::optional;
using std::nullopt;
using std::nullopt;
using std::make_optional;
#else
using tl::optional;
Expand All @@ -268,7 +270,7 @@ def generate_header(library_namespace: Stripped) -> str:
#endif"""
),
Stripped(
f"""\
"""\
// Please keep in sync with the preprocessing directives above in the include block.
#if ((defined(_MSVC_LANG) && _MSVC_LANG > 202002L) || __cplusplus > 202002L)
using std::expected;
Expand Down Expand Up @@ -373,7 +375,7 @@ def generate_header(library_namespace: Stripped) -> str:
* \\param start of the range
* \\param end of the range
* \\return \\parblock
* `true` if any number between \\p start and
* `true` if any number between \\p start and
* \\p end (excluded) satisfy the \\p condition
* \\endparblock
*/
Expand Down Expand Up @@ -414,9 +416,8 @@ def generate_header(library_namespace: Stripped) -> str:
{I}) != container_end;
}}"""
),

Stripped(
f"""\
"""\
/**
* Convert platform-independent the wide string to a UTF-8 string.
*
Expand All @@ -442,7 +443,7 @@ def generate_header(library_namespace: Stripped) -> str:
);"""
),
Stripped(
f"""\
"""\
/**
* Convert platform-independent the UTF-8 encoded string to a wide string.
*
Expand Down Expand Up @@ -493,7 +494,7 @@ def generate_implementation(library_namespace: Stripped) -> str:
#include "{include_prefix_path}/common.hpp"'''
),
Stripped(
f"""\
"""\
#pragma warning(push, 0)
#include <algorithm>
#pragma warning(pop)
Expand Down Expand Up @@ -586,7 +587,7 @@ def generate_implementation(library_namespace: Stripped) -> str:
{II}text_size_int,
{II}&(result[0]),
{II}size_needed,
{II}nullptr,
{II}nullptr,
{II}nullptr
{I});
Expand Down
16 changes: 13 additions & 3 deletions aas_core_codegen/cpp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ def generate_type(
# concrete descendants since we want to allow enhancing.
interface_name = cpp_naming.interface_name(our_type.name)

type_identifier: str

if types_namespace is None:
type_identifier = interface_name
else:
Expand Down Expand Up @@ -443,7 +445,7 @@ def break_type_in_lines(text: str) -> str:
buffer = io.StringIO()

level = 0
last_match = None # type: Optional[re.Match]
last_match = None # type: Optional[re.Match[str]]
for match in _ANGLE_BRACKETS_IN_TYPE_RE.finditer(text):
if last_match is None:
buffer.write(text[: match.start()])
Expand All @@ -466,7 +468,13 @@ def break_type_in_lines(text: str) -> str:

last_match = match

if last_match is None:
# NOTE (mristin, 2024-01-18):
# Not a signle angle bracket has been detected.
return text

buffer.write(text[last_match.end() :])

return buffer.getvalue()


Expand Down Expand Up @@ -501,14 +509,16 @@ def __next__(self) -> Identifier:

return result


def generate_include_prefix_path(library_namespace: Stripped) -> Stripped:
"""
Generate the prefix path for the includes.
>>> generate_namespace_opening(Stripped("some::name::space"))
>>> generate_include_prefix_path(Stripped("some::name::space"))
'some/name/space'
"""
return Stripped(library_namespace.replace('::', '/'))
return Stripped(library_namespace.replace("::", "/"))


def generate_namespace_opening(library_namespace: Stripped) -> Stripped:
"""
Expand Down
7 changes: 2 additions & 5 deletions aas_core_codegen/cpp/constants/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ def _generate_constant_set_of_primitives_definition(
writer.write(comment)
writer.write("\n")

# noinspection PyUnusedLocal
literal_codes = [] # type: List[str]

# noinspection PyUnusedLocal
set_type = None # type: Optional[str]

Expand Down Expand Up @@ -404,14 +401,14 @@ def generate_header(
),
cpp_common.WARNING,
Stripped(
f'''\
f"""\
#include "{include_prefix_path}/types.hpp"
#pragma warning(push, 0)
#include <cstdint>
#include <unordered_set>
#include <vector>
#pragma warning(pop)'''
#pragma warning(pop)"""
),
cpp_common.generate_namespace_opening(library_namespace),
Stripped(
Expand Down
4 changes: 3 additions & 1 deletion aas_core_codegen/cpp/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ def _is_single_paragraph(para: docutils.nodes.paragraph) -> bool:
or not.
"""
result, error = _IS_SINGLE_PARAGRAPHER.transform(para)

assert error is None, f"Unexpected error: {error}"
assert result is not None

return result


Expand Down Expand Up @@ -418,7 +421,6 @@ def transform_note(

assert text is not None

# TODO (mristin, 2023-06-28): check this before the release!
if (
len(element.children) == 1
and isinstance(element.children[0], docutils.nodes.paragraph)
Expand Down
Loading

0 comments on commit aa42369

Please sign in to comment.