1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
|
"""
C++17 code generator for a given Schema Salad definition.
Currently only supports emitting YAML from the C++ objects, not yet parsing
YAML into C++ objects.
The generated code requires the libyaml-cpp library & headers
To see an example of usage, look at schema_salad/tests/codegen/cwl.cpp
which can be combined with the CWL V1.0 schema as shown below::
schema-salad-tool --codegen cpp \
schema_salad/tests/test_schema/CommonWorkflowLanguage.yml \
> cwl_v1_0.h
g++ --std=c++20 -I. -lyaml-cpp schema_salad/tests/codegen/cwl.cpp -o cwl-v1_0-test
./cwl-v1_0-test
# g++ versions older than version 10 may need "--std=c++2a" instead of "--std=c++20"
"""
import os
import re
from typing import IO, Any, Optional, Union, cast
from . import _logger
from .codegen_base import CodeGenBase, TypeDef
from .exceptions import SchemaException
from .schema import shortname
from .utils import aslist
def q(s: str) -> str:
"""Put quotes around a string."""
return '"' + s + '"'
def replaceKeywords(s: str) -> str:
"""Rename keywords that are reserved in C++."""
if s in (
"class",
"enum",
"int",
"long",
"float",
"double",
"default",
"stdin",
"stdout",
"stderr",
"union",
):
s = s + "_"
return s
def safename(name: str) -> str:
"""Create a C++ safe name."""
classname = re.sub(r"[^a-zA-Z0-9]", "_", name)
return replaceKeywords(classname)
def safenamespacename(name: str) -> str:
"""Create a C++ safe name for namespaces."""
name = re.sub(r"^[a-zA-Z0-9]+://", "", name) # remove protocol
name = re.sub(r"//+", "", name) # remove duplicate slashes
name = re.sub(r"/$", "", name) # remove trailing slashes
name = re.sub(r"[^a-zA-Z0-9/]", "_", name)
name = re.sub(r"[/]", "::", name)
return name
# TODO: this should be somehow not really exists
def safename2(name: dict[str, str]) -> str:
"""Create a namespaced safename."""
return safenamespacename(name["namespace"]) + "::" + safename(name["classname"])
def split_name(s: str) -> tuple[str, str]:
"""Split url name into its components.
Splits names like https://xyz.xyz/blub#cwl/class
into its class path and non class path
"""
t = s.split("#")
if len(t) != 2:
raise ValueError("Expected field to be formatted as 'https://xyz.xyz/blub#cwl/class'.")
return (t[0], t[1])
def split_field(s: str) -> tuple[str, str, str]:
"""Split field into its components.
similar to split_name but for field names
"""
(namespace, field) = split_name(s)
t = field.split("/")
if len(t) != 2:
raise ValueError("Expected field to be formatted as 'https://xyz.xyz/blub#cwl/class'.")
return (namespace, t[0], t[1])
class ClassDefinition:
"""Prototype of a class."""
def __init__(self, name: str):
"""Initialize the class definition with a name."""
self.fullName = name
self.extends: list[dict[str, str]] = []
# List of types from parent classes that have been specialized
self.specializationTypes: list[str] = []
# this includes fields that are also inheritant
self.allfields: list[FieldDefinition] = []
self.fields: list[FieldDefinition] = []
self.abstract = False
(self.namespace, self.classname) = split_name(name)
self.namespace = safenamespacename(self.namespace)
self.classname = safename(self.classname)
def writeFwdDeclaration(self, target: IO[str], fullInd: str, ind: str) -> None:
"""Write forward declaration."""
target.write(f"{fullInd}namespace {self.namespace} {{ struct {self.classname}; }}\n")
def writeDefinition(
self, target: IO[Any], fullInd: str, ind: str, common_namespace: str
) -> None:
"""Write definition of the class."""
target.write(f"{fullInd}namespace {self.namespace} {{\n")
target.write(f"{fullInd}struct {self.classname}")
extends = list(map(safename2, self.extends))
override = ""
virtual = "virtual "
if len(self.extends) > 0:
target.write(f"\n{fullInd}{ind}: ")
target.write(f"\n{fullInd}{ind}, ".join(extends))
override = " override"
virtual = ""
target.write(" {\n")
for field in self.fields:
field.writeDefinition(target, fullInd + ind, ind, self.namespace)
if self.abstract:
target.write(f"{fullInd}{ind}virtual ~{self.classname}() = 0;\n")
else:
target.write(f"{fullInd}{ind}{virtual}~{self.classname}(){override} = default;\n")
target.write(
f"{fullInd}{ind}{virtual}auto toYaml([[maybe_unused]] "
f"{common_namespace}::store_config const& config) const -> YAML::Node{override};\n"
)
target.write(f"{fullInd}{ind}{virtual}void fromYaml(YAML::Node const& n){override};\n")
target.write(f"{fullInd}}};\n")
target.write(f"{fullInd}}}\n\n")
def writeImplDefinition(
self, target: IO[str], fullInd: str, ind: str, common_namespace: str
) -> None:
"""Write definition with implementation."""
extends = list(map(safename2, self.extends))
# Declaring default destructor
if self.abstract:
target.write(
f"{fullInd}inline {self.namespace}::{self.classname}::~{self.classname}() = default;\n"
)
# Write toYaml function
target.write(
f"{fullInd}inline auto {self.namespace}::{self.classname}::toYaml([[maybe_unused]] "
f"::{common_namespace}::store_config const& config) const -> YAML::Node {{\n"
f"{fullInd}{ind}using ::{common_namespace}::toYaml;\n"
f"{fullInd}{ind}auto n = YAML::Node{{}};\n"
f"{fullInd}{ind}if (config.generateTags) {{\n"
f'{fullInd}{ind}{ind}n.SetTag("{self.classname}");\n'
f"{fullInd}{ind}}}\n"
)
for e in extends:
target.write(f"{fullInd}{ind}n = mergeYaml(n, {e}::toYaml(config));\n")
for field in self.fields:
fieldname = safename(field.name)
target.write(f"{fullInd}{ind}{{\n")
target.write(f"{fullInd}{ind}{ind} auto member = toYaml(*{fieldname}, config);\n")
if field.typeDSL:
target.write(f"{fullInd}{ind}{ind} member = simplifyType(member, config);\n")
target.write(
f"{fullInd}{ind}{ind} member = convertListToMap(member, "
f"{q(field.mapSubject)}, {q(field.mapPredicate)}, config);\n"
)
target.write(f"{fullInd}{ind}{ind}addYamlField(n, {q(field.name)}, member);\n")
target.write(f"{fullInd}{ind}}}\n")
target.write(f"{fullInd}{ind}return n;\n{fullInd}}}\n")
# Write fromYaml function
functionname = f"{self.namespace}::{self.classname}::fromYaml"
target.write(
f"{fullInd}inline void {functionname}([[maybe_unused]] YAML::Node const& n) {{\n"
f"{fullInd}{ind}using ::{common_namespace}::fromYaml;\n"
)
for e in extends:
target.write(f"{fullInd}{ind}{e}::fromYaml(n);\n")
for field in self.fields:
fieldname = safename(field.name)
expandType = ""
if field.typeDSL:
expandType = "expandType"
target.write(
f"{fullInd}{ind}{{\n"
f"{fullInd}{ind}{ind}auto nodeAsList = convertMapToList("
f"n[{q(field.name)}], {q(field.mapSubject)}, {q(field.mapPredicate)});\n"
f"{fullInd}{ind}{ind}auto expandedNode = {expandType}(nodeAsList);\n"
f"{fullInd}{ind}{ind}fromYaml(expandedNode, *{fieldname});\n"
f"{fullInd}{ind}}}\n"
)
target.write(f"{fullInd}}}\n")
# write type detection function
if not self.abstract:
e = f"::{self.namespace}::{self.classname}"
target.write(
f"namespace {common_namespace} {{\n"
f"template <>\n"
f"struct DetectAndExtractFromYaml<{e}> {{\n"
f" auto operator()(YAML::Node const& n) const -> std::optional<{e}> {{\n"
f" if (!n.IsDefined()) return std::nullopt;\n"
f" if (!n.IsMap()) return std::nullopt;\n"
f" auto res = {e}{{}};\n\n"
)
for field in self.fields:
fieldname = safename(field.name)
target.write(
f" if constexpr (::{common_namespace}::IsConstant<"
f"decltype(res.{fieldname})::value_t>::value) try {{\n"
f" fromYaml(n[{q(field.name)}], *res.{fieldname});\n"
" fromYaml(n, res);\n"
" return res;\n"
f" }} catch(...) {{}}\n\n"
)
target.write(" return std::nullopt;\n }\n};\n}\n")
class FieldDefinition:
"""Prototype of a single field from a class definition."""
def __init__(
self,
name: str,
typeStr: str,
optional: bool,
mapSubject: str,
mapPredicate: str,
typeDSL: bool,
):
"""Initialize field definition.
Creates a new field with name, its type, optional and which field to use to convert
from list to map (or empty if it is not possible)
"""
self.name = name
self.typeStr = typeStr
self.optional = optional
self.mapSubject = mapSubject
self.mapPredicate = mapPredicate
self.typeDSL = typeDSL
def writeDefinition(self, target: IO[Any], fullInd: str, ind: str, namespace: str) -> None:
"""Write a C++ definition for the class field."""
name = safename(self.name)
typeStr = self.typeStr.replace(namespace + "::", "")
target.write(f"{fullInd}heap_object<{typeStr}> {name};\n")
class MapDefinition:
"""Prototype of a map."""
def __init__(self, name: str, values: list[str]):
"""Initialize union definition with a name and possible values."""
self.values = values
(self.namespace, self.classname) = split_name(name)
self.namespace = safenamespacename(self.namespace)
self.classname = safename(self.classname)
def _remove_namespace(self, typeStr: str) -> str:
return typeStr.replace(f"{self.namespace}::", "")
def writeFwdDeclaration(self, target: IO[str], fullInd: str, ind: str) -> None:
"""Write forward declaration."""
target.write(f"{fullInd}namespace {self.namespace} {{ struct {self.classname}; }}\n")
def writeDefinition(self, target: IO[str], ind: str, common_namespace: str) -> None:
"""Write map definition to output."""
target.write(f"namespace {self.namespace} {{\n")
if len(self.values) == 1:
valueType = self._remove_namespace(self.values[0])
else:
valueType = f"std::variant<{', '.join(self._remove_namespace(v) for v in self.values)}>"
target.write(f"struct {self.classname} {{\n")
target.write(f"{ind}heap_object<std::map<std::string, {valueType}>> value;\n")
target.write(
f"{ind}auto toYaml([[maybe_unused]] "
f"::{common_namespace}::store_config const& config) const -> YAML::Node;\n"
)
target.write(f"{ind}void fromYaml(YAML::Node const& n);\n")
target.write("};\n")
target.write("}\n\n")
def writeImplDefinition(
self, target: IO[str], fullInd: str, ind: str, common_namespace: str
) -> None:
"""Write definition with implementation."""
# Write toYaml function
functionname = f"{self.namespace}::{self.classname}::toYaml"
target.write(
f"{fullInd}inline auto {functionname}([[maybe_unused]] "
f"::{common_namespace}::store_config const& config) const -> YAML::Node {{\n"
f"{fullInd}{ind}using ::{common_namespace}::toYaml;\n"
f"{fullInd}{ind}return toYaml(*value, config);\n"
f"{fullInd}}}\n"
)
# Write fromYaml function
functionname = f"{self.namespace}::{self.classname}::fromYaml"
target.write(
f"{fullInd}inline void {functionname}([[maybe_unused]] YAML::Node const& n) {{\n"
f"{fullInd}{ind}using ::{common_namespace}::fromYaml;\n"
f"{fullInd}{ind}fromYaml(n, *value);\n"
f"{fullInd}}}\n"
)
class UnionDefinition:
"""Prototype of a union."""
def __init__(self, name: str, types: list[str]):
"""Initialize union definition with a name and possible types."""
(self.namespace, self.classname) = split_name(name)
self.namespace = safenamespacename(self.namespace)
self.classname = safename(self.classname)
self.types = (
self._remove_namespace(types[0])
if len(types) == 1
else f"std::variant<{', '.join(self._remove_namespace(t) for t in types)}>"
)
def _remove_namespace(self, typeStr: str) -> str:
return typeStr.replace(f"{self.namespace}::", "")
def writeFwdDeclaration(self, target: IO[str], fullInd: str, ind: str) -> None:
"""Write forward declaration."""
target.write(f"{fullInd}namespace {self.namespace} {{ struct {self.classname}; }}\n")
def writeDefinition(self, target: IO[str], ind: str, common_namespace: str) -> None:
"""Write union definition to output."""
target.write(f"namespace {self.namespace} {{\n")
target.write(f"struct {self.classname} {{\n")
target.write(f"{ind}{self.types} *value = nullptr;\n")
target.write(f"{ind}{self.classname}();\n")
target.write(f"{ind}~{self.classname}();\n")
target.write(
f"{ind}auto toYaml([[maybe_unused]] "
f"::{common_namespace}::store_config const& config) const -> YAML::Node;\n"
)
target.write(f"{ind}void fromYaml(YAML::Node const& n);\n")
target.write("};\n")
target.write("}\n\n")
def writeImplDefinition(
self, target: IO[str], fullInd: str, ind: str, common_namespace: str
) -> None:
"""Write definition with implementation."""
# Write constructor
functionname = f"{self.namespace}::{self.classname}::{self.classname}"
target.write(
f"{fullInd}{functionname}() {{\n"
f"{fullInd}{ind}value = new {self.types}();\n"
f"{fullInd}}}\n"
)
# Write destructor
functionname = f"{self.namespace}::{self.classname}::~{self.classname}"
target.write(
f"{fullInd}{functionname}() {{\n"
f"{fullInd}{ind}if (value != nullptr) {{\n"
f"{fullInd}{ind}{ind}delete value;\n"
f"{fullInd}{ind}{ind}value = nullptr;\n"
f"{fullInd}{ind}}}\n"
f"{fullInd}}}\n"
)
# Write toYaml function
functionname = f"{self.namespace}::{self.classname}::toYaml"
target.write(
f"{fullInd}inline auto {functionname}([[maybe_unused]] "
f"::{common_namespace}::store_config const& config) const -> YAML::Node {{\n"
f"{fullInd}{ind}using ::{common_namespace}::toYaml;\n"
f"{fullInd}{ind}return toYaml(*value, config);\n"
f"{fullInd}}}\n"
)
# Write fromYaml function
functionname = f"{self.namespace}::{self.classname}::fromYaml"
target.write(
f"{fullInd}inline void {functionname}([[maybe_unused]] YAML::Node const& n) {{\n"
f"{fullInd}{ind}using ::{common_namespace}::fromYaml;\n"
f"{fullInd}{ind}fromYaml(n, *value);\n"
f"{fullInd}}}\n"
)
class EnumDefinition:
"""Prototype of a enum."""
def __init__(self, name: str, values: list[str]):
"""Initialize enum definition with a name and possible values."""
self.name = name
self.values = values
(self.namespace, self.classname) = split_name(name)
self.namespace = safenamespacename(self.namespace)
self.classname = safename(self.classname)
def writeDefinition(self, target: IO[str], ind: str, common_namespace: str) -> None:
"""Write enum definition to output."""
namespace = ""
if len(self.name.split("#")) == 2:
(namespace, classname) = split_name(self.name)
namespace = safenamespacename(namespace)
classname = safename(classname)
name = namespace + "::" + classname
else:
name = safename(self.name)
classname = name
if len(namespace) > 0:
target.write(f"namespace {namespace} {{\n")
target.write(f"enum class {classname} : unsigned int {{\n{ind}")
target.write(f",\n{ind}".join(map(safename, self.values)))
target.write("\n};\n")
target.write(f"inline auto to_string({classname} v) {{\n")
target.write(f"{ind}static auto m = std::vector<std::string_view> {{\n")
target.write(f'{ind} "')
target.write(f'",\n{ind} "'.join(self.values))
target.write(f'"\n{ind}}};\n')
target.write(f"{ind}using U = std::underlying_type_t<{name}>;\n")
target.write(f"{ind}return m.at(static_cast<U>(v));\n}}\n")
if len(namespace) > 0:
target.write("}\n")
target.write(f"inline void to_enum(std::string_view v, {name}& out) {{\n")
target.write(f"{ind}static auto m = std::map<std::string, {name}, std::less<>> {{\n")
for v in self.values:
target.write(f"""{ind}{ind}{{{q(v)}, {name}::{safename(v)}}},\n""")
target.write(f"{ind}}};\n{ind}auto iter = m.find(v);\n")
target.write(f"{ind}if (iter == m.end()) throw bool{{}};\n")
target.write(f"{ind}out = iter->second;\n}}\n")
# Write toYaml function
target.write(f"namespace {common_namespace} {{\n")
target.write(
f"inline auto toYaml({name} v, [[maybe_unused]] "
f"::{common_namespace}::store_config const& config) {{\n"
)
target.write(f"{ind}auto n = YAML::Node{{std::string{{to_string(v)}}}};\n")
target.write(f'{ind}if (config.generateTags) n.SetTag("{name}");\n')
target.write(f"{ind}return n;\n}}\n")
# Write fromYaml function
target.write(f"inline void fromYaml(YAML::Node n, {name}& out) {{\n")
target.write(f"{ind}to_enum(n.as<std::string>(), out);\n}}\n")
if len(self.values):
target.write(f"template <> struct IsConstant<{name}> : std::true_type {{}};\n")
target.write("}\n")
target.write("\n")
# !TODO way to many functions, most of these shouldn't exists
def isPrimitiveType(v: Any) -> bool:
"""Check if v is a primitive type."""
if not isinstance(v, str):
return False
return v in ["null", "boolean", "int", "long", "float", "double", "string"]
def hasFieldValue(e: Any, f: str, v: Any) -> bool:
"""Check if e has a field f value."""
if not isinstance(e, dict):
return False
if f not in e:
return False
return bool(e[f] in [v, f"https://w3id.org/cwl/salad#{v}"])
def isRecordSchema(v: Any) -> bool:
"""Check if v is of type record schema."""
return hasFieldValue(v, "type", "record")
def isEnumSchema(v: Any) -> bool:
"""Check if v is of type enum schema."""
if not hasFieldValue(v, "type", "enum"):
return False
if "symbols" not in v:
return False
if not isinstance(v["symbols"], list):
return False
return True
def isArray(v: Any) -> bool:
"""Check if v is of type array."""
if not isinstance(v, list):
return False
for i in v:
if not pred(i):
return False
return True
def pred(i: Any) -> bool:
"""Check if v is any of the simple types."""
return (
isPrimitiveType(i)
or isRecordSchema(i)
or isEnumSchema(i)
or isArraySchema(i)
or isMapSchema(i)
or isUnionSchema(i)
or isinstance(i, str)
)
def isArraySchema(v: Any) -> bool:
"""Check if v is of type array schema."""
if not hasFieldValue(v, "type", "array"):
return False
if "items" not in v:
return False
if not isinstance(v["items"], list):
return False
for i in v["items"]:
if not (pred(i) or isArray(i)):
return False
return True
def isMapSchema(v: Any) -> bool:
"""Check if v is of type map schema."""
if not hasFieldValue(v, "type", "map"):
return False
if "values" not in v:
return False
if not isinstance(v["values"], list):
return False
for i in v["values"]:
if not (pred(i) or isArray(i)):
return False
return True
def isUnionSchema(v: Any) -> bool:
"""Check if v is of type union schema."""
return hasFieldValue(v, "type", "union")
class CppCodeGen(CodeGenBase):
"""Generation of C++ code for a given Schema Salad definition."""
def __init__(
self,
base: str,
target: IO[str],
examples: Optional[str],
package: str,
copyright: Optional[str],
spdx_copyright_text: Optional[list[str]],
spdx_license_identifier: Optional[str],
) -> None:
"""Initialize the C++ code generator."""
super().__init__()
self.base_uri = base
self.target = target
self.examples = examples
self.package = package
self.copyright = copyright
self.spdx_copyright_text = spdx_copyright_text
self.spdx_license_identifier = spdx_license_identifier
self.classDefinitions: dict[str, ClassDefinition] = {}
self.enumDefinitions: dict[str, EnumDefinition] = {}
self.mapDefinitions: dict[str, MapDefinition] = {}
self.unionDefinitions: dict[str, UnionDefinition] = {}
self.documentRootTypes: list[ClassDefinition] = []
def convertTypeToCpp(self, type_declaration: Union[list[Any], dict[str, Any], str]) -> str:
"""Convert a Schema Salad type to a C++ type."""
if not isinstance(type_declaration, list):
return self.convertTypeToCpp([type_declaration])
if len(type_declaration) == 1:
if type_declaration[0] in ("null", "https://w3id.org/cwl/salad#null"):
return "std::monostate"
elif type_declaration[0] in (
"string",
"http://www.w3.org/2001/XMLSchema#string",
):
return "std::string"
elif type_declaration[0] in ("int", "http://www.w3.org/2001/XMLSchema#int"):
return "int32_t"
elif type_declaration[0] in (
"long",
"http://www.w3.org/2001/XMLSchema#long",
):
return "int64_t"
elif type_declaration[0] in (
"float",
"http://www.w3.org/2001/XMLSchema#float",
):
return "float"
elif type_declaration[0] in (
"double",
"http://www.w3.org/2001/XMLSchema#double",
):
return "double"
elif type_declaration[0] in (
"boolean",
"http://www.w3.org/2001/XMLSchema#boolean",
):
return "bool"
elif type_declaration[0] == "https://w3id.org/cwl/salad#Any":
return "std::any"
elif type_declaration[0] == "https://w3id.org/cwl/cwl#Expression":
return "cwl_expression_string"
elif type_declaration[0] in (
"PrimitiveType",
"https://w3id.org/cwl/salad#PrimitiveType",
):
return "std::variant<bool, int32_t, int64_t, float, double, std::string>"
elif isinstance(type_declaration[0], dict):
if "type" in type_declaration[0] and type_declaration[0]["type"] in (
"enum",
"https://w3id.org/cwl/salad#enum",
):
name = type_declaration[0]["name"]
if name not in self.enumDefinitions:
self.enumDefinitions[name] = EnumDefinition(
type_declaration[0]["name"],
list(map(shortname, type_declaration[0]["symbols"])),
)
if len(name.split("#")) != 2:
return safename(name)
(namespace, classname) = name.split("#")
return safenamespacename(namespace) + "::" + safename(classname)
elif "type" in type_declaration[0] and type_declaration[0]["type"] in (
"array",
"https://w3id.org/cwl/salad#array",
):
items = type_declaration[0]["items"]
if isinstance(items, list):
ts = [self.convertTypeToCpp(i) for i in items]
name = ", ".join(ts)
return f"std::vector<std::variant<{name}>>"
else:
i = self.convertTypeToCpp(items)
return f"std::vector<{i}>"
elif "type" in type_declaration[0] and type_declaration[0]["type"] in (
"map",
"https://w3id.org/cwl/salad#map",
):
values = type_declaration[0]["values"]
if isinstance(values, list):
ts = [self.convertTypeToCpp(i) for i in values]
name = ", ".join(ts)
return f"std::map<std::string, std::variant<{name}>>"
else:
i = self.convertTypeToCpp(values)
return f"std::map<std::string, {i}>"
elif "type" in type_declaration[0] and type_declaration[0]["type"] in (
"record",
"https://w3id.org/cwl/salad#record",
):
n = type_declaration[0]["name"]
(namespace, classname) = split_name(n)
return safenamespacename(namespace) + "::" + safename(classname)
n = type_declaration[0]["type"]
(namespace, classname) = split_name(n)
return safenamespacename(namespace) + "::" + safename(classname)
if len(type_declaration[0].split("#")) != 2:
_logger.debug(f"// something weird2 about {type_declaration[0]}")
return cast(str, type_declaration[0])
(namespace, classname) = split_name(type_declaration[0])
return safenamespacename(namespace) + "::" + safename(classname)
type_declaration = list(map(self.convertTypeToCpp, type_declaration))
type_declaration = ", ".join(type_declaration)
return f"std::variant<{type_declaration}>"
def epilogue(self, root_loader: Optional[TypeDef]) -> None:
"""Trigger to generate the epilouge code."""
# find common namespace
common_namespace = os.path.commonprefix(
list(
map(
lambda x: x.namespace,
list(self.classDefinitions.values()) + list(self.enumDefinitions.values()),
)
)
)
common_namespace = re.sub("(::)+$", "", common_namespace)
"""Generate final part of our cpp file."""
if self.spdx_copyright_text:
for text in self.spdx_copyright_text:
self.target.write(f"""// SPDX-FileCopyrightText: {text}\n""")
if self.spdx_license_identifier:
self.target.write(f"""// SPDX-License-Identifier: {self.spdx_license_identifier}\n""")
self.target.write("#pragma once\n\n")
self.target.write(
"""/* This file was generated using schema-salad code generator.
*
* The embedded document is subject to the license of the original schema.
"""
)
if self.copyright:
self.target.write("* The original schema is {self.copyright}.\n")
self.target.write("*/\n\n")
self.target.write(
"""#include <any>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include <yaml-cpp/yaml.h>
"""
)
self.target.write(f"namespace {common_namespace} {{\n")
self.target.write(
"""
struct store_config {
bool simplifyTypes = true;
bool transformListsToMaps = true;
bool generateTags = false;
};
inline auto simplifyType(YAML::Node type, store_config const& config) -> YAML::Node {
if (!config.simplifyTypes) return type;
auto is_optional = [](YAML::Node const & node) {
return node.IsSequence() && node.size() == 2u && node[0].Scalar() == "null";
};
auto is_array = [](YAML::Node const & node) {
return node.IsMap() && node["type"].Scalar() == "array" && node["items"].IsScalar();
};
// 1. Collapsing optional scalar types into one option
if (is_optional(type) && type[1].IsScalar()) {
type = type[1].as<std::string>() + "?";
}
// 2. Collapsing array types into one option
if (is_array(type)) {
type = type["items"].as<std::string>() + "[]";
}
// 3. Collapsing optional array types into one option
if (is_optional(type) && is_array(type[1])) {
type = type[1]["items"].as<std::string>() + "[]?";
}
return type;
}
inline auto expandType(YAML::Node type) -> YAML::Node {
auto ends_with = [](std::string str, std::string suffix) {
if (str.size() < suffix.size()) return false;
auto str_suffix = str.substr(str.size()-suffix.size(), suffix.size());
return str_suffix == suffix;
};
// 0. If not a scalar type, nothing to do
if (!type.IsDefined() || !type.IsScalar()) {
return type;
}
auto str = type.as<std::string>();
// 1. Check if optional array type and expand
if (ends_with(str, "[]?")) {
auto result = YAML::Node{};
result.push_back(YAML::Node{"null"});
auto array = YAML::Node{};
array["type"] = "array";
array["items"] = expandType(YAML::Node(str.substr(0, str.size()-3)));
result.push_back(array);
return result;
}
// 2. Expand array
if (ends_with(str, "[]")) {
auto array = YAML::Node{};
array["type"] = "array";
array["items"] = expandType(YAML::Node(str.substr(0, str.size()-2)));
return array;
}
// 3. Expand optional scalar type
if (ends_with(str, "?")) {
auto result = YAML::Node{};
result.push_back(YAML::Node{"null"});
result.push_back(expandType(YAML::Node(str.substr(0, str.size()-1))));
return result;
}
return type;
}
inline auto mergeYaml(YAML::Node n1, YAML::Node n2) {
for (auto const& e : n2) {
n1[e.first.as<std::string>()] = e.second;
}
return n1;
}
// declaring toYaml
inline auto toYaml(bool v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(float v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(double v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(char v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(int8_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(uint8_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(int16_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(uint16_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(int32_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(uint32_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(int64_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(uint64_t v, [[maybe_unused]] store_config const&) { return YAML::Node{v}; }
inline auto toYaml(std::monostate const&, [[maybe_unused]] store_config const&) {
return YAML::Node(YAML::NodeType::Undefined);
}
inline auto toYaml(std::string const& v, [[maybe_unused]] store_config const&) {
return YAML::Node{v};
}
template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a, [[maybe_unused]] store_config const& config) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v, config);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a, config);
}
return toYaml(std::monostate{}, config);
}
inline auto toYaml(std::any const& a, [[maybe_unused]] store_config const& config) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a, config);
}
// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
}
inline void fromYaml(YAML::Node const& n, float& v) {
v = n.as<float>();
}
inline void fromYaml(YAML::Node const& n, double& v) {
v = n.as<double>();
}
inline void fromYaml(YAML::Node const& n, int32_t& v) {
v = n.as<int32_t>();
}
inline void fromYaml(YAML::Node const& n, int64_t& v) {
v = n.as<int64_t>();
}
inline void fromYaml(YAML::Node const& n, std::string& v) {
v = n.as<std::string>();
}
inline void fromYaml(YAML::Node const&, std::any&) {
}
inline void fromYaml(YAML::Node const&, std::monostate&) {
}
inline void addYamlField(YAML::Node& node, std::string const& key, YAML::Node value) {
if (value.IsDefined()) {
node[key] = value;
}
}
inline auto convertListToMap(YAML::Node list, std::string const& mapSubject,
std::string const& mapPredicate, store_config const& config) {
if (!config.transformListsToMaps) return list;
if (mapSubject.empty()) return list;
if (list.size() == 0) return list;
auto map = YAML::Node{};
for (YAML::Node n : list) {
auto key = n[mapSubject].as<std::string>();
if (mapPredicate.empty() || n[mapPredicate].IsMap() || n.size() > 2) {
n.remove(mapSubject);
map[key] = n;
} else {
map[key] = n[mapPredicate];
}
}
return map;
}
inline auto convertMapToList(YAML::Node map, std::string const& mapSubject,
std::string const& mapPredicate) {
if (mapSubject.empty()) return map;
if (!map.IsDefined()) return map;
if (!map.IsMap()) return map;
auto list = YAML::Node{};
for (auto n : map) {
if (mapPredicate.empty() || n.second.IsMap()) {
n.second[mapSubject] = n.first;
list.push_back(n.second);
} else {
auto n2 = YAML::Node{};
n2[mapSubject] = n.first;
n2[mapPredicate] = n.second;
list.push_back(n2);
}
}
return list;
}
template <typename T> struct IsConstant : std::false_type {};
// fwd declaring toYaml
template <typename T>
auto toYaml(std::vector<T> const& v, [[maybe_unused]] store_config const& config) -> YAML::Node;
template <typename T>
auto toYaml(std::map<std::string, T> const& v, [[maybe_unused]] store_config const& config) -> YAML::Node;
template <typename T>
auto toYaml(T const& t, [[maybe_unused]] store_config const& config) -> YAML::Node;
template <typename ...Args>
auto toYaml(std::variant<Args...> const& t, [[maybe_unused]] store_config const& config) -> YAML::Node;
// fwd declaring fromYaml
template <typename T>
void fromYaml(YAML::Node const& n, std::vector<T>& v);
template <typename T>
void fromYaml(YAML::Node const& n, std::map<std::string, T>& v);
template <typename T>
void fromYaml(YAML::Node const& n, T& t);
template <typename ...Args>
void fromYaml(YAML::Node const& n, std::variant<Args...>& t);
template <typename T>
struct DetectAndExtractFromYaml {
auto operator()(YAML::Node const&) const -> std::optional<T> {
return std::nullopt;
}
};
// special cwl expression string
struct cwl_expression_string {
std::string s;
auto toYaml([[maybe_unused]] store_config const& config) const {
auto n = YAML::Node{s};
if (config.generateTags) {
n.SetTag("Expression");
}
return n;
}
void fromYaml(YAML::Node const& n) {
s = n.as<std::string>();
}
};
template <>
struct DetectAndExtractFromYaml<std::monostate> {
auto operator()(YAML::Node const& n) const -> std::optional<std::monostate> {
if (!n.IsDefined()) return std::monostate{};
return std::nullopt;
}
};
template <typename S>
struct DetectAndExtractFromYaml_implScalar {
auto operator()(YAML::Node const& n) const -> std::optional<S> {
try {
if (n.IsScalar()) return n.as<S>();
} catch(...) {}
return std::nullopt;
}
};
template <> struct DetectAndExtractFromYaml<bool> : DetectAndExtractFromYaml_implScalar<bool>{};
template <> struct DetectAndExtractFromYaml<float> : DetectAndExtractFromYaml_implScalar<float>{};
template <> struct DetectAndExtractFromYaml<double> : DetectAndExtractFromYaml_implScalar<double>{};
template <> struct DetectAndExtractFromYaml<int32_t> : DetectAndExtractFromYaml_implScalar<int32_t>{};
template <> struct DetectAndExtractFromYaml<int64_t> : DetectAndExtractFromYaml_implScalar<int64_t>{};
template <> struct DetectAndExtractFromYaml<std::string> : DetectAndExtractFromYaml_implScalar<std::string>{};
template <typename T>
struct DetectAndExtractFromYaml<std::vector<T>> {
auto operator()(YAML::Node const& n) const -> std::optional<std::vector<T>> {
if (!n.IsDefined()) return std::nullopt;
if (!n.IsSequence()) return std::nullopt;
auto res = std::vector<T>{};
fromYaml(n, res);
return res;
}
};
template <typename T>
struct DetectAndExtractFromYaml<std::map<std::string, T>> {
auto operator()(YAML::Node const& n) const -> std::optional<std::map<std::string, T>> {
if (!n.IsDefined()) return std::nullopt;
if (!n.IsMap()) return std::nullopt;
auto res = std::map<std::string, T>{};
fromYaml(n, res);
return res;
}
};
template <typename T>
class heap_object {
std::unique_ptr<T> data = std::make_unique<T>();
public:
using value_t = T;
heap_object() noexcept(false) = default;
heap_object(heap_object const& oth) {
*data = *oth;
}
heap_object(heap_object&& oth) noexcept(noexcept(*data = std::move(*oth))) {
*data = std::move(*oth);
}
template <typename T2>
heap_object(T2 const& oth) {
*data = oth;
}
template <typename T2>
heap_object(T2&& oth) noexcept(noexcept(*data = std::forward<T2>(oth))) {
*data = std::forward<T2>(oth);
}
~heap_object();
auto operator=(heap_object const& oth) -> heap_object& {
*data = *oth;
return *this;
}
auto operator=(heap_object&& oth) noexcept(noexcept(*data = std::move(*oth))) -> heap_object& {
*data = std::move(*oth);
return *this;
}
template <typename T2>
auto operator=(T2 const& oth) -> heap_object& {
*data = oth;
return *this;
}
template <typename T2>
auto operator=(T2&& oth) noexcept(noexcept(*data = std::forward<T2>(oth))) -> heap_object& {
*data = std::forward<T2>(oth);
return *this;
}
auto operator->() noexcept(true) -> T* {
return data.get();
}
auto operator->() const noexcept(true) -> T const* {
return data.get();
}
auto operator*() noexcept(true) -> T& {
return *data;
}
auto operator*() const noexcept(true) -> T const& {
return *data;
}
};
}
"""
)
# main body, printing fwd declaration, class definitions, and then implementations
for key in self.classDefinitions:
self.classDefinitions[key].writeFwdDeclaration(self.target, "", " ")
for key in self.mapDefinitions:
self.mapDefinitions[key].writeFwdDeclaration(self.target, "", " ")
for key in self.unionDefinitions:
self.unionDefinitions[key].writeFwdDeclaration(self.target, "", " ")
# remove parent classes, that are specialized/templated versions
for key in self.classDefinitions:
if len(self.classDefinitions[key].specializationTypes) > 0:
self.classDefinitions[key].extends = []
# remove fields that are available in a parent class
for key in self.classDefinitions:
for field in self.classDefinitions[key].allfields:
found = False
for parent_key in self.classDefinitions[key].extends:
fullKey = parent_key["namespace"] + "#" + parent_key["classname"]
for f in self.classDefinitions[fullKey].allfields:
if f.name == field.name:
found = True
break
if found:
break
if not found:
self.classDefinitions[key].fields.append(field) # noqa: B038
# write definitions
for key in self.enumDefinitions:
self.enumDefinitions[key].writeDefinition(self.target, " ", common_namespace)
for key in self.classDefinitions:
self.classDefinitions[key].writeDefinition(self.target, "", " ", common_namespace)
for key in self.mapDefinitions:
self.mapDefinitions[key].writeDefinition(self.target, " ", common_namespace)
for key in self.unionDefinitions:
self.unionDefinitions[key].writeDefinition(self.target, " ", common_namespace)
# CPP23: std::unique_ptr in heap_object is constexpr.
# Hence, the compiler will try to instantiate the destructor on definition.
# If the destructor was defined inside heap_object, other classes would only
# be forward declared at this point.
# This results in an error, because the destructor cannot be generated for
# incomplete types.
# Therefore, the destructor is defined here, after all classes have been defined.
self.target.write(
f"namespace {common_namespace} {{\n"
f"template <typename T> heap_object<T>::~heap_object() = default;\n}}\n\n"
)
# write implementations
for key in self.classDefinitions:
self.classDefinitions[key].writeImplDefinition(
self.target, "", " ", common_namespace
)
for key in self.mapDefinitions:
self.mapDefinitions[key].writeImplDefinition(self.target, "", " ", common_namespace)
for key in self.unionDefinitions:
self.unionDefinitions[key].writeImplDefinition(
self.target, "", " ", common_namespace
)
self.target.write(f"namespace {common_namespace} {{\n")
self.target.write(
"""
template <typename T>
auto toYaml(std::vector<T> const& v, [[maybe_unused]] store_config const& config) -> YAML::Node {
auto n = YAML::Node(YAML::NodeType::Sequence);
for (auto const& e : v) {
n.push_back(toYaml(e, config));
}
return n;
}
template <typename T>
auto toYaml(std::map<std::string, T> const& v, [[maybe_unused]] store_config const& config) -> YAML::Node {
auto n = YAML::Node(YAML::NodeType::Map);
for (auto const& [key, value] : v) {
n[key] = toYaml(value, config);
}
return n;
}
template <typename T>
auto toYaml(T const& t, [[maybe_unused]] store_config const& config) -> YAML::Node {
if constexpr (std::is_enum_v<T>) {
return toYaml(t, config);
} else {
return t.toYaml(config);
}
}
template <typename ...Args>
auto toYaml(std::variant<Args...> const& t, store_config const& config) -> YAML::Node {
return std::visit([config](auto const& e) {
return toYaml(e, config);
}, t);
}
template <typename T>
void fromYaml(YAML::Node const& n, std::vector<T>& v){
if (!n.IsSequence()) return;
for (auto e : n) {
v.emplace_back();
fromYaml(e, v.back());
}
}
template <typename T>
void fromYaml(YAML::Node const& n, std::map<std::string, T>& v){
if (!n.IsMap()) return;
for (auto e : n) {
auto key = e.first.as<std::string>();
fromYaml(e.second, v[key]);
}
}
template <typename T>
void fromYaml(YAML::Node const& n, T& t){
if constexpr (std::is_enum_v<T>) {
fromYaml(n, t);
} else {
t.fromYaml(n);
}
}
template <typename SomeVariant, typename Head, typename ...Args>
bool detectAndExtractFromYaml(YAML::Node const& n, SomeVariant& v, Head* = nullptr) {
auto r = DetectAndExtractFromYaml<Head>{}(n);
if (r) {
v = *r;
return true;
}
if constexpr (sizeof...(Args) > 0) {
return detectAndExtractFromYaml<SomeVariant, Args...>(n, v);
}
return false;
}
template <typename SomeVariant, typename Head, typename Tail>
bool detectAndExtractFromYaml(YAML::Node const& n, std::variant<std::monostate, Tail>& v, Head* = nullptr) {
auto r = DetectAndExtractFromYaml<Head>{}(n);
if (r) {
v = *r;
return true;
}
auto t = Tail{};
fromYaml(n, t);
v = t;
return true;
}
template <typename ...Args>
void fromYaml(YAML::Node const& n, std::variant<Args...>& v){
bool found = detectAndExtractFromYaml<std::variant<Args...>, Args...>(n, v);
if (!found) throw std::runtime_error{"didn't find any overload"};
}
"""
)
rootTypes = []
for cd in self.documentRootTypes:
rootTypes.append(f"{cd.namespace}::{cd.classname}")
documentRootType = ", ".join(rootTypes)
self.target.write(f"using DocumentRootType = std::variant<{documentRootType}>;")
self.target.write(
"""
auto load_document_from_yaml(YAML::Node n) -> DocumentRootType {
DocumentRootType root;
fromYaml(n, root);
return root;
}
auto load_document_from_string(std::string document) -> DocumentRootType {
return load_document_from_yaml(YAML::Load(document));
}
auto load_document(std::filesystem::path path) -> DocumentRootType {
return load_document_from_yaml(YAML::LoadFile(path.string()));
}
void store_document(DocumentRootType const& root, std::ostream& ostream, store_config config={}) {
auto y = toYaml(root, config);
YAML::Emitter out;
out << y;
ostream << out.c_str() << std::endl;
}
void store_document(DocumentRootType const& root, std::filesystem::path const& path, store_config config={}) {
auto ofs = std::ofstream{path};
store_document(root, ofs, config);
}
auto store_document_as_string(DocumentRootType const& root, store_config config={}) -> std::string {
auto ss = std::stringstream{};
store_document(root, ss, config);
return ss.str();
}
}"""
)
def parseRecordField(self, field: dict[str, Any]) -> FieldDefinition:
"""Parse a record field."""
(namespace, classname, fieldname) = split_field(field["name"])
mapSubject = ""
mapPredicate = ""
typeDSL = False
if "jsonldPredicate" in field:
if "mapSubject" in field["jsonldPredicate"]:
mapSubject = field["jsonldPredicate"]["mapSubject"]
if "mapPredicate" in field["jsonldPredicate"]:
mapPredicate = field["jsonldPredicate"]["mapPredicate"]
if "typeDSL" in field["jsonldPredicate"]:
typeDSL = field["jsonldPredicate"]["typeDSL"]
if isinstance(field["type"], dict):
if field["type"]["type"] == "enum":
fieldtype = "Enum"
else:
fieldtype = self.convertTypeToCpp(field["type"])
else:
fieldtype = self.convertTypeToCpp(field["type"])
return FieldDefinition(
name=fieldname,
typeStr=fieldtype,
optional=False,
mapSubject=mapSubject,
mapPredicate=mapPredicate,
typeDSL=typeDSL,
)
def parseRecordSchema(self, stype: dict[str, Any]) -> None:
"""Parse a record schema."""
cd = ClassDefinition(name=stype["name"])
cd.abstract = stype.get("abstract", False)
if "extends" in stype:
for ex in aslist(stype["extends"]):
(base_namespace, base_classname) = split_name(ex)
ext = {"namespace": base_namespace, "classname": base_classname}
cd.extends.append(ext)
if "specialize" in stype:
for e in aslist(stype["specialize"]):
cd.specializationTypes.append(e["specializeFrom"])
if "fields" in stype:
for field in stype["fields"]:
cd.allfields.append(self.parseRecordField(field))
self.classDefinitions[stype["name"]] = cd
if stype.get("documentRoot", False):
self.documentRootTypes.append(cd)
def parseMapSchema(self, stype: dict[str, Any]) -> str:
"""Parse a map schema."""
name = cast(str, stype["name"])
if name not in self.mapDefinitions:
self.mapDefinitions[name] = MapDefinition(
name, list(map(self.convertTypeToCpp, stype["values"]))
)
return name
def parseUnionSchema(self, stype: dict[str, Any]) -> str:
"""Parse a union schema."""
name = cast(str, stype["name"])
if name not in self.unionDefinitions:
self.unionDefinitions[name] = UnionDefinition(
name, list(map(self.convertTypeToCpp, stype["names"]))
)
return name
def parseEnum(self, stype: dict[str, Any]) -> str:
"""Parse a schema salad enum."""
name = cast(str, stype["name"])
if name not in self.enumDefinitions:
self.enumDefinitions[name] = EnumDefinition(
name, list(map(shortname, stype["symbols"]))
)
return name
def parse(self, items: list[dict[str, Any]]) -> None:
"""Parse sechema salad items.
This function is being called from the outside and drives
the whole code generation.
"""
for stype in items:
if "type" in stype and stype["type"] == "documentation":
continue
if not (pred(stype) or isArray(stype)):
raise SchemaException("not a valid SaladRecordField")
# parsing a record
if isRecordSchema(stype):
self.parseRecordSchema(stype)
elif isMapSchema(stype):
self.parseMapSchema(stype)
elif isUnionSchema(stype):
self.parseUnionSchema(stype)
elif isEnumSchema(stype):
self.parseEnum(stype)
else:
_logger.error(f"not parsed{stype}")
self.epilogue(None)
self.target.close()
|