Make argnames escape
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "megasniff"
|
name = "megasniff"
|
||||||
version = "0.2.3.post1"
|
version = "0.2.3.post2"
|
||||||
description = "Library for in-time codegened type validation"
|
description = "Library for in-time codegened type validation"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "nikto_b", email = "niktob560@yandex.ru" }
|
{ name = "nikto_b", email = "niktob560@yandex.ru" }
|
||||||
|
|||||||
@@ -32,15 +32,38 @@ class IterableTypeRenderData(TypeRenderData):
|
|||||||
is_union = False
|
is_union = False
|
||||||
|
|
||||||
|
|
||||||
|
def _escape_python_name(name: str) -> str:
|
||||||
|
name = name.replace('-', '__dash__').replace('+', '__plus__').replace('/', '__shash__')
|
||||||
|
if name[0].isnumeric():
|
||||||
|
name = '__num__' + name
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FieldRenderData:
|
class FieldRenderData:
|
||||||
argname: str
|
argname: str
|
||||||
|
argname_escaped: str
|
||||||
constrs: TypeRenderData
|
constrs: TypeRenderData
|
||||||
typename: str
|
typename: str
|
||||||
is_optional: bool
|
is_optional: bool
|
||||||
allow_none: bool
|
allow_none: bool
|
||||||
default_option: Optional[str]
|
default_option: Optional[str]
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
argname: str,
|
||||||
|
constrs: TypeRenderData,
|
||||||
|
typename: str,
|
||||||
|
is_optional: bool,
|
||||||
|
allow_none: bool,
|
||||||
|
default_option: Optional[str]):
|
||||||
|
self.argname = argname
|
||||||
|
self.constrs = constrs
|
||||||
|
self.typename = typename
|
||||||
|
self.is_optional = is_optional
|
||||||
|
self.allow_none = allow_none
|
||||||
|
self.default_option = default_option
|
||||||
|
self.argname_escaped = _escape_python_name(argname)
|
||||||
|
|
||||||
|
|
||||||
class SchemaInflatorGenerator:
|
class SchemaInflatorGenerator:
|
||||||
templateLoader: jinja2.BaseLoader
|
templateLoader: jinja2.BaseLoader
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{
|
|||||||
{% for conv in conversions %}
|
{% for conv in conversions %}
|
||||||
if '{{conv.argname}}' not in from_data_keys:
|
if '{{conv.argname}}' not in from_data_keys:
|
||||||
{% if conv.is_optional %}
|
{% if conv.is_optional %}
|
||||||
{{conv.argname}} = {{conv.default_option}}
|
{{conv.argname_escaped}} = {{conv.default_option}}
|
||||||
{% else %}
|
{% else %}
|
||||||
raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}")
|
raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}")
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@@ -21,12 +21,12 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{
|
|||||||
{% if not conv.allow_none %}
|
{% if not conv.allow_none %}
|
||||||
raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data)
|
raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data)
|
||||||
{% else %}
|
{% else %}
|
||||||
{{conv.argname}} = None
|
{{conv.argname_escaped}} = None
|
||||||
{% endif %}
|
{% endif %}
|
||||||
else:
|
else:
|
||||||
|
|
||||||
{{ unwrap_type_data.render_segment(conv.argname, conv.constrs, "conv_data", false) | indent(4*3) }}
|
{{ unwrap_type_data.render_segment(conv.argname_escaped, conv.constrs, "conv_data", false) | indent(4*3) }}
|
||||||
|
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
return {{funcname}}_tgt_type({% for conv in conversions %}{{conv.argname}}={{conv.argname}}, {% endfor %})
|
return {{funcname}}_tgt_type({% for conv in conversions %}{{conv.argname_escaped}}={{conv.argname_escaped}}, {% endfor %})
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
|
|
||||||
def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{from_type}}{% endif %}) {% if tgt_type is not none %} -> tuple {% endif %}:
|
def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{from_type}}{% endif %}) {% if tgt_type is not none %} -> tuple {% endif %}:
|
||||||
"""
|
"""
|
||||||
{% for conv in conversions %}{{conv.argname}}:{{conv.typename}}, {% endfor %}
|
{% for conv in conversions %}{{conv.argname_escaped}}:{{conv.typename}}, {% endfor %}
|
||||||
"""
|
"""
|
||||||
from_data_keys = from_data.keys()
|
from_data_keys = from_data.keys()
|
||||||
|
|
||||||
{% for conv in conversions %}
|
{% for conv in conversions %}
|
||||||
if '{{conv.argname}}' not in from_data_keys:
|
if '{{conv.argname}}' not in from_data_keys:
|
||||||
{% if conv.is_optional %}
|
{% if conv.is_optional %}
|
||||||
{{conv.argname}} = {{conv.default_option}}
|
{{conv.argname_escaped}} = {{conv.default_option}}
|
||||||
{% else %}
|
{% else %}
|
||||||
raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}")
|
raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}")
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@@ -21,12 +21,12 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{
|
|||||||
{% if not conv.allow_none %}
|
{% if not conv.allow_none %}
|
||||||
raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data)
|
raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data)
|
||||||
{% else %}
|
{% else %}
|
||||||
{{conv.argname}} = None
|
{{conv.argname_escaped}} = None
|
||||||
{% endif %}
|
{% endif %}
|
||||||
else:
|
else:
|
||||||
|
|
||||||
{{ unwrap_type_data.render_segment(conv.argname, conv.constrs, "conv_data", false) | indent(4*3) }}
|
{{ unwrap_type_data.render_segment(conv.argname_escaped, conv.constrs, "conv_data", false) | indent(4*3) }}
|
||||||
|
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
return ({% for conv in conversions %}{{conv.argname}}, {% endfor %})
|
return ({% for conv in conversions %}{{conv.argname_escaped}}, {% endfor %})
|
||||||
|
|||||||
Reference in New Issue
Block a user