Skip to content

fix: give top-level anonymous groups Python TTP's result shape (#62) - #115

Merged
roc-ops merged 1 commit into
mainfrom
fix/62-anonymous-group-level
Nov 28, 2026
Merged

roc-ops merged 1 commit into
mainfrom
fix/62-anonymous-group-level

Conversation

@roc-ops

@roc-ops roc-ops commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

A top-level anonymous group (an unnamed <group>, or a template with no group tags) gave a result one list level short, dropped every input after the first, and lost named groups next to it. Each input's result is now built the way Python TTP's template.form_results builds it (ttp/ttp.py:989-1003).

                    before (gottp main)              after = Python TTP 0.10.1
issue repro         [[{A},{B}]]                      [[[{A},{B}]]]
2 inputs            [[{A},{B}]]          (C lost)    [[[{A},{B}],[{C}]]]
anon + named group  [[{A},{B}]]          (h lost)    [[[{A},{B},{h:{host:X}}]]]

per input:  _anonymous_ matches ──► the input's result
            other top-level results ──► appended as one more item
            every input ──► keeps its own entry

What changed:

  • formResults (internal/compiled/runtime.go): a port of form_results, used by both Runtime.Parse and Runtime.ParseWithSourceMap for per_input and per_template. It replaces the _anonymous_list + goto skipResultsListBuild code in both paths. The two paths are still separate, so this is not the later refactor. The source-map per_template path had no anonymous handling at all and returned {"_anonymous_": [...]}.
  • Implicit group name (internal/parser/template.go): a template with no group tags is now the group _anonymous_, as in TTP (ttp/ttp.py:1388-1394). So one match gives a dict ([{A}]) and an unnamed <group> always gives a list ([[{A}]]). Both match TTP. ParseStream still reports groupPath "" for it (internal/compiler/streamability.go).
  • Table formatter (processOutputFunctions): a per-input list now counts as table rows, so the anonymous matches still make one table.
  • test/comparison is strict. normalizeJSON unwrapped every one-item list holding a list (research §3.4), which hid this bug. The Go result is now wrapped like result() and compared with list levels included. The 9 tests that still differ only by a list level are listed in a stale-proof looseListLevels table: 8 for TTP compat: Formatter output from <output> gets an extra list level #98 (formatter output gets an extra list level) and 1 for the new TTP compat: functions="itemize(...)" flattens a list-valued key #111 (functions="itemize(...)" flattens a list value). Those 9 are still compared loosely, and a listed test that starts matching strictly fails. With a strict comparison, main fails 17 tests; this branch fails only those 9.
  • ParseStream doesn't build per-input lists, so it never had this shape bug. It has a new test that the implicit group still streams with an empty groupPath.

Evidence

Regression tests (written first; red on main, green here):

  • test/anonymous_group_test.go TestAnonymousGroupResultShape: 18 shapes × {source map off, on}, each expected value recorded from Python TTP 0.10.1. Covers implicit vs unnamed vs name="_anonymous_", 0/1/2 matches, 2 inputs, an input with no match, anonymous + named groups, two unnamed groups, nested child groups, and per_template (also with a named group). On main, 28 of the 36 subtests fail, 4 pass and 4 are skipped (the multi-input source-map subtests, for Source map: a group without input= parses only Default_Input (other inputs give an empty result) #105).
  • parsestream_test.go TestParseStream_ImplicitGroupHasEmptyGroupPath.

NewParser().Result():

                           main                          this branch                                  Python TTP 0.10.1
issue repro                [[{"name":"A"},{"name":"B"}]]  [[[{"name":"A"},{"name":"B"}]]]               [[[{"name":"A"},{"name":"B"}]]]
unnamed <group>, 2 inputs  [[{"name":"A"},{"name":"B"}]]  [[[{"name":"A"},{"name":"B"}],[{"name":"C"}]]] [[[{"name":"A"},{"name":"B"}],[{"name":"C"}]]]
anon + named group         [[{"name":"A"},{"name":"B"}]]  [[[{"name":"A"},{"name":"B"},{"h":{"host":"X"}}]]] (same)

csv cascade (test_output_formatters/test_csv_formatter_simple):

main:   [["\"interface\",\"vlan\"\n\"Port-Chanel11\",\"10\"", "\"interface\",\"vlan\"\n\"Loopback0\",\"20\""]]   one csv per match
branch: [["\"interface\",\"vlan\"\n\"Port-Chanel11\",\"10\"\n\"Loopback0\",\"20\""]]                            one csv, all rows
TTP:     ["\"interface\",\"vlan\"\n\"Port-Chanel11\",\"10\"\n\"Loopback0\",\"20\""]                             (remaining difference: #98)

test/ttpsuite:

before: TTP 0.10.1 suite: pass 22 / skipped-by-issue 174 / design 4 of 200
after:  TTP 0.10.1 suite: pass 48 / skipped-by-issue 148 / design 4 of 200
        gottp repros: pass 1 / skipped-by-issue 9 / design 0 of 10   (unchanged)
        source-map parity: same 199 / diverge 11 of 210              (unchanged)

26 of the 65 #62 cases pass and were removed from known_failures.json. The other 39 are re-listed under the issue they hit next. Most of those issues were already filed (#63, #64, #67, #69, #70, #72, #79, #97, #98, #99, …). For 11 cases the next difference was untracked, so I filed #110 (group-specific output= results), #112 (a chain stored as a <vars> list), #113 (macro() inside group chain=) and #114 (group validate=). The README's counts, per-issue table and macro section are updated. sourcemap_divergences.json needed no change.

Checks run locally: go vet ./..., go test ./..., go test -race (all but test/memory), with GOTTP_PYTHON set to TTP 0.10.1 for test/comparison; staticcheck@2025.1.1 ./... (clean); GOOS=js GOARCH=wasm go build ./....

Merge Danger

This changes the public result shape for every template whose top-level group is anonymous. Templates where every top-level group is named are unaffected.

  • NewParser().Result() / CompiledTemplate.Parse: one more list level (result[0] is now the input's list of matches, not a match). Inputs after the first and named groups beside the anonymous group now appear where they used to be dropped. Single-match unnamed <group> now gives [[{...}]] instead of [{...}].
  • per_template + EnableSourceMap: was {"_anonymous_": [...]}, now the list of matches.
  • Source maps: MatchMapping.GroupName for the implicit group of a tagless template is now "_anonymous_" (was ""). The ResultPath fallback for that group is _anonymous_ (was _anonymous_*). ParseStream's groupPath is unchanged ("").
  • In this repo: two tests that read result[0] as a dict (test/joinmatches_test.go, test/match_condition_test.go) now read result[0][0]. examples/ output is unchanged (they use named groups). The editor's source-map navigation keys on GroupName/ResultPath, and its result view will show the extra level for anonymous templates. It has no anonymous-specific code, but click-through on anonymous-template results wasn't tested here. docs/.../Anonymous group.rst documents the new top-level shape, and there's a breaking-change entry under [Unreleased] in CHANGELOG.md.
  • Outside the repo: any consumer that parses tagless or unnamed-group templates and indexes the result. The Data Harbor gottp CLI path (ConvertJSONToNotifications) should be checked for templates without named top-level groups before rollout. Anything matching the old shape would silently read the wrong level, or only the first input.

🤖 Generated with Claude Code

Fixes #62

A template whose top-level group has no name (an unnamed <group>, or a
template with no group tags) used its list of matches as the whole
per-input result list: the result was one list level short, every input
after the first was dropped, and named groups beside it were lost.

Build each input's result the way TTP's template.form_results does
(ttp/ttp.py:989-1003): the _anonymous_ matches are the input's result,
the rest of that input's results is appended as one more item, and each
input keeps its own entry. Both Runtime.Parse and ParseWithSourceMap use
the new formResults helper, for per_input and per_template (the
source-map per_template path kept a literal _anonymous_ key).

Name the implicit group of a tagless template "_anonymous_", as TTP
does, so one match gives a dict while an unnamed <group> always gives a
list. ParseStream keeps an empty groupPath for it.

The table formatter now also combines per-input lists into one table.
test/comparison compares strictly (Go result wrapped like result(), no
single-item-list unwrapping); the 9 tests that still differ only by a
list level are listed in looseListLevels with #98 and #111.

TTP 0.10.1 suite: pass 22 -> 48 of 200. The 39 ex-#62 cases that still
fail are listed under the issue they hit next (new: #110, #112-#114).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@roc-ops roc-ops added this to the Baseline v0.1.14 milestone Nov 28, 2026
@roc-ops
roc-ops merged commit 291675a into main Nov 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TTP compat: Top-level anonymous group loses one list level (and drops other inputs)

1 participant