4242
4343from collections import defaultdict , OrderedDict
4444import random
45+ import re
4546from typing import Dict , Iterable , List , Optional , Tuple , Type , Union
4647
4748from .api .binary import get_binary_type , put_binary_type
5657 BinaryTypeError , CacheError , ReconnectError , SQLError , connection_errors ,
5758)
5859from .utils import (
59- entity_id , schema_id , select_version , status_to_exception , is_iterable
60+ capitalize , entity_id , schema_id , process_delimiter , select_version ,
61+ status_to_exception , is_iterable ,
6062)
6163from .binary import GenericObjectMeta
6264
@@ -82,6 +84,10 @@ class Client:
8284 _current_node : int = None
8385 _nodes : List [Connection ] = None
8486
87+ # used for Complex object data class names sanitizing
88+ _identifier = re .compile (r'[^0-9a-zA-Z_.+$]' , re .UNICODE )
89+ _ident_start = re .compile (r'^[^a-zA-Z_]+' , re .UNICODE )
90+
8591 affinity_version : Optional [Tuple ] = None
8692 protocol_version : Optional [Tuple ] = None
8793
@@ -383,11 +389,40 @@ def _sync_binary_registry(self, type_id: int):
383389 for schema in type_info ['schemas' ]:
384390 if not self ._registry [type_id ].get (schema_id (schema ), None ):
385391 data_class = self ._create_dataclass (
386- type_info ['type_name' ],
392+ self . _create_type_name ( type_info ['type_name' ]) ,
387393 schema ,
388394 )
389395 self ._registry [type_id ][schema_id (schema )] = data_class
390396
397+ @classmethod
398+ def _create_type_name (cls , type_name : str ) -> str :
399+ """
400+ Creates Python data class name from GridGain binary type name.
401+
402+ Handles all the special cases found in
403+ `java.org.apache.ignite.binary.BinaryBasicNameMapper.simpleName()`.
404+ Tries to adhere to PEP8 along the way.
405+ """
406+
407+ # general sanitizing
408+ type_name = cls ._identifier .sub ('' , type_name )
409+
410+ # - name ending with '$' (Scala)
411+ # - name + '$' + some digits (anonymous class)
412+ # - '$$Lambda$' in the middle
413+ type_name = process_delimiter (type_name , '$' )
414+
415+ # .NET outer/inner class delimiter
416+ type_name = process_delimiter (type_name , '+' )
417+
418+ # Java fully qualified class name
419+ type_name = process_delimiter (type_name , '.' )
420+
421+ # start chars sanitizing
422+ type_name = capitalize (cls ._ident_start .sub ('' , type_name ))
423+
424+ return type_name
425+
391426 def register_binary_type (
392427 self , data_class : Type , affinity_key_field : str = None ,
393428 ):
0 commit comments