Distinguish between vector type and constructor (fix #81)

This commit is contained in:
Lonami Exo 2017-05-19 09:41:22 +02:00
parent b84641023a
commit c4447bf226
2 changed files with 19 additions and 6 deletions

View File

@ -159,8 +159,14 @@ class TLArg:
vector_match = re.match(r'vector<(\w+)>', self.type, re.IGNORECASE) vector_match = re.match(r'vector<(\w+)>', self.type, re.IGNORECASE)
if vector_match: if vector_match:
self.is_vector = True self.is_vector = True
self.type = vector_match.group(
1) # Update the type to match the one inside the vector # If the type's first letter is not uppercase, then
# it is a constructor and we use (read/write) its ID
# as pinpointed on issue #81.
self.use_vector_id = self.type[0] == 'V'
# Update the type to match the one inside the vector
self.type = vector_match.group(1)
# The name may contain "date" in it, if this is the case and the type is "int", # The name may contain "date" in it, if this is the case and the type is "int",
# we can safely assume that this should be treated as a "date" object. # we can safely assume that this should be treated as a "date" object.
@ -179,7 +185,10 @@ class TLArg:
real_type = '#' real_type = '#'
if self.is_vector: if self.is_vector:
if self.use_vector_id:
real_type = 'Vector<{}>'.format(real_type) real_type = 'Vector<{}>'.format(real_type)
else:
real_type = 'vector<{}>'.format(real_type)
if self.is_generic: if self.is_generic:
real_type = '!{}'.format(real_type) real_type = '!{}'.format(real_type)

View File

@ -311,8 +311,10 @@ class TLGenerator:
builder.writeln('if {}:'.format(name)) builder.writeln('if {}:'.format(name))
if arg.is_vector: if arg.is_vector:
if arg.use_vector_id:
builder.writeln( builder.writeln(
"writer.write_int(0x1cb5c415, signed=False) # Vector's constructor ID") "writer.write_int(0x1cb5c415, signed=False) # Vector's constructor ID")
builder.writeln('writer.write_int(len({}))'.format(name)) builder.writeln('writer.write_int(len({}))'.format(name))
builder.writeln('for {}_item in {}:'.format(arg.name, name)) builder.writeln('for {}_item in {}:'.format(arg.name, name))
# Temporary disable .is_vector, not to enter this if again # Temporary disable .is_vector, not to enter this if again
@ -405,7 +407,9 @@ class TLGenerator:
arg.is_flag = False arg.is_flag = False
if arg.is_vector: if arg.is_vector:
if arg.use_vector_id:
builder.writeln("reader.read_int() # Vector's constructor ID") builder.writeln("reader.read_int() # Vector's constructor ID")
builder.writeln('{} = [] # Initialize an empty list'.format(name)) builder.writeln('{} = [] # Initialize an empty list'.format(name))
builder.writeln('{}_len = reader.read_int()'.format(arg.name)) builder.writeln('{}_len = reader.read_int()'.format(arg.name))
builder.writeln('for _ in range({}_len):'.format(arg.name)) builder.writeln('for _ in range({}_len):'.format(arg.name))