Skip to content

Commit

Permalink
sarif: don't generate intermediates when deserializing optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Aug 1, 2024
1 parent c52d1ec commit c7d5089
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 825 deletions.
23 changes: 17 additions & 6 deletions sarif/scripts/sarif-headergen/src/sarif_headergen/__cli__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ def _get_type_name(type: FieldType):
return f"::gap::sarif::{name}"
return name


def sanitize(name: str) -> str:
return name.translate(str.maketrans({
"-": "_",
".": "_",
" ": "_",
}))


def to_snake_case(name: str) -> str:
tr = str.maketrans({
letter: f"_{letter.lower()}" for letter in "QWERTYUIOPASDFGHJKLZXCVBNM"
})
return name.translate(tr)


def to_pascal_case(name: str) -> str:
return name[0].upper() + name[1:]


def get_default_value(field: Field) -> str:
def stringify(val):
type = field.type
Expand Down Expand Up @@ -74,6 +78,7 @@ def stringify(val):
else:
return stringify(field.default)


def print_struct(elem: Struct, file):
elem_name = get_type_name(elem)

Expand Down Expand Up @@ -138,12 +143,7 @@ def print_from_json(elem: Struct, file):
is_first = False
else:
print(f" else if ( key == \"{field.name}\" ) {{", file=file)
if field.required or field.default is not None:
print(f" val.get_to(o.{field.name});", file=file)
else:
print(f" {get_type_name(field.type)} field;", file=file)
print(" val.get_to(field);", file=file)
print(f" o.{field.name} = field;", file=file)
print(f" val.get_to(o.{field.name});", file=file)
print(" }", end="", file=file)
if elem.additional_props is not None:
print(" else {", file=file)
Expand All @@ -153,6 +153,7 @@ def print_from_json(elem: Struct, file):
print(" }", file=file)
print(" }", file=file)


def print_to_json(elem: Struct, file):
print(f" void to_json(json &j, const {get_type_name(elem)} &o) {{", file=file)
for field in elem.fields:
Expand Down Expand Up @@ -185,6 +186,7 @@ def print_to_json(elem: Struct, file):
print(" }", file=file)
print(" }", file=file)


if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -237,6 +239,15 @@ def print_to_json(elem: Struct, file):
#include <gap/sarif/sarif.hpp>
namespace nlohmann {
template<typename T>
struct adl_serializer< std::optional<T> > {
static void from_json(const json& j, std::optional<T>& opt) {
opt = std::optional<T>{ j.get<T>() };
}
};
}
namespace gap::sarif {""", file=output_source)
for elem in output:
if not isinstance(elem, Struct):
Expand Down
Loading

0 comments on commit c7d5089

Please sign in to comment.