コード例 #1
0
ファイル: dict.py プロジェクト: freider/tsgen
 def ts_create_dto(self, ctx: CodeSnippetContext,
                   ts_expression: str) -> str:
     ctx.add("_mapObject", self.MAP_OBJECT_TS_HELPER)
     sub_expr = self.value_type.ts_create_dto(ctx, "val")
     if sub_expr == "val":
         return ts_expression
     return f"_mapObject({ts_expression}, val => ({sub_expr}))"
コード例 #2
0
def test_snippet_context_subcontext():
    ctx = CodeSnippetContext()
    ctx.add("foo", "dummy")
    sub = ctx.subcontext("foo")
    sub.add("bar", "dummy")
    ctx.add("baz", "dummy")
    assert "foo" in ctx
    assert "bar" in ctx
    assert ctx.top_level_snippets() == {"foo", "baz"}
    assert ctx.natural_order() == ["baz", "bar", "foo"]
コード例 #3
0
 def ts_repr(self, ctx: CodeSnippetContext):
     if self.name:
         interface_name = to_pascal(self.name) if self.translate_name else self.name
         if interface_name not in ctx:
             subctx = ctx.subcontext(interface_name)
             code = self._render_ts_interface(interface_name, subctx)
             ctx.add(interface_name, code)
         return interface_name
     else:
         return self._render_ts_interface(None, ctx)
コード例 #4
0
ファイル: apis.py プロジェクト: freider/tsgen
def build_ts_func(
        name: str,
        return_type_tree: Optional[AbstractNode],
        payload: Optional[tuple[str, AbstractNode]],
        url_pattern: str,
        url_args: list[str],
        method: str,
        ctx: CodeSnippetContext
    ):
    ts_args = []
    for arg in url_args:
        ts_arg_name = to_camel(arg)
        url_pattern = url_pattern.replace(f"<{arg}>", f"${{{ts_arg_name}}}")
        ts_args.append((ts_arg_name, "string"))

    if return_type_tree is None:
        ts_return_type = "void"
        return_expression = None
        response_dto_type = None
    else:
        ts_return_type = return_type_tree.ts_repr(ctx)
        return_expression = return_type_tree.ts_parse_dto(ctx, "dto")
        response_dto_type = return_type_tree.dto_tree().ts_repr(ctx)

    if payload:
        payload_name, payload_type_tree = payload
        ts_payload_type = payload_type_tree.ts_repr(ctx)
        payload_arg_name = to_camel(payload_name)
        payload_expression = payload_type_tree.ts_create_dto(ctx, payload_arg_name)
        ts_args.append((payload_arg_name, ts_payload_type))
    else:
        payload_expression = None

    ctx.add("ApiError", TS_API_ERROR)
    ts_function_code = jinja2.Template(TS_FUNC_TEMPLATE).render({
        "function_name": name,
        "response_type_name": ts_return_type,
        "response_dto_type": response_dto_type,
        "payload_expression": payload_expression,
        "args": ts_args,
        "method": method,
        "url_pattern": url_pattern,
        "return_expression": return_expression,
    })
    return ts_function_code
コード例 #5
0
ファイル: dates.py プロジェクト: freider/tsgen
 def ts_create_dto(self, ctx: CodeSnippetContext, ts_expression: str) -> Optional[str]:
     prep_function_name = "_formatISODateString"
     if prep_function_name not in ctx:
         iso_formatter_ts = f"const {prep_function_name} = (d: Date): string => d.toISOString().split('T')[0];"
         ctx.add(prep_function_name, iso_formatter_ts)
     return f"{prep_function_name}({ts_expression})"
コード例 #6
0
def test_snippet_context_single_snippet():
    ctx = CodeSnippetContext()
    ctx.add("foo", "dummy")
    assert "foo" in ctx
    assert ctx.top_level_snippets() == {"foo"}
    assert ctx.natural_order() == ["foo"]