Bug 1693487 - Define interfaces for event extras on Typescript template (#373)
* Define interfaces foe event extras on Typescript template * Attend to review comments
This commit is contained in:
Родитель
be44315001
Коммит
0939066112
|
@ -1,6 +1,7 @@
|
|||
# Changelog
|
||||
|
||||
- Expose ping reasons enum on JavaScript / TypeScript templates. ([bug 1719136](https://bugzilla.mozilla.org/show_bug.cgi?id=1719136))
|
||||
- Define an interface with the allowed extras for each event on the TypeScript template. ([bug 1693487](https://bugzilla.mozilla.org/show_bug.cgi?id=1693487))
|
||||
|
||||
## 3.7.0 (2021-07-13)
|
||||
|
||||
|
|
|
@ -63,6 +63,16 @@ def class_name_factory(platform: str) -> Callable[[str], str]:
|
|||
return class_name
|
||||
|
||||
|
||||
def extra_type_name(extra_type: str) -> str:
|
||||
"""
|
||||
Returns the equivalent TypeScript type to an extra type.
|
||||
"""
|
||||
if extra_type == "quantity":
|
||||
return "number"
|
||||
|
||||
return extra_type
|
||||
|
||||
|
||||
def import_path(obj_type: str) -> str:
|
||||
"""
|
||||
Returns the import path of the given object inside the @mozilla/glean package.
|
||||
|
@ -127,6 +137,7 @@ def output(
|
|||
"javascript.jinja2",
|
||||
filters=(
|
||||
("class_name", class_name_factory(platform)),
|
||||
("extra_type_name", extra_type_name),
|
||||
("import_path", import_path),
|
||||
("js", javascript_datatypes_filter),
|
||||
("args", args),
|
||||
|
|
|
@ -312,7 +312,8 @@ class Event(Metric):
|
|||
def allowed_extra_keys_with_types(self):
|
||||
# Sort keys so that output is deterministic
|
||||
return sorted(
|
||||
[(k, v["type"]) for (k, v) in self.extra_keys.items()], key=lambda x: x[0]
|
||||
[(k, v.get("type", "string")) for (k, v) in self.extra_keys.items()],
|
||||
key=lambda x: x[0],
|
||||
)
|
||||
|
||||
@property
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
{# The final Javascript/Typescript code is autogenerated, but this
|
||||
Jinja2 template is not. Please file bugs! #}
|
||||
{% macro obj_declaration(obj) %}
|
||||
new {{ obj.type|class_name }}({
|
||||
new {{ obj.type|class_name }}{% if obj.extra_keys and lang == "typescript" %}<{
|
||||
{% for name, type in obj.allowed_extra_keys_with_types %}
|
||||
{{ name }}?: {{ type|extra_type_name }},
|
||||
{% endfor %}
|
||||
}>{% endif %}({
|
||||
{% for arg_name in (obj.type|args).common if obj[arg_name] is defined %}
|
||||
{{ arg_name|camelize }}: {{ obj[arg_name]|js }},
|
||||
{% endfor %}
|
||||
|
|
|
@ -373,3 +373,47 @@ def test_qt_platform_generated_correct_qmldir_file(tmpdir):
|
|||
assert content.count("DottedCategory 0.14 dottedCategory.js") == 1
|
||||
assert content.count("GleanInternalMetrics 0.14 gleanInternalMetrics.js") == 1
|
||||
assert content.count("depends org.mozilla.Glean 0.14") == 1
|
||||
|
||||
|
||||
def test_event_extra_keys_with_types(tmpdir):
|
||||
"""
|
||||
Assert that the extra keys with types appear with their corresponding types.
|
||||
"""
|
||||
|
||||
tmpdir = Path(str(tmpdir))
|
||||
|
||||
translate.translate(
|
||||
ROOT / "data" / "events_with_types.yaml",
|
||||
"typescript",
|
||||
tmpdir,
|
||||
)
|
||||
|
||||
assert set(x.name for x in tmpdir.iterdir()) == set(["core.ts"])
|
||||
|
||||
with (tmpdir / "core.ts").open("r", encoding="utf-8") as fd:
|
||||
content = fd.read()
|
||||
content = " ".join(content.split())
|
||||
print(content)
|
||||
assert (
|
||||
"new EventMetricType<{ "
|
||||
"enabled?: boolean, "
|
||||
"preference?: string, "
|
||||
"swapped?: number, "
|
||||
"}>({" in content
|
||||
)
|
||||
assert '"enabled", "preference", "swapped"' in content
|
||||
|
||||
# Make sure this only happens for the TypeScript template.
|
||||
translate.translate(
|
||||
ROOT / "data" / "events_with_types.yaml",
|
||||
"javascript",
|
||||
tmpdir,
|
||||
)
|
||||
|
||||
assert set(x.name for x in tmpdir.iterdir()) == set(["core.js", "core.ts"])
|
||||
|
||||
with (tmpdir / "core.js").open("r", encoding="utf-8") as fd:
|
||||
content = fd.read()
|
||||
content = " ".join(content.split())
|
||||
assert "new EventMetricType({" in content
|
||||
assert '"enabled", "preference", "swapped"' in content
|
||||
|
|
Загрузка…
Ссылка в новой задаче