diff --git a/python/revng/daemon/schema.graphql.tpl b/python/revng/daemon/schema.graphql.tpl index 5fef17a04..1a33c6693 100644 --- a/python/revng/daemon/schema.graphql.tpl +++ b/python/revng/daemon/schema.graphql.tpl @@ -27,7 +27,7 @@ type Mutation { type AnalysisMutations { {%- for step in steps %} {%- if step.analyses_count() > 0 %} - {{ step.name | pascal_to_camel }}: {{ step.name }}Analyses! + {{ step.name }}: {{ step.name }}Analyses! {%- endif %} {%- endfor %} } @@ -101,7 +101,7 @@ type Target { {% for rank, steps in rank_to_artifact_steps.items() %} type {{ rank.name | capitalize }} { {%- for step in steps %} - {{ step.name | pascal_to_camel }}(onlyIfReady: Boolean): String! + {{ step.name }}(onlyIfReady: Boolean): String! {%- endfor %} } {% endfor %} @@ -110,7 +110,7 @@ type {{ rank.name | capitalize }} { {%- if step.analyses_count() > 0 %} type {{ step.name }}Analyses { {%- for analysis in step.analyses() %} - {{ analysis.name | pascal_to_camel }}({{ analysis | generate_analysis_parameters }}): String! + {{ analysis.name | normalize }}({{ analysis | generate_analysis_parameters }}): String! {%- endfor %} } {%- endif %} diff --git a/python/revng/daemon/schema_generator.py b/python/revng/daemon/schema_generator.py index e238dd2dd..3de49d88d 100644 --- a/python/revng/daemon/schema_generator.py +++ b/python/revng/daemon/schema_generator.py @@ -3,6 +3,7 @@ # import json +import re from collections import defaultdict from pathlib import Path from typing import Dict, List, Set @@ -17,7 +18,6 @@ from revng.api.rank import Rank from revng.api.step import Step from .static_handlers import DEFAULT_BINDABLES, analysis_mutations, run_in_executor -from .util import pascal_to_camel, str_to_snake_case class SchemaGenerator: @@ -31,8 +31,7 @@ class SchemaGenerator: self.jinja_environment = Environment(loader=FileSystemLoader(str(local_folder))) filters = self.jinja_environment.filters filters["rank_param"] = self._rank_to_arguments - filters["pascal_to_camel"] = pascal_to_camel - filters["str_to_snake_case"] = str_to_snake_case + filters["normalize"] = normalize filters["generate_analysis_parameters"] = self._generate_analysis_parameters def get_schema(self, manager: Manager) -> GraphQLSchema: @@ -56,7 +55,7 @@ class SchemaGenerator: @staticmethod def _generate_analysis_parameters(analysis: Analysis) -> str: return ", ".join( - f"{str_to_snake_case(argument.name)}: String!" for argument in analysis.arguments() + f"{normalize(argument.name)}: String!" for argument in analysis.arguments() ) @staticmethod @@ -107,7 +106,7 @@ class DynamicBindableGenerator: handle = self.gen_step_handle(step) rank_obj = rank_objects[step_kind.rank] - rank_obj.set_field(pascal_to_camel(step.name), handle) + rank_obj.set_field(step.name, handle) return bindables @@ -118,12 +117,12 @@ class DynamicBindableGenerator: if step.analyses_count() < 1: continue - analysis_mutations.set_field(pascal_to_camel(step.name), self.analysis_mutation_handle) + analysis_mutations.set_field(step.name, self.analysis_mutation_handle) step_analysis_obj = ObjectType(f"{step.name}Analyses") bindables.append(step_analysis_obj) for analysis in step.analyses(): handle = self.gen_step_analysis_handle(step, analysis) - step_analysis_obj.set_field(pascal_to_camel(analysis.name), handle) + step_analysis_obj.set_field(normalize(analysis.name), handle) return bindables @@ -158,7 +157,7 @@ class DynamicBindableGenerator: @staticmethod def gen_step_analysis_handle(step: Step, analysis: Analysis): - argument_mapping = {str_to_snake_case(a.name): a.name for a in analysis.arguments()} + argument_mapping = {normalize(a.name): a.name for a in analysis.arguments()} async def step_analysis_handle(_, info, **kwargs): manager: Manager = info.context["manager"] @@ -174,3 +173,8 @@ class DynamicBindableGenerator: return json.dumps(result) return step_analysis_handle + + +def normalize(string: str) -> str: + leading_digit = bool(re.match(r"\d", string)) + return re.sub(r"[^A-Za-z0-9_]", "_", f"{'_' if leading_digit else ''}{string}") diff --git a/python/revng/daemon/util.py b/python/revng/daemon/util.py index 69519809e..60aa6da65 100644 --- a/python/revng/daemon/util.py +++ b/python/revng/daemon/util.py @@ -47,28 +47,6 @@ def clean_container_list(container_list: List): container_list.remove(container) -def str_to_snake_case(string: str) -> str: - ret = [] - for idx, char in enumerate(string): - if char.isupper(): - if (idx > 0 and string[idx - 1].isupper()) or idx == 0: - ret.append(char.lower()) - else: - ret += ["_", char.lower()] - elif char == ".": - ret.append("_") - else: - ret.append(char) - return "".join(ret) - - -def pascal_to_camel(string: str) -> str: - if len(string) == 0: - return "" - first = string[0] - return first.lower() + string[1:] - - def b64e(string: str) -> str: ret = b64encode(string.encode("utf-8")) return ret.decode("utf-8") diff --git a/share/revng/test/tests/daemon/test.py b/share/revng/test/tests/daemon/test.py index 190b18842..d48a3e2f0 100755 --- a/share/revng/test/tests/daemon/test.py +++ b/share/revng/test/tests/daemon/test.py @@ -160,33 +160,13 @@ def test_info_global(client): def test_lift(client): - q = gql( - """ - { - binary { - lift - } - } - """ - ) - - result = client.execute(q) - - assert result["binary"]["lift"] is not None + result = client.execute(gql("{ binary { Lift } }")) + assert result["binary"]["Lift"] is not None @mark.xfail(raises=Exception) def test_lift_ready_fail(client): - q = gql( - """ - { - binary { - lift(onlyIfReady: true) - } - } - """ - ) - client.execute(q) + client.execute(gql("{ binary { Lift(onlyIfReady: true) } }")) @mark.xfail(raises=Exception) @@ -255,18 +235,8 @@ def test_begin_has_containers(client): def test_get_model(client): - client.execute(gql("{binary{lift}}")) - - q = gql( - """ - { - info { - model - } - } - """ - ) - result = client.execute(q) + client.execute(gql("{ binary { Lift } }")) + result = client.execute(gql("{ info { model } }")) assert result["info"]["model"] is not None @@ -366,8 +336,8 @@ def test_function_endpoint(client): """ mutation { analyses { - lift { - detectABI(module_ll: ":Root") + Lift { + DetectABI(module_ll: ":Root") } } } @@ -393,14 +363,14 @@ def test_function_endpoint(client): """ query function($param1: String!) { function(param1: $param1) { - isolate + Isolate } } """ ) result = client.execute(q, {"param1": first_function["serialized"]}) - assert result["function"]["isolate"] is not None + assert result["function"]["Isolate"] is not None @mark.xfail(raises=Exception) @@ -410,8 +380,8 @@ def test_analysis_kind_check(client): """ mutation { analyses { - lift { - detectABI(module_ll: ":IsolatedRoot") + Lift { + DetectABI(module_ll: ":IsolatedRoot") } } }