ttp icon indicating copy to clipboard operation
ttp copied to clipboard

Group functions filter out tail match results

Open dmulyalin opened this issue 2 years ago • 2 comments

When having this template and data:

data = """
interface Gig123
 description foo
 mtu 1234
 ipv4 address 1.2.3.4 255.255.255.0
 arp timeout 123
 mac-address 123.123.1
!
interface Gig321
 description foo
 mtu 4321
 ipv4 address 1.2.3.5 255.255.255.0
 arp-timeout 4321
 mac-address 123.123.2
!
"""
template = """
<group name="interfaces*" contains="name">
interface {{ name }}
 description {{ description | re(".*") }}
 mtu {{ mtu | to_int }}
 mac-address {{ mac_address }}
 arp timeout {{ arp_timeout }}
 
 <group name="ipv4*" method="table">
 ipv4 address {{ ip | _exact_ }} {{ mask }}
 </group>
 
!{{ _end_ }}
</group>
"""

Parsing gives these results:

[[{'interfaces': [{'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.4', 'mask': '255.255.255.0'}],
                   'mtu': 1234,
                   'name': 'Gig123'},
                  {'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.5', 'mask': '255.255.255.0'}],
                   'mtu': 4321,
                   'name': 'Gig321'}]}]]

this is due to the fact that mac_address matched after ipv4 group matched, and by the time we process mac_address match group functions results does not have name attribute in them. Removing contains="name" produces expected result:

[[{'interfaces': [{'arp_timeout': '123',
                   'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.4', 'mask': '255.255.255.0'}],
                   'mac_address': '123.123.1',
                   'mtu': 1234,
                   'name': 'Gig123'},
                  {'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.5', 'mask': '255.255.255.0'}],
                   'mac_address': '123.123.2',
                   'mtu': 4321,
                   'name': 'Gig321'}]}]]

dmulyalin avatar Apr 14 '23 23:04 dmulyalin

Introduced fix in latest commit, fix logic is weak but simplest to fix this issue, better approach required to collect parsing results in general but will require significant code refactoring.

dmulyalin avatar Apr 14 '23 23:04 dmulyalin

@dmulyalin first of all thank you so much for this project, it is amazing.

Unfortunately, this bug is still not fixed even in version 0.9.5. I can provide the minimal example to reproduce if needed.

ffhan avatar Jun 28 '23 10:06 ffhan