Skip to content

Commit

Permalink
support type_mapping configuration for request for python server codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
wkarwacki committed Sep 8, 2024
1 parent 00dbd96 commit 2a8058c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/gen/python/server/templates/router.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from typing import Annotated, Any
responses={200: {"headers": { {{#each this.res.meta}}"{{@key}}": {"schema": {"type": "string"{{#if (eq this.type "const")}}, "const": "{{#each this.val}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}"{{/if}} }}{{/each}} }}}{{/if}}
)
def {{to_snake_case this.name}}({{#if this.req}}{{#if this.req.path}}{{fmtName this.req.path}}: {{>dtoName val=(fmtClass this.req.path)}}{{else}}{{#if (eq this.req.form "multipart/form-data")}}{{fmtName (add this.name "Req")}}: {{>dtoName val=(fmtClass (add this.name "Req"))}} = Depends({{>dtoName val=(fmtClass (add this.name "Req"))}}.of_form){{else if (or (eq this.req.type "obj") (eq this.req.type "seq") (eq this.req.type "map"))}}{{fmtName (add this.name "Req")}}: {{>dtoName val=(fmtClass (add this.name "Req"))}}{{else}}request: {{fmtType this.req}}{{/if}}{{/if}}, {{/if}}{{#each this.params}}{{to_snake_case this.name}}: Annotated[{{#if (and (hasKey this "default") (eq this.default null))}}{{fmtOpt (fmtType this)}}{{else}}{{fmtType this}}{{/if}}, {{fmtClass this.loc}}{{#if (ne (to_snake_case this.name) this.name)}}(alias = {{json this.name}}){{/if}}]{{#if (hasKey this "default")}} = {{fmtValue this.default}}{{/if}}, {{/each}}service: {{to_pascal_case ../../feature}}Service = Depends({{to_pascal_case ../../feature}}Service)) -> {{#with this.res}}{{#if (eq this.carrier "stream")}}Streaming{{else}}JSON{{/if}}Response{{else}}None{{/with}}:
{{#if this.res}}content{{#if (filterNonconst this.res.meta)}}, headers{{/if}} = {{/if}}service.{{to_snake_case this.name}}({{#if this.req}}{{#if this.req.path}}{{fmtName this.req.path}}{{else}}{{#if (or (eq this.req.form "multipart/form-data") (eq this.req.type "obj") (eq this.req.type "seq") (eq this.req.type "map"))}}{{fmtName (add this.name "Req")}}{{else}}request{{/if}}{{/if}}{{#if this.params}}, {{/if}}{{/if}}{{#each this.params}}{{to_snake_case this.name}}{{#unless @last}}, {{/unless}}{{/each}}){{#if this.res}}
{{#if this.res}}content{{#if (filterNonconst this.res.meta)}}, headers{{/if}} = {{/if}}service.{{to_snake_case this.name}}({{#if this.req}}{{#if this.req.path}}{{#with (resolveIfMappedType (fmtClass this.req.path))}}{{fmtName ../req.path}}_to_{{fmtName this}}({{fmtName ../req.path}}){{else}}{{fmtName this.req.path}}{{/with}}{{else}}{{#if (or (eq this.req.form "multipart/form-data") (eq this.req.type "obj") (eq this.req.type "seq") (eq this.req.type "map"))}}{{fmtName (add this.name "Req")}}{{else}}request{{/if}}{{/if}}{{#if this.params}}, {{/if}}{{/if}}{{#each this.params}}{{to_snake_case this.name}}{{#unless @last}}, {{/unless}}{{/each}}){{#if this.res}}
{{#with this.res}}{{#with (resolveIfMappedType (fmtClass this.path))}}content = {{fmtName this}}_to_{{fmtName ../path}}(content){{/with}}{{/with}}
{{#unless (eq this.res.carrier "stream")}}content = jsonable_encoder(content){{/unless}}
{{#with this.res}}response = {{#if (eq this.carrier "stream")}}Streaming{{else}}JSON{{/if}}Response(content=content{{#with this.form}}, media_type = "{{this}}"{{/with}}){{/with}}{{#each this.res.meta}}{{#if (eq this.type "const")}}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/gen/python/server/templates/service.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class {{to_pascal_case feature}}Service(ABC):
}{{/if}}

{{#unless ../../autoImpl}}@abstractmethod{{/unless}}
def {{to_snake_case this.name}}(self{{#if this.req}}, {{#if this.req.path}}{{fmtName this.req.path}}: {{>dtoName val=(fmtClass this.req.path)}}{{else}}{{#if (eq this.req.form "multipart/form-data")}}{{fmtName (add this.name "Req")}}: {{>dtoName val=(fmtClass (add this.name "Req"))}}{{else if (or (eq this.req.type "obj") (eq this.req.type "seq") (eq this.req.type "map"))}}{{fmtName (add this.name "Req")}}: {{fmtName (add this.name "Req")}}.{{>dtoName val=(fmtClass (add this.name "Req"))}}{{else}}request: {{fmtType this.req}}{{/if}}{{/if}}{{/if}}{{#each this.params}}, {{to_snake_case this.name}}: {{#if this.opt}}{{fmtOpt (fmtType this)}}{{else}}{{fmtType this}}{{/if}}{{/each}}) -> {{#if this.res}}{{#if (filterNonconst this.res.meta)}}Tuple[{{/if}}{{#if (eq this.res.carrier "stream")}}Generator[{{fmtType this.res}}, None, None]{{else}}{{#if this.res.path}}{{#with (resolveIfMappedType (fmtClass this.res.path))}}{{fmtClass this}}{{else}}{{fmtType this.res (to_pascal_case this.name)}}{{/with}}{{else if (or (eq this.res.type "obj") (eq this.res.type "seq") (eq this.res.type "map"))}}{{>dtoName val=(fmtClass (add this.name "Res"))}}{{else}}{{fmtType this.res}}{{/if}}{{/if}}{{#if this.res.meta}}{{#if (filterNonconst this.res.meta)}}, {{to_pascal_case this.name}}Headers]{{/if}}{{/if}}{{else}}None{{/if}}:
def {{to_snake_case this.name}}(self{{#if this.req}}, {{#if this.req.path}}{{fmtName this.req.path}}: {{#with (resolveIfMappedType (fmtClass this.req.path))}}{{fmtClass this}}{{else}}{{>dtoName val=(fmtClass this.req.path)}}{{/with}}{{else}}{{#if (eq this.req.form "multipart/form-data")}}{{fmtName (add this.name "Req")}}: {{>dtoName val=(fmtClass (add this.name "Req"))}}{{else if (or (eq this.req.type "obj") (eq this.req.type "seq") (eq this.req.type "map"))}}{{fmtName (add this.name "Req")}}: {{fmtName (add this.name "Req")}}.{{>dtoName val=(fmtClass (add this.name "Req"))}}{{else}}request: {{fmtType this.req}}{{/if}}{{/if}}{{/if}}{{#each this.params}}, {{to_snake_case this.name}}: {{#if this.opt}}{{fmtOpt (fmtType this)}}{{else}}{{fmtType this}}{{/if}}{{/each}}) -> {{#if this.res}}{{#if (filterNonconst this.res.meta)}}Tuple[{{/if}}{{#if (eq this.res.carrier "stream")}}Generator[{{fmtType this.res}}, None, None]{{else}}{{#if this.res.path}}{{#with (resolveIfMappedType (fmtClass this.res.path))}}{{fmtClass this}}{{else}}{{fmtType this.res (to_pascal_case this.name)}}{{/with}}{{else if (or (eq this.res.type "obj") (eq this.res.type "seq") (eq this.res.type "map"))}}{{>dtoName val=(fmtClass (add this.name "Res"))}}{{else}}{{fmtType this.res}}{{/if}}{{/if}}{{#if this.res.meta}}{{#if (filterNonconst this.res.meta)}}, {{to_pascal_case this.name}}Headers]{{/if}}{{/if}}{{else}}None{{/if}}:
{{#if (resolveIfMappedType (fmtClass this.res.path))}}raise NotImplementedError{{else}}{{#if ../../autoImpl}}{{#if this.res}}return {{#if (filterNonconst this.res.meta)}}({{/if}}{{>stubImpl val=this.res key=this.name}}{{#if (filterNonconst this.res.meta)}}, {{to_pascal_case ../../feature}}Service.{{to_pascal_case this.name}}Headers.get()){{/if}}{{else}}pass{{/if}}{{else}}raise NotImplementedError{{/if}}{{/if}}
{{/each}}{{/each}}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pydantic import BaseModel
from trust import TypeMapping
from trust.transformer.transformer_preview_request import TransformerPreviewRequestDto


class AppTransformerPreviewRequest(BaseModel, TypeMapping[TransformerPreviewRequestDto, 'AppTransformerPreviewRequest']):
start: int
end: int

@classmethod
def of(cls, dto: TransformerPreviewRequestDto) -> 'AppTransformerPreviewRequest':
return AppTransformerPreviewRequest(
start=dto.start_row,
end=dto.end_row
)

def to(self) -> TransformerPreviewRequestDto:
return TransformerPreviewRequestDto(
start_row=self.start,
end_row=self.end,
filter_masterpiece={},
sort_masterpiece=[]
)
1 change: 1 addition & 0 deletions test/integration/gens/python/server/trust-cfg.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
autoImplement: true
typeMapping:
LiftResponse: app.ibis.app_lift_response.AppLiftResponse
TransformerPreviewRequest: app.transformer.app_transformer_preview_request.AppTransformerPreviewRequest

0 comments on commit 2a8058c

Please sign in to comment.