Codegen: add internal to properties, rename ql_internal->internal

This commit is contained in:
Paolo Tranquilli
2023-11-24 05:58:45 +01:00
parent a8fcfd154f
commit 9264b2a6d6
6 changed files with 50 additions and 29 deletions

View File

@@ -114,6 +114,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
description=prop.description,
synth=bool(cls.synth) or prop.synth,
type_is_hideable=lookup[prop.type].hideable if prop.type in lookup else False,
internal="ql_internal" in prop.pragmas,
)
if prop.is_single:
args.update(
@@ -151,7 +152,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
def get_ql_class(cls: schema.Class, lookup: typing.Dict[str, schema.Class]) -> ql.Class:
pragmas = {k: True for k in cls.pragmas if k.startswith("ql")}
pragmas = {k: True for k in cls.pragmas if k.startswith("qltest")}
prev_child = ""
properties = []
for p in cls.properties:
@@ -167,6 +168,7 @@ def get_ql_class(cls: schema.Class, lookup: typing.Dict[str, schema.Class]) -> q
dir=pathlib.Path(cls.group or ""),
doc=cls.doc,
hideable=cls.hideable,
internal="ql_internal" in cls.pragmas,
**pragmas,
)
@@ -313,7 +315,7 @@ def _get_stub(cls: schema.Class, base_import: str, generated_import_prefix: str)
accessors = []
return ql.Stub(name=cls.name, base_import=base_import, import_prefix=generated_import_prefix,
doc=cls.doc, synth_accessors=accessors,
ql_internal="ql_internal" in cls.pragmas)
internal="ql_internal" in cls.pragmas)
_stub_qldoc_header = "// the following QLdoc is generated: if you need to edit it, do it in the schema file\n"
@@ -397,7 +399,7 @@ def generate(opts, renderer):
_patch_class_qldoc(c.name, qldoc, stub_file)
# for example path/to/elements -> path/to/elements.qll
renderer.render(ql.ImportList([i for name, i in imports.items() if not classes[name].ql_internal]),
renderer.render(ql.ImportList([i for name, i in imports.items() if not classes[name].internal]),
include_file)
elements_module = get_import(include_file, opts.root_dir)
@@ -405,7 +407,7 @@ def generate(opts, renderer):
renderer.render(
ql.GetParentImplementation(
classes=list(classes.values()),
imports=[elements_module] + [i for name, i in imports.items() if classes[name].ql_internal],
imports=[elements_module] + [i for name, i in imports.items() if classes[name].internal],
),
out / 'ParentChild.qll')

View File

@@ -44,6 +44,7 @@ class Property:
doc_plural: Optional[str] = None
synth: bool = False
type_is_hideable: bool = False
internal: bool = False
def __post_init__(self):
if self.tableparams:
@@ -112,7 +113,7 @@ class Class:
qltest_skip: bool = False
qltest_collapse_hierarchy: bool = False
qltest_uncollapse_hierarchy: bool = False
ql_internal: bool = False
internal: bool = False
doc: List[str] = field(default_factory=list)
hideable: bool = False
@@ -162,7 +163,7 @@ class Stub:
base_import: str
import_prefix: str
synth_accessors: List[SynthUnderlyingAccessor] = field(default_factory=list)
ql_internal: bool = False
internal: bool = False
doc: List[str] = field(default_factory=list)
@property
@@ -171,7 +172,7 @@ class Stub:
@property
def has_qldoc(self) -> bool:
return bool(self.doc) or self.ql_internal
return bool(self.doc) or self.internal
@dataclass

View File

@@ -3,8 +3,8 @@
{{#doc}}
* {{.}}
{{/doc}}
{{#ql_internal}}
{{#internal}}
* INTERNAL: Do not use.
{{/ql_internal}}
{{/internal}}
*/
{{/has_qldoc}}

View File

@@ -1,6 +1,6 @@
/**
* This module provides a hand-modifiable wrapper around the generated class `{{name}}`.
{{#ql_internal}}
{{#internal}}
* INTERNAL: Do not use.
{{/ql_internal}}
{{/internal}}
*/

View File

@@ -147,15 +147,15 @@ def test_class_with_children():
assert cls.has_children is True
@pytest.mark.parametrize("doc,ql_internal,expected",
@pytest.mark.parametrize("doc,internal,expected",
[
(["foo", "bar"], False, True),
(["foo", "bar"], True, True),
([], False, False),
([], True, True),
])
def test_has_doc(doc, ql_internal, expected):
stub = ql.Stub("Class", base_import="foo", import_prefix="bar", doc=doc, ql_internal=ql_internal)
def test_has_doc(doc, internal, expected):
stub = ql.Stub("Class", base_import="foo", import_prefix="bar", doc=doc, internal=internal)
assert stub.has_qldoc is expected

View File

@@ -157,8 +157,8 @@ def test_one_empty_internal_class(generate_classes):
assert generate_classes([
schema.Class("A", pragmas=["ql_internal"])
]) == {
"A.qll": (a_ql_stub(name="A", ql_internal=True),
a_ql_class(name="A", final=True, ql_internal=True)),
"A.qll": (a_ql_stub(name="A", internal=True),
a_ql_class(name="A", final=True, internal=True)),
}
@@ -202,11 +202,11 @@ def test_hierarchy_children(generate_children_implementations):
schema.Class("C", bases=["A"], derived={"D"}, pragmas=["ql_internal"]),
schema.Class("D", bases=["B", "C"]),
]) == ql.GetParentImplementation(
classes=[a_ql_class(name="A", ql_internal=True),
classes=[a_ql_class(name="A", internal=True),
a_ql_class(name="B", bases=["A"], imports=[
stub_import_prefix + "A"]),
a_ql_class(name="C", bases=["A"], imports=[
stub_import_prefix + "A"], ql_internal=True),
stub_import_prefix + "A"], internal=True),
a_ql_class(name="D", final=True, bases=["B", "C"],
imports=[stub_import_prefix + cls for cls in "BC"]),
],
@@ -228,6 +228,21 @@ def test_single_property(generate_classes):
}
def test_internal_property(generate_classes):
assert generate_classes([
schema.Class("MyObject", properties=[
schema.SingleProperty("foo", "bar", pragmas=["ql_internal"])]),
]) == {
"MyObject.qll": (a_ql_stub(name="MyObject"),
a_ql_class(name="MyObject", final=True,
properties=[
ql.Property(singular="Foo", type="bar", tablename="my_objects",
tableparams=["this", "result"], doc="foo of this my object",
internal=True),
])),
}
def test_children(generate_classes):
assert generate_classes([
schema.Class("FakeRoot"),
@@ -424,7 +439,8 @@ def test_class_dir(generate_classes):
schema.Class("A", derived={"B"}, group=dir),
schema.Class("B", bases=["A"]),
]) == {
f"{dir}/A.qll": (a_ql_stub(name="A", import_prefix="another.rel.path."), a_ql_class(name="A", dir=pathlib.Path(dir))),
f"{dir}/A.qll": (
a_ql_stub(name="A", import_prefix="another.rel.path."), a_ql_class(name="A", dir=pathlib.Path(dir))),
"B.qll": (a_ql_stub(name="B"),
a_ql_class(name="B", final=True, bases=["A"],
imports=[stub_import_prefix + "another.rel.path.A"])),
@@ -878,9 +894,9 @@ def test_stub_on_class_with_synth_from_class(generate_classes):
ql.SynthUnderlyingAccessor(argument="Entity", type="Raw::A", constructorparams=["result"]),
]),
a_ql_class(name="MyObject", final=True, properties=[
ql.Property(singular="Foo", type="bar", tablename="my_objects", synth=True,
tableparams=["this", "result"], doc="foo of this my object"),
])),
ql.Property(singular="Foo", type="bar", tablename="my_objects", synth=True,
tableparams=["this", "result"], doc="foo of this my object"),
])),
}
@@ -895,9 +911,9 @@ def test_stub_on_class_with_synth_on_arguments(generate_classes):
ql.SynthUnderlyingAccessor(argument="Label", type="string", constructorparams=["_", "_", "result"]),
]),
a_ql_class(name="MyObject", final=True, properties=[
ql.Property(singular="Foo", type="bar", tablename="my_objects", synth=True,
tableparams=["this", "result"], doc="foo of this my object"),
])),
ql.Property(singular="Foo", type="bar", tablename="my_objects", synth=True,
tableparams=["this", "result"], doc="foo of this my object"),
])),
}
@@ -909,7 +925,8 @@ def test_synth_property(generate_classes):
"MyObject.qll": (a_ql_stub(name="MyObject"),
a_ql_class(name="MyObject", final=True,
properties=[
ql.Property(singular="Foo", type="bar", tablename="my_objects", synth=True,
ql.Property(singular="Foo", type="bar", tablename="my_objects",
synth=True,
tableparams=["this", "result"], doc="foo of this my object"),
])),
}
@@ -934,9 +951,10 @@ def test_hideable_property(generate_classes):
"Other.qll": (a_ql_stub(name="Other"),
a_ql_class(name="Other", imports=[stub_import_prefix + "MyObject"],
final=True, properties=[
ql.Property(singular="X", type="MyObject", tablename="others", type_is_hideable=True,
tableparams=["this", "result"], doc="x of this other"),
])),
ql.Property(singular="X", type="MyObject", tablename="others",
type_is_hideable=True,
tableparams=["this", "result"], doc="x of this other"),
])),
}