Skip to content

Commit

Permalink
Merge pull request #7 from AsherGlick/xml_converter
Browse files Browse the repository at this point in the history
Xml converter
  • Loading branch information
klingbolt authored Oct 3, 2023
2 parents 62b8373 + 32ccac8 commit 9da018f
Show file tree
Hide file tree
Showing 17 changed files with 826 additions and 495 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:
- name: Install protoc
run: sudo apt-get install protobuf-compiler

- name: Install gtest
run: sudo apt-get install libgtest-dev

- name: Build xml_converter
run: |
Expand Down
18 changes: 18 additions & 0 deletions xml_converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ if(MSVC)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -Wpedantic)
endif()

### TESTS ######################################################################
# Enable testing using CMake's built-in functionality
enable_testing()

set(TEST_TARGET_NAME xml_converter_tests)

file(GLOB_RECURSE TEST_SOURCES "src/*.cpp")
list(REMOVE_ITEM TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/xml_converter.cpp")

find_package(GTest REQUIRED)

add_executable(${TEST_TARGET_NAME} tests/test_string_hierarchy.cpp ${TEST_SOURCES} ${PROTO_SRC})

target_link_libraries(${TEST_TARGET_NAME} GTest::GTest GTest::Main)
target_link_libraries(${TEST_TARGET_NAME} ${Protobuf_LIBRARIES})

gtest_discover_tests(${TEST_TARGET_NAME})
29 changes: 1 addition & 28 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ vector<string> {{cpp_class}}::as_xml() const {
return xml_node_contents;
}

{% if cpp_class == "Category": %}
waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(string full_category_name, map<string, vector<Parseable*>>* parsed_pois) const {
full_category_name += this->name;
{% else %}
waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% endif %}
waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
waypoint::{{cpp_class}} proto_{{cpp_class_header}};
{% if cpp_class == "Icon": %}
waypoint::Trigger* trigger = nullptr;
Expand Down Expand Up @@ -150,28 +145,6 @@ vector<string> {{cpp_class}}::as_xml() const {
proto_{{cpp_class_header}}.set_allocated_trigger(trigger);
}
{% endif %}
{% if cpp_class == "Category": %}

auto pois = parsed_pois->find(full_category_name);

if (pois != parsed_pois->end()) {
for (unsigned int i = 0; i < pois->second.size(); i++) {
if (pois->second[i]->classname() == "POI") {
Icon* icon = dynamic_cast<Icon*>(pois->second[i]);
proto_{{cpp_class_header}}.add_icon()->MergeFrom(icon->as_protobuf());
}
else if (pois->second[i]->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(pois->second[i]);
proto_{{cpp_class_header}}.add_trail()->MergeFrom(trail->as_protobuf());
}
}
}

for (const auto& [key, val] : this->children) {
waypoint::{{cpp_class}} proto_{{cpp_class_header}}_child = val.as_protobuf(full_category_name + ".", parsed_pois);
proto_{{cpp_class_header}}.add_children()->CopyFrom(proto_{{cpp_class_header}}_child);
}
{% endif %}
return proto_{{cpp_class_header}};
}

Expand Down
6 changes: 1 addition & 5 deletions xml_converter/generators/cpp_templates/class_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ class {{cpp_class}} : public Parseable {
virtual std::vector<std::string> as_xml() const;
virtual std::string classname();
bool init_xml_attribute(rapidxml::xml_attribute<>* attribute, std::vector<XMLError*>* errors, std::string base_dir = "");
{% if cpp_class == "Category": %}
waypoint::{{cpp_class}} as_protobuf(std::string full_category_name, std::map<std::string, std::vector<Parseable*>>* parsed_pois) const;
{% else: %}
waypoint::{{cpp_class}} as_protobuf() const;
{% endif %}
waypoint::{{cpp_class}} as_protobuf() const;
void parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}});
{% if attributes_of_type_marker_category %}
bool validate_attributes_of_type_marker_category();
Expand Down
31 changes: 15 additions & 16 deletions xml_converter/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,21 @@ if (( $? > 0 )); then
error_count=`expr $error_count + 1`
fi

# Run Include What You Use
#
# We have 2 sed filters here, they could be combined but they are split for clarity
# The first one removes all blank lines
# The second one removes all "everything is good" lines from iwyu
#
# TODO: When this or newer versions of iwyu_tool that carry over the exit codes
# from the include-what-you-use command calls are more widely standard this can
# be replaced with just a call to iwyu_tool instead.
echo "Include What You Use"
echo "--------------------"
../third_party/iwyu_tool.py -p . -o quiet
# include-what-you-use has a "success code" of 2 for a legacy reason.
if [[ $? -ne 2 ]]; then
error_count=`expr $error_count + 1`
fi

# IWYU is too noisy right now and not always completely correct. It is going
# to be disabled for now but later will be re-enabled once it is wrangled better
# # Run Include What You Use
# #
# # TODO: When this or newer versions of iwyu_tool that carry over the exit codes
# # from the include-what-you-use command calls are more widely standard this can
# # be replaced with just a call to iwyu_tool instead.
# echo "Include What You Use"
# echo "--------------------"
# ../third_party/iwyu_tool.py -p . -o quiet
# # include-what-you-use has a "success code" of 2 for a legacy reason.
# if [[ $? -ne 2 ]]; then
# error_count=`expr $error_count + 1`
# fi


# Validate that clang-format would make no changes
Expand Down
23 changes: 1 addition & 22 deletions xml_converter/src/category_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ vector<string> Category::as_xml() const {
return xml_node_contents;
}

waypoint::Category Category::as_protobuf(string full_category_name, map<string, vector<Parseable*>>* parsed_pois) const {
full_category_name += this->name;
waypoint::Category Category::as_protobuf() const {
waypoint::Category proto_category;
if (this->default_visibility_is_set) {
proto_category.set_default_visibility(this->default_visibility);
Expand All @@ -113,26 +112,6 @@ waypoint::Category Category::as_protobuf(string full_category_name, map<string,
if (this->tooltip_description_is_set) {
proto_category.set_tip_description(this->tooltip_description);
}

auto pois = parsed_pois->find(full_category_name);

if (pois != parsed_pois->end()) {
for (unsigned int i = 0; i < pois->second.size(); i++) {
if (pois->second[i]->classname() == "POI") {
Icon* icon = dynamic_cast<Icon*>(pois->second[i]);
proto_category.add_icon()->MergeFrom(icon->as_protobuf());
}
else if (pois->second[i]->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(pois->second[i]);
proto_category.add_trail()->MergeFrom(trail->as_protobuf());
}
}
}

for (const auto& [key, val] : this->children) {
waypoint::Category proto_category_child = val.as_protobuf(full_category_name + ".", parsed_pois);
proto_category.add_children()->CopyFrom(proto_category_child);
}
return proto_category;
}

Expand Down
2 changes: 1 addition & 1 deletion xml_converter/src/category_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class Category : public Parseable {
virtual std::vector<std::string> as_xml() const;
virtual std::string classname();
bool init_xml_attribute(rapidxml::xml_attribute<>* attribute, std::vector<XMLError*>* errors, std::string base_dir = "");
waypoint::Category as_protobuf(std::string full_category_name, std::map<std::string, std::vector<Parseable*>>* parsed_pois) const;
waypoint::Category as_protobuf() const;
void parse_protobuf(waypoint::Category proto_category);
};
Loading

0 comments on commit 9da018f

Please sign in to comment.