Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix, python: add best-case formatting to snippet templates #3428

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generators/python/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0-rc3
1.3.0-rc4
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from typing import Dict, List, Optional, Union

import fern.ir.resources as ir_types
Expand Down Expand Up @@ -63,6 +62,8 @@ class SnippetTemplateFactory:
# Write this in the fern def to share between FE + BE
TEMPLATE_SENTINEL = "$FERN_INPUT"

TAB_CHAR = '\t'

def __init__(
self,
project: Project,
Expand Down Expand Up @@ -133,9 +134,11 @@ def _get_generic_template(
is_optional=True,
template_string=f"{name}={self.TEMPLATE_SENTINEL}" if name is not None else f"{self.TEMPLATE_SENTINEL}",
template_inputs=[
PayloadInput(
location=location,
path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs),
TemplateInput.factory.payload(
PayloadInput(
location=location,
path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs),
)
)
],
)
Expand All @@ -148,17 +151,19 @@ def _get_container_template(
location: PayloadLocation,
wire_or_original_name: Optional[str],
name_breadcrumbs: Optional[List[str]],
indentation_level: int = 0,
) -> Union[Template, None]:
child_indentation_level = indentation_level + 1
return container.visit(
list_=lambda innerTr: Template.factory.iterable(
IterableTemplate(
imports=[],
is_optional=True,
container_template_string=f"{name}=[{self.TEMPLATE_SENTINEL}]"
container_template_string=f"{name}=[\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}]"
if name is not None
else f"[{self.TEMPLATE_SENTINEL}]",
delimiter=", ",
inner_template=self.get_type_reference_template(innerTr, None, location, None, None),
else f"[\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}]",
delimiter=f",\n{self.TAB_CHAR * child_indentation_level}",
inner_template=self.get_type_reference_template(innerTr, None, location, None, None, child_indentation_level),
template_input=PayloadInput(
location=location, path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs)
),
Expand All @@ -168,11 +173,11 @@ def _get_container_template(
IterableTemplate(
imports=[],
is_optional=True,
container_template_string=f"{name}={{{self.TEMPLATE_SENTINEL}}}"
container_template_string=f"{name}={{\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}}}"
if name is not None
else f"{{{self.TEMPLATE_SENTINEL}}}",
delimiter=", ",
inner_template=self.get_type_reference_template(innerTr, None, location, None, None),
else f"{{\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}}}",
delimiter=f",\n{self.TAB_CHAR * child_indentation_level}",
inner_template=self.get_type_reference_template(innerTr, None, location, None, None, child_indentation_level),
template_input=PayloadInput(
location=location, path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs)
),
Expand All @@ -182,20 +187,20 @@ def _get_container_template(
DictTemplate(
imports=[],
is_optional=True,
container_template_string=f"{name}={{{self.TEMPLATE_SENTINEL}}}"
container_template_string=f"{name}={{\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}}}"
if name is not None
else f"{{{self.TEMPLATE_SENTINEL}}}",
delimiter=", ",
else f"{{\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level}}}",
delimiter=f",\n{self.TAB_CHAR * child_indentation_level}",
key_value_separator=": ",
key_template=self.get_type_reference_template(kvTr.key_type, None, location, None, None),
value_template=self.get_type_reference_template(kvTr.value_type, None, location, None, None),
key_template=self.get_type_reference_template(kvTr.key_type, None, location, None, None, child_indentation_level),
value_template=self.get_type_reference_template(kvTr.value_type, None, location, None, None, child_indentation_level),
template_input=PayloadInput(
location=location, path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs)
),
)
),
optional=lambda value: self.get_type_reference_template(
value, name, location, wire_or_original_name, name_breadcrumbs
value, name, location, wire_or_original_name, name_breadcrumbs, indentation_level
),
literal=lambda _: None,
)
Expand Down Expand Up @@ -405,6 +410,7 @@ def _get_object_template(
location: PayloadLocation,
wire_or_original_name: Optional[str],
name_breadcrumbs: Optional[List[str]],
indentation_level: int = 0,
) -> Template:
object_reference = self._context.pydantic_generator_context.get_class_reference_for_type_id(
type_id=type_name.type_id
Expand All @@ -428,18 +434,20 @@ def _get_object_template(
if template_input is not None:
template_inputs.append(template_input)

object_class_name = type_name.name.pascal_case.unsafe_name
child_indentation_level = indentation_level + 1
return Template.factory.generic(
GenericTemplate(
imports=[self._imports_manager._get_import_as_string(object_reference.import_)]
if object_reference.import_ is not None
else [],
is_optional=True,
# TODO: move the object name getter to a function instead of the dot access below
template_string=f"{name}={type_name.name.pascal_case.unsafe_name}(\n{self.TEMPLATE_SENTINEL}\n)"
template_string=f"{name}={object_class_name}(\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level})"
if name is not None
else f"{type_name.name.pascal_case.unsafe_name}({self.TEMPLATE_SENTINEL})",
else f"{object_class_name}(\n{self.TAB_CHAR * child_indentation_level}{self.TEMPLATE_SENTINEL}\n{self.TAB_CHAR * indentation_level})",
template_inputs=template_inputs,
input_delimiter=",\n",
input_delimiter=f",\n{self.TAB_CHAR * child_indentation_level}",
)
)

Expand All @@ -450,6 +458,7 @@ def _get_named_template(
location: PayloadLocation,
wire_or_original_name: Optional[str],
name_breadcrumbs: Optional[List[str]],
indentation_level: int = 0,
) -> Union[Template, None]:
type_declaration = self._context.pydantic_generator_context.get_declaration_for_type_id(
type_id=type_name.type_id
Expand All @@ -463,6 +472,7 @@ def _get_named_template(
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level
),
enum=lambda etd: self._get_enum_template(
type_name=type_name,
Expand All @@ -479,6 +489,7 @@ def _get_named_template(
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level
),
union=lambda utd: self._get_discriminated_union_template(
type_name=type_name,
Expand All @@ -499,6 +510,7 @@ def get_type_reference_template(
location: PayloadLocation,
wire_or_original_name: Optional[str],
name_breadcrumbs: Optional[List[str]],
indentation_level: int = 0,
) -> Union[Template, None]:
# if type is literal return None, we do not use literals as inputs
if self._is_type_literal(type_):
Expand All @@ -523,13 +535,15 @@ def get_type_reference_template(
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
),
named=lambda type_name: self._get_named_template(
type_name=type_name,
name=name,
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
),
)

Expand All @@ -540,6 +554,7 @@ def get_type_reference_template_input(
location: PayloadLocation,
wire_or_original_name: Optional[str],
name_breadcrumbs: Optional[List[str]],
indentation_level: int = 0,
) -> Union[TemplateInput, None]:
# if type is literal return None, we do not use literals as inputs
if self._is_type_literal(type_):
Expand All @@ -551,6 +566,7 @@ def get_type_reference_template_input(
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
)
return self._get_template_input_from_template(template=template) if template is not None else None

Expand Down Expand Up @@ -623,6 +639,7 @@ def generate_templates(self) -> List[SnippetRegistryEntry]:
location="HEADERS",
wire_or_original_name=header.name.wire_value,
name_breadcrumbs=None,
indentation_level=1
)
if ti is not None:
top_level_template_inputs.append(ti)
Expand All @@ -634,6 +651,7 @@ def generate_templates(self) -> List[SnippetRegistryEntry]:
location="PATH",
wire_or_original_name=path_parameter.name.original_name,
name_breadcrumbs=None,
indentation_level=1
)
if ti is not None:
top_level_template_inputs.append(ti)
Expand All @@ -645,6 +663,7 @@ def generate_templates(self) -> List[SnippetRegistryEntry]:
location="QUERY",
wire_or_original_name=query_parameter.name.name.original_name,
name_breadcrumbs=None,
indentation_level=1
)
if ti is not None:
top_level_template_inputs.append(ti)
Expand Down Expand Up @@ -681,6 +700,7 @@ def generate_templates(self) -> List[SnippetRegistryEntry]:
location="BODY",
wire_or_original_name=parameter.raw_name,
name_breadcrumbs=None,
indentation_level=1
)
if parameter.raw_type is not None
else None
Expand All @@ -698,7 +718,7 @@ def generate_templates(self) -> List[SnippetRegistryEntry]:
imports=[],
template_string=init_string_template,
template_inputs=top_level_template_inputs,
input_delimiter=",\n",
input_delimiter=",\n\t",
is_optional=True,
)
)
Expand Down
8 changes: 5 additions & 3 deletions seed/python-sdk/api-wide-base-path/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions seed/python-sdk/audiences/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions seed/python-sdk/basic-auth/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading