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 + ts: additional template bugs #4198

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions generators/python/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.3.5] - 2024-08-04

- Fix: Literal templates are generated if they are union members

## [3.3.4] - 2024-08-02

- Improvement: Aliased literals are also defaulted within Pydantic models, whereas previously only direct literals were defaulted.
Expand Down
2 changes: 1 addition & 1 deletion generators/python/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.4
3.3.5
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def _get_container_template(
name_breadcrumbs: Optional[List[str]],
is_function_parameter: bool,
indentation_level: int = 0,
include_literal_templates: bool = False,
) -> Union[Template, None]:
child_indentation_level = indentation_level + 1

Expand Down Expand Up @@ -328,6 +329,27 @@ def _get_container_template(
is_function_parameter=is_function_parameter,
)

if include_literal_templates and container_union.type == "literal":
literal_value = container_union.literal.visit(
string=lambda s: f'"{s}"',
boolean=lambda b: f"{b}",
)
template_string = f"{self._get_name_value_separator(name=name, is_function_parameter=is_function_parameter)}{literal_value}"
return Template.factory.generic(
GenericTemplate(
is_optional=True,
template_string=template_string,
template_inputs=[
TemplateInput.factory.payload(
PayloadInput(
location=location,
path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs),
)
)
],
)
)

return None

def _convert_enum_value_to_str(
Expand Down Expand Up @@ -640,11 +662,13 @@ def _get_undiscriminated_union_template(
member_template = self.get_type_reference_template(
type_=member.type,
name=None,
location=location,
wire_or_original_name=wire_or_original_name,
name_breadcrumbs=name_breadcrumbs,
location="RELATIVE",
wire_or_original_name=None,
name_breadcrumbs=None,
indentation_level=indentation_level,
is_function_parameter=False,
# Allow creation of literal members
include_literal_templates=True,
)
if member_template is not None:
member_templates.append(
Expand All @@ -660,7 +684,10 @@ def _get_undiscriminated_union_template(
is_optional=True,
template_string=f"{self._get_name_value_separator(name=name, is_function_parameter=is_function_parameter)}{TEMPLATE_SENTINEL}",
members=member_templates,
template_input=PayloadInput(location="RELATIVE"),
template_input=PayloadInput(
location=location,
path=self._get_breadcrumb_path(wire_or_original_name, name_breadcrumbs),
),
)
)

Expand All @@ -673,6 +700,7 @@ def _get_named_template(
name_breadcrumbs: Optional[List[str]],
is_function_parameter: bool,
indentation_level: int = 0,
include_literal_templates: bool = False,
) -> Union[Template, None]:
type_declaration = self._context.pydantic_generator_context.get_declaration_for_type_id(
type_id=type_name.type_id
Expand All @@ -688,6 +716,7 @@ def _get_named_template(
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
is_function_parameter=is_function_parameter,
include_literal_templates=include_literal_templates,
),
enum=lambda etd: self._get_enum_template(
type_name=cast(ir_types.DeclaredTypeName, type_name),
Expand Down Expand Up @@ -738,6 +767,8 @@ def get_type_reference_template(
name_breadcrumbs: Optional[List[str]],
is_function_parameter: bool,
indentation_level: int = 0,
# Only used for union members
include_literal_templates: bool = False,
) -> Union[Template, None]:
# if type is literal return None, we do not use literals as inputs
if self._is_type_literal(type_):
Expand Down Expand Up @@ -766,6 +797,7 @@ def get_type_reference_template(
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
is_function_parameter=is_function_parameter,
include_literal_templates=include_literal_templates,
),
named=lambda type_name: self._get_named_template(
type_name=type_name,
Expand All @@ -775,6 +807,7 @@ def get_type_reference_template(
name_breadcrumbs=name_breadcrumbs,
indentation_level=indentation_level,
is_function_parameter=is_function_parameter,
include_literal_templates=include_literal_templates,
),
)

Expand Down
9 changes: 7 additions & 2 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.38.3] - 2024-08-04

- Fix: Literal templates are generated if they are union members
- Fix: Snippet templates no longer try to inline objects within containers

## [0.38.2] - 2024-08-01

- Fix: Refactors the `noScripts` feature flag to make sure that no `yarn install` commands
can be accidentally triggered.
- Fix: Refactors the `noScripts` feature flag to make sure that no `yarn install` commands
can be accidentally triggered.

## [0.38.1] - 2024-08-01

Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.38.2
0.38.3
67 changes: 47 additions & 20 deletions generators/typescript/sdk/generator/src/TemplateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,12 @@ export class TemplateGenerator {
const memberTemplate = this.getTemplateFromTypeReference({
typeReference: member.type,
name: undefined,
location,
wireOrOriginalName,
nameBreadcrumbs,
location: FdrSnippetTemplate.PayloadLocation.Relative,
wireOrOriginalName: undefined,
nameBreadcrumbs: undefined,
indentationLevel: childIndentationLevel,
isObjectInlined: true
isObjectInlined: true,
includeLiteralTemplates: true
});

if (memberTemplate != null) {
Expand Down Expand Up @@ -418,7 +419,7 @@ export class TemplateGenerator {
wireOrOriginalName: prop.name.wireValue,
nameBreadcrumbs: childBreadcrumbs,
indentationLevel: childIndentationLevel,
isObjectInlined
isObjectInlined: false
});

if (propInput != null) {
Expand All @@ -445,7 +446,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}: {
typeName: DeclaredTypeName;
name: string | undefined;
Expand All @@ -454,6 +456,7 @@ export class TemplateGenerator {
nameBreadcrumbs: string[] | undefined;
indentationLevel: number;
isObjectInlined: boolean;
includeLiteralTemplates?: boolean;
}): FdrSnippetTemplate.Template | undefined {
const td = this.endpointContext.type.getTypeDeclaration(typeName);
const generatedType = this.endpointContext.type.getGeneratedType(typeName);
Expand All @@ -476,7 +479,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}),
object: (otd) =>
this.getObjectTemplate({
Expand Down Expand Up @@ -517,7 +521,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}: {
containerType: ContainerType;
name: string | undefined;
Expand All @@ -526,6 +531,7 @@ export class TemplateGenerator {
nameBreadcrumbs: string[] | undefined;
indentationLevel: number;
isObjectInlined: boolean;
includeLiteralTemplates?: boolean;
}): FdrSnippetTemplate.Template | undefined {
const childIndentationLevel = indentationLevel + 1;
const selfTabs = "\t".repeat(indentationLevel);
Expand All @@ -540,7 +546,7 @@ export class TemplateGenerator {
wireOrOriginalName: undefined,
nameBreadcrumbs: undefined,
indentationLevel: childIndentationLevel,
isObjectInlined
isObjectInlined: false
});
return innerTemplate != null
? FdrSnippetTemplate.Template.iterable({
Expand All @@ -567,7 +573,7 @@ export class TemplateGenerator {
wireOrOriginalName: undefined,
nameBreadcrumbs: undefined,
indentationLevel: childIndentationLevel,
isObjectInlined
isObjectInlined: false
});

return innerTemplate != null
Expand Down Expand Up @@ -595,7 +601,7 @@ export class TemplateGenerator {
wireOrOriginalName: undefined,
nameBreadcrumbs: undefined,
indentationLevel: childIndentationLevel,
isObjectInlined
isObjectInlined: false
});
const valueTemplate = this.getTemplateFromTypeReference({
typeReference: kvType.valueType,
Expand All @@ -604,7 +610,7 @@ export class TemplateGenerator {
wireOrOriginalName: undefined,
nameBreadcrumbs: undefined,
indentationLevel: childIndentationLevel,
isObjectInlined
isObjectInlined: false
});

return keyTemplate != null && valueTemplate != null
Expand Down Expand Up @@ -636,7 +642,28 @@ export class TemplateGenerator {
indentationLevel,
isObjectInlined
}),
literal: () => undefined,
literal: (literal) => {
const literalValue = literal._visit<string>({
boolean: (bool) => bool.toString(),
string: (str) => `"${str}"`,
_other: () => {
throw new Error("Unknown literal type: " + literal.type);
}
});
return includeLiteralTemplates
? FdrSnippetTemplate.Template.generic({
imports: [],
templateString: name != null ? `${name}: ${literalValue}` : literalValue,
isOptional: true,
templateInputs: [
FdrSnippetTemplate.TemplateInput.payload({
location,
path: this.getBreadCrumbPath({ wireOrOriginalName, nameBreadcrumbs })
})
]
})
: undefined;
},
_other: () => undefined
});
}
Expand All @@ -648,7 +675,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}: {
typeReference: TypeReference;
name: string | undefined;
Expand All @@ -657,11 +685,8 @@ export class TemplateGenerator {
nameBreadcrumbs: string[] | undefined;
indentationLevel: number;
isObjectInlined: boolean;
includeLiteralTemplates?: boolean;
}): FdrSnippetTemplate.Template | undefined {
// Do not insert literals into templates
if (typeReference.type === "container" && typeReference.container.type === "literal") {
return;
}
// TODO: Implement a better way to handle type -> template relation to better handle
// circular references
if (indentationLevel > 10) {
Expand All @@ -679,7 +704,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}),
named: (typeName) =>
this.getNamedTypeTemplate({
Expand All @@ -689,7 +715,8 @@ export class TemplateGenerator {
wireOrOriginalName,
nameBreadcrumbs,
indentationLevel,
isObjectInlined
isObjectInlined,
includeLiteralTemplates
}),
_other: () => undefined
});
Expand Down
Loading
Loading