dmulyalin / ttp

Template Text Parser
MIT License
348 stars 34 forks source link

joinmatches doesn't work with embedded groups #69

Open haowangapple opened 2 years ago

haowangapple commented 2 years ago

joinmatches() doesn't work when the lines it matched appear after some other lines that are matched by an embedded group. This is similar to the following issue: https://github.com/dmulyalin/ttp/issues/65

Example that is working:

<input load="text">
snmp-server chassis-id test
snmp-server enable traps bgp
snmp-server enable traps entity
snmp-server host 2.2.2.2 version 2c public
snmp-server host 1.1.1.1 version 2c public
</input>

<group name="snmp.config" expand="" >
snmp-server chassis-id {{ chassis_id }}
<group name="servers*" expand="" >
snmp-server host {{ server }} version {{ version }} {{ community }}
</group>
snmp-server enable traps {{ traps | joinmatches(',') | to_list }}
</group>

<output returner="terminal" format="json" />

It generates the following output, which is expected:

[
    {
        "snmp": {
            "config": {
                "chassis_id": "test",
                "servers": [
                    {
                        "community": "public",
                        "server": "2.2.2.2",
                        "version": "2c"
                    },
                    {
                        "community": "public",
                        "server": "1.1.1.1",
                        "version": "2c"
                    }
                ],
                "traps": [
                    "bgp",
                    "entity"
                ]
            }
        }
    }
]

However, if we move

snmp-server host 2.2.2.2 version 2c public
snmp-server host 1.1.1.1 version 2c public

above

snmp-server enable traps bgp
snmp-server enable traps entity

so that the embedded group "servers*" will match lines first, then the later joinmatches() does not work any more. Here is the example code that show this:

<input load="text">
snmp-server chassis-id test
snmp-server host 2.2.2.2 version 2c public
snmp-server host 1.1.1.1 version 2c public
snmp-server enable traps bgp
snmp-server enable traps entity
</input>

<group name="snmp.config" expand="" >
snmp-server chassis-id {{ chassis_id }}
<group name="servers*" expand="" >
snmp-server host {{ server }} version {{ version }} {{ community }}
</group>
snmp-server enable traps {{ traps | joinmatches(',') | to_list }}
</group>

<output returner="terminal" format="json" />

And it generates following result, which doesn't include the joinmatches:

[
    {
        "snmp": {
            "config": {
                "chassis_id": "test",
                "servers": [
                    {
                        "community": "public",
                        "server": "2.2.2.2",
                        "version": "2c"
                    },
                    {
                        "community": "public",
                        "server": "1.1.1.1",
                        "version": "2c"
                    }
                ]
            }
        }
    }
]
dmulyalin commented 2 years ago

Added fix for this issue in latest commit 16c98a5d02ced53869d38af73922057d24fd86e3, this is the test content - https://github.com/dmulyalin/ttp/blob/16c98a5d02ced53869d38af73922057d24fd86e3/test/pytest/test_answers_and_docs.py#L7145 - feel free to review or install from master and test for your environment, let me know how it goes.