Add support for flags2 in the TL

This commit is contained in:
Lonami Exo
2022-05-12 10:20:42 +02:00
parent f9643bf737
commit 5feb210442
4 changed files with 44 additions and 46 deletions

View File

@@ -100,9 +100,9 @@ class TLArg:
# Default values
self.is_vector = False
self.is_flag = False
self.flag = None # name of the flag to check if self is present
self.skip_constructor_id = False
self.flag_index = -1
self.flag_index = -1 # bit index of the flag to check if self is present
self.cls = None
# Special case: some types can be inferred, which makes it
@@ -122,16 +122,14 @@ class TLArg:
# Strip the exclamation mark always to have only the name
self.type = arg_type.lstrip('!')
# The type may be a flag (flags.IDX?REAL_TYPE)
# Note that 'flags' is NOT the flags name; this
# is determined by a previous argument
# However, we assume that the argument will always be called 'flags'
flag_match = re.match(r'flags.(\d+)\?([\w<>.]+)', self.type)
# The type may be a flag (FLAGS.IDX?REAL_TYPE)
# FLAGS can be any name, but it should have appeared previously.
flag_match = re.match(r'(\w+).(\d+)\?([\w<>.]+)', self.type)
if flag_match:
self.is_flag = True
self.flag_index = int(flag_match.group(1))
self.flag = flag_match.group(1)
self.flag_index = int(flag_match.group(2))
# Update the type to match the exact type, not the "flagged" one
self.type = flag_match.group(2)
self.type = flag_match.group(3)
# Then check if the type is a Vector<REAL_TYPE>
vector_match = re.match(r'[Vv]ector<([\w\d.]+)>', self.type)
@@ -180,7 +178,7 @@ class TLArg:
}.get(cls, "'Type{}'".format(cls))
if self.is_vector:
result = 'List[{}]'.format(result)
if self.is_flag and cls != 'date':
if self.flag and cls != 'date':
result = 'Optional[{}]'.format(result)
return result
@@ -200,8 +198,8 @@ class TLArg:
if self.is_generic:
real_type = '!{}'.format(real_type)
if self.is_flag:
real_type = 'flags.{}?{}'.format(self.flag_index, real_type)
if self.flag:
real_type = '{}.{}?{}'.format(self.flag, self.flag_index, real_type)
return real_type
@@ -244,5 +242,5 @@ class TLArg:
self.cls[0].as_example(f, indent)
def omit_example(self):
return (self.is_flag or self.can_be_inferred) \
return (self.flag or self.can_be_inferred) \
and self.name in OMITTED_EXAMPLES

View File

@@ -72,7 +72,7 @@ class TLObject:
can be inferred will go last so they can default =None)
"""
return sorted(self.args,
key=lambda x: x.is_flag or x.can_be_inferred)
key=lambda x: bool(x.flag) or x.can_be_inferred)
def __repr__(self, ignore_id=False):
if self.id is None or ignore_id:
@@ -95,8 +95,9 @@ class TLObject:
.replace('<', ' ').replace('>', '')\
.replace('{', '').replace('}', '')
# Remove optional empty values (special-cased to the true type)
representation = re.sub(
r' \w+:flags\.\d+\?true',
r' \w+:\w+\.\d+\?true',
r'',
representation
)