Skip to content

Instantly share code, notes, and snippets.

@qi7chen
Created June 14, 2024 03:06
Show Gist options
  • Save qi7chen/db4784ba283a6248fcbd334882251b46 to your computer and use it in GitHub Desktop.
Save qi7chen/db4784ba283a6248fcbd334882251b46 to your computer and use it in GitHub Desktop.
parse_embed.py
# 解析嵌入类型的字段
def parse_one_embed(self, start: int) -> int:
all_range = []
gap_fields = []
end = start
last_elem_prefix = ''
for i in range(start, len(self.fields)):
field = self.fields[i]
prefix, idx = helper.parse_array_name_index(field.name)
if prefix != '' and idx == 0:
if last_elem_prefix == '':
start = i
end = start
if prefix != last_elem_prefix:
end += 1
last_elem_prefix = prefix
all_range.append(field.name)
gap_fields.append(copy.deepcopy(field))
else:
break
else:
if len(gap_fields) > 0:
break
if end - start < 2:
return end
gap_len = len(gap_fields)
index = 0
for i in range(end, len(self.fields), gap_len):
match_count = 0
index += 1
for j in range(gap_len):
field = self.fields[i + j]
gap_field = gap_fields[j]
expect_name = f'{gap_field.name[:-3]}[{index}]'
if field.name == expect_name and field.type_name == gap_field.type_name:
match_count += 1
else:
break
if match_count != gap_len:
break
for j in range(gap_len):
field = self.fields[i + j]
all_range.append(field.name)
end += gap_len
if len(all_range) > 0:
embed = EmbedField()
for field in gap_fields:
field.name = field.name[:-3]
field.camel_case_name = field.camel_case_name[:-3]
embed.element_fields.append(field)
embed.field_names = all_range
embed.generate_field_name()
self.embed_fields.append(embed)
return end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment