Fix deflator and typename item types escaping
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "megasniff"
|
name = "megasniff"
|
||||||
version = "0.2.4.post1"
|
version = "0.2.5"
|
||||||
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" }
|
||||||
|
|||||||
@@ -99,14 +99,12 @@ def _flatten_type(t: type | TypeAliasType) -> tuple[type, Optional[str]]:
|
|||||||
|
|
||||||
def _schema_to_deflator_func(t: type | TypeAliasType) -> str:
|
def _schema_to_deflator_func(t: type | TypeAliasType) -> str:
|
||||||
t, _ = _flatten_type(t)
|
t, _ = _flatten_type(t)
|
||||||
ret = 'deflate_' + typename(t).replace('.', '_')
|
return ('deflate_' + typename(t)
|
||||||
ret = ret.replace(' | ', '__OR__')
|
.replace('.', '_')
|
||||||
ret = ret.replace('<', '')
|
.replace('[', '_of_')
|
||||||
ret = ret.replace('>', '')
|
.replace(']', '_of_')
|
||||||
ret = ret.replace(' ', '__')
|
.replace(',', '_and_')
|
||||||
ret = ret.replace("'", '')
|
.replace(' ', '_'))
|
||||||
ret = ret.replace('.', '_')
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def _fallback_unwrapper(obj: Any) -> JsonObject:
|
def _fallback_unwrapper(obj: Any) -> JsonObject:
|
||||||
@@ -159,7 +157,7 @@ class SchemaDeflatorGenerator:
|
|||||||
self.object_template = self.templateEnv.get_template(object_template_filename)
|
self.object_template = self.templateEnv.get_template(object_template_filename)
|
||||||
|
|
||||||
def schema_to_deflator(self,
|
def schema_to_deflator(self,
|
||||||
schema: type | UnionType,
|
schema: type,
|
||||||
strict_mode_override: Optional[bool] = None,
|
strict_mode_override: Optional[bool] = None,
|
||||||
explicit_casts_override: Optional[bool] = None,
|
explicit_casts_override: Optional[bool] = None,
|
||||||
) -> Callable[[Any], dict[str, Any]]:
|
) -> Callable[[Any], dict[str, Any]]:
|
||||||
@@ -170,15 +168,13 @@ class SchemaDeflatorGenerator:
|
|||||||
imports = ('from typing import Any\n'
|
imports = ('from typing import Any\n'
|
||||||
'from megasniff.exceptions import MissingFieldException, FieldValidationException\n')
|
'from megasniff.exceptions import MissingFieldException, FieldValidationException\n')
|
||||||
txt = imports + '\n' + txt
|
txt = imports + '\n' + txt
|
||||||
print(txt)
|
|
||||||
exec(txt, namespace)
|
exec(txt, namespace)
|
||||||
fn = namespace[_schema_to_deflator_func(schema)]
|
fn = namespace[_schema_to_deflator_func(schema)]
|
||||||
if self._store_sources:
|
if self._store_sources:
|
||||||
setattr(fn, '__megasniff_sources__', txt)
|
setattr(fn, '__megasniff_sources__', txt)
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
def schema_to_unwrapper(self, schema: type | UnionType | TypeAliasType, *,
|
def schema_to_unwrapper(self, schema: type | TypeAliasType, *, _visited_types: Optional[list[type]] = None):
|
||||||
_visited_types: Optional[list[type]] = None):
|
|
||||||
if _visited_types is None:
|
if _visited_types is None:
|
||||||
_visited_types = []
|
_visited_types = []
|
||||||
else:
|
else:
|
||||||
@@ -253,16 +249,17 @@ class SchemaDeflatorGenerator:
|
|||||||
|
|
||||||
ret_unw = ObjectUnwrapping(fields)
|
ret_unw = ObjectUnwrapping(fields)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError(f'type not implemented yet: {schema}')
|
||||||
|
|
||||||
return ret_unw, field_rename, set(_visited_types) | ongoing_types, recurcive_types
|
return ret_unw, field_rename, set(_visited_types) | ongoing_types, recurcive_types
|
||||||
|
|
||||||
def _schema_to_deflator(self,
|
def _schema_to_deflator(self,
|
||||||
schema: type | UnionType,
|
schema: type | Sequence[TupleSchemaItem | tuple[str, type]] | OrderedDict[str, type],
|
||||||
strict_mode_override: Optional[bool] = None,
|
strict_mode_override: Optional[bool] = None,
|
||||||
explicit_casts_override: Optional[bool] = None,
|
explicit_casts_override: Optional[bool] = None,
|
||||||
into_type_override: Optional[type | TypeAliasType] = None,
|
into_type_override: Optional[type | TypeAliasType] = None,
|
||||||
*,
|
*,
|
||||||
|
_funcname='deflate',
|
||||||
_namespace=None,
|
_namespace=None,
|
||||||
) -> tuple[str, dict]:
|
) -> tuple[str, dict]:
|
||||||
if strict_mode_override is not None:
|
if strict_mode_override is not None:
|
||||||
|
|||||||
@@ -72,26 +72,21 @@ def typename(tp: type) -> str:
|
|||||||
if get_origin(tp) is None and hasattr(tp, '__name__'):
|
if get_origin(tp) is None and hasattr(tp, '__name__'):
|
||||||
ret = tp.__name__
|
ret = tp.__name__
|
||||||
ret = str(tp)
|
ret = str(tp)
|
||||||
|
ret = (ret
|
||||||
ret = ret.replace(' | ', '__OR__')
|
.replace('.', '_')
|
||||||
ret = ret.replace('<', '')
|
.replace('[', '_of_')
|
||||||
ret = ret.replace('>', '')
|
.replace(']', '_of_')
|
||||||
ret = ret.replace(' ', '__')
|
.replace(',', '_and_')
|
||||||
ret = ret.replace("'", '')
|
.replace(' ', '_')
|
||||||
ret = ret.replace('.', '_')
|
.replace('\'', '')
|
||||||
|
.replace('<', '')
|
||||||
|
.replace('>', ''))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def is_class_definition(obj):
|
def is_class_definition(obj):
|
||||||
return isinstance(obj, type) or inspect.isclass(obj)
|
return (isinstance(obj, type) or inspect.isclass(obj))
|
||||||
|
|
||||||
|
|
||||||
def hashname(obj) -> str:
|
def hashname(obj) -> str:
|
||||||
ret = '_' + str(hash(obj)).replace('-', '_')
|
return '_' + str(hash(obj)).replace('-', '_')
|
||||||
ret = ret.replace(' | ', '__OR__')
|
|
||||||
ret = ret.replace('<', '')
|
|
||||||
ret = ret.replace('>', '')
|
|
||||||
ret = ret.replace(' ', '__')
|
|
||||||
ret = ret.replace("'", '')
|
|
||||||
ret = ret.replace('.', '_')
|
|
||||||
return ret
|
|
||||||
|
|||||||
Reference in New Issue
Block a user