Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ All notable changes to GoTTP will be documented in this file.
### Fixed
- **`strip`/`lstrip`/`rstrip` characters argument**: `strip("chars")` ignored its argument and only removed whitespace, and `lstrip`/`rstrip` weren't registered, so all three returned the value unchanged. They now behave like Python's `str.strip/lstrip/rstrip(chars)`: the argument is a set of characters removed from both ends, the start or the end, and without an argument they remove whitespace. The `chain()` fallback does the same. A quoted argument made only of the other quote character (`strip('"')`) also reached the function as an empty string; quote characters inside a quoted argument are now kept. (#35)

### Changed (breaking: matching)
- **Indented template lines match the exact indent**: a template line's leading whitespace was turned into `[ \t]+`, so a line indented by two spaces also matched data indented by one space, four spaces or a tab. Python TTP rebuilds the indent verbatim, and gottp now does the same: two spaces match exactly two spaces, and a tab matches only a tab. Unindented lines are unchanged, and so is whitespace inside a line, which still matches any run of spaces and tabs. Templates that relied on the loose match need their indent to match the data. Templates saved with `SaveCompiledTemplate` keep the regex they were compiled with until they are compiled again. (#30)

## [v0.1.13] - 2026-09-03

### Fixed
Expand Down
Binary file modified editor/gottp.wasm
Binary file not shown.
Binary file modified editor/js/gottp.wasm
Binary file not shown.
20 changes: 10 additions & 10 deletions internal/pattern/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ func (e *Engine) GenerateRegexFromVariables(line string, variables []*MatchVaria
}
}

// Python TTP rebuilds the line's leading whitespace verbatim
// ("\n" + indent + ...), so an indented line matches only data with
// exactly that indent (#30). Emit it literally (spaces and tabs need no
// escaping) and build the rest of the regex from the unindented line.
indentLen := len(line) - len(strings.TrimLeft(line, " \t"))
indent := line[:indentLen]
line = line[indentLen:]

// Find variable positions
re := regexp.MustCompile(`\{\{([\S\s]+?)\}\}`)
matches := re.FindAllStringSubmatchIndex(line, -1)
Expand All @@ -483,7 +491,7 @@ func (e *Engine) GenerateRegexFromVariables(line string, variables []*MatchVaria
}

// Build regex by replacing variables and escaping text
var regexParts []string
regexParts := []string{indent}
lastEnd := 0

for i, match := range matches {
Expand Down Expand Up @@ -543,15 +551,7 @@ func (e *Engine) GenerateRegexFromVariables(line string, variables []*MatchVaria
// If the original line has leading spaces, we need to account for them in the regex
// Python TTP preserves leading spaces in the pattern
if !strings.HasPrefix(regexStr, "^") {
// Check if the original line has leading spaces
leadingSpaces := len(line) - len(strings.TrimLeft(line, " \t"))
if leadingSpaces > 0 && len(regexParts) > 0 {
// The first part should already have the leading spaces from escapeText
// But we need to ensure the pattern starts with ^ to anchor to line start
regexStr = "^" + regexStr
} else {
regexStr = "^" + regexStr
}
regexStr = "^" + regexStr
}

// Handle trailing whitespace: Python TTP strips it and adds optional matching
Expand Down
48 changes: 48 additions & 0 deletions internal/pattern/indent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pattern

import "testing"

// Issue #30: Python TTP rebuilds a template line's leading whitespace
// verbatim ("\n" + indent + ...), so an indented template line matches
// data lines with exactly that indent: not deeper, not shallower, and a
// tab only where the template has a tab.
func TestIndentMatchesExactly(t *testing.T) {
cases := []struct {
tmpl string
data string
match bool
}{
{" {{ v }}", " a", true},
{" {{ v }}", " a", false},
{" {{ v }}", " a", false},
{" {{ v }}", "\ta", false},
{" {{ v }}", "a", false},
{"\t{{ v }}", "\ta", true},
{"\t{{ v }}", " a", false},
{" x {{ y }} z", " x y z", true},
{" x {{ y }} z", " x y z", false},
{" {{ x | _start_ }}", " a", true},
{" {{ x | _start_ }}", " a", false},
{" {{ x | _line_ }}", " a b", true},
{" {{ x | _line_ }}", "a b", false},
{" literal {{ ignore }}", " literal a", true},
{" literal {{ ignore }}", " literal a", false},
// A standalone {{ _start_ }}/{{ _end_ }} still matches any blank line:
// the runtime right-trims data lines, so a whitespace-only line
// arrives as "" and has no indent to check.
{" {{ _start_ }}", "", true},
// Unindented lines are unchanged.
{"{{ v }}", "a", true},
{"H {{ _start_ }}", "H", true},
}
e := NewEngine()
for _, tc := range cases {
cp, err := e.CompilePattern(tc.tmpl, false, false)
if err != nil {
t.Fatalf("CompilePattern(%q): %v", tc.tmpl, err)
}
if got := cp.Regex.MatchString(tc.data); got != tc.match {
t.Errorf("template %q (regex %s) on %q: match = %v, want %v", tc.tmpl, cp.Regex, tc.data, got, tc.match)
}
}
}
18 changes: 10 additions & 8 deletions test/ttpsuite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ go test ./test/ttpsuite -v | grep -E 'suite:|repros:|parity:'
```

```
TTP 0.10.1 suite: pass 48 / skipped-by-issue 148 / design 4 of 200
gottp repros: pass 2 / skipped-by-issue 8 / design 0 of 10
TTP 0.10.1 suite: pass 49 / skipped-by-issue 147 / design 4 of 200
gottp repros: pass 3 / skipped-by-issue 7 / design 0 of 10
source-map parity: same 199 / diverge 11 of 210
```

Expand All @@ -36,12 +36,12 @@ The Go tests need no Python, so they run in plain `go test ./...` and in CI. The
|---|---|
| `issue28_parent_start_line_without_variables` | #28 |
| `issue29_joinmatches_on_start_line` | #29 |
| `issue30_indented_line_matches_any_indentation` | #30 |
| `issue30_indented_line_matches_any_indentation` | passes; #30 is fixed |
| `issue31_ignore_vars_name` | #31 |
| `issue32_one_line_fills_several_template_lines` | #32 |
| `issue33_trailing_ignore_with_trailing_whitespace` | #33 |
| `issue34_split_joinmatches` | #34 |
| `issue35_strip_characters_argument` | passes; #35 is fixed |
| `issue35_strip_characters_argument` | #35 |
| `issue36_unknown_function_falls_back_to_str_method` | #36 |
| `sourcemap_void_group` | passes; covers `void` for source-map parity (#108) |

Expand Down Expand Up @@ -79,11 +79,10 @@ Cases per issue (first strict difference), and how many more cases hit the issue
| Issue | Cases | Also | Title |
|---|---|---|---|
| #68 | 13 | | _headers_ (and columns) indicator is unimplemented |
| #28 | 12 | | Parent group whose _start_ line has no variables drops its child groups |
| #67 | 10 | | Raw-string arguments r"…" silently match nothing |
| #28 | 9 | | Parent group whose _start_ line has no variables drops its child groups |
| #63 | 9 | | set(True) / set(False) (and any literal argument) is a string |
| #80 | 9 | 2 | Missing output features |
| #63 | 8 | | set(True) / set(False) (and any literal argument) is a string |
| #30 | 7 | | Indented template lines match data at any indentation |
| #64 | 6 | | A variable with default() captures the leading space |
| #74 | 6 | | <extend> inside a <group> is ignored |
| #93 | 6 | | <extend>: <vars> from the extended file and the groups=/vars=/inputs=/lookups= filters are not applied |
Expand All @@ -93,14 +92,14 @@ Cases per issue (first strict difference), and how many more cases hit the issue
| #88 | 4 | | itemize on a child group drops its values, and the path argument is ignored |
| #97 | 4 | | A group macro that returns None loses its in-place changes, and the next chained macro gets None |
| #110 | 4 | | Group-specific output= results are merged into the main result instead of added as a separate item |
| #100 | 4 | | Nested-section templates from TTP answers diverge; root cause not isolated |
| #112 | 4 | | A chain stored in <vars> as a list is ignored (group chain= and chain()) |
| #69 | 3 | | A _start_ line rejected by a condition doesn't close the previous record |
| #73 | 3 | 1 | Getters: gethostname through set(), and get_date/get_time/getfilename/get_timestamp* missing |
| #79 | 3 | | _line_ with joinmatches also captures the _start_ line |
| #91 | 3 | 1 | A group with no matching lines does not emit its default values |
| #94 | 3 | | <input url=...> pointing at a directory loads nothing |
| #99 | 3 | | record()/set= across groups: the recorded value is not set and the first child record is lost |
| #100 | 3 | | Nested-section templates from TTP answers diverge; root cause not isolated |
| #72 | 2 | | Group to_int has no float fallback |
| #76 | 2 | | A dynamic-path value containing . is split into path segments |
| #77 | 2 | | Nested <template> children are merged into one result |
Expand All @@ -116,6 +115,9 @@ Cases per issue (first strict difference), and how many more cases hit the issue
| #101 | 1 | 1 | <output> with its settings in the tag body is ignored |
| #102 | 1 | | format_attributes is ignored (tabulate tablefmt) |
| #114 | 1 | | Group validate= attribute is not applied |
| #118 | 1 | 1 | A joinmatches line keeps collecting after a sibling group's _start_ |
| #119 | 1 | | A group's second _start_ line matches lines that belong to a sibling group |
| #120 | 1 | | Sibling groups with the same name: the second replaces the first instead of forming a list |

#62 (top-level anonymous group loses one list level) was the first difference in 65 cases. Its fix made 26 of them pass; the other 39 are listed under the issue they hit next, which for 11 of them is one of four new issues (#110, #112, #113, #114).

Expand Down
39 changes: 17 additions & 22 deletions test/ttpsuite/known_failures.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"issue": 29,
"reason": "joinmatches on a _start_ line merges consecutive records into one"
},
"gottp-repros/issue30_indented_line_matches_any_indentation": {
"issue": 30,
"reason": "indented template line matches data at any indentation"
},
"gottp-repros/issue31_ignore_vars_name": {
"issue": 31,
"reason": "ignore(\"<vars name>\") empties the group"
Expand Down Expand Up @@ -89,12 +85,15 @@
"reason": "parent group whose start line is \"service {{ ignore }}\" drops its child groups (the macro also uses Python \"import re\")"
},
"test_answers_and_docs/test_github_issue_42": {
"issue": 30,
"reason": "indented template lines also match the \"!\" lines at other indents"
"issue": 120,
"also": [
118
],
"reason": "sibling groups named route-targets: the second replaces the first instead of forming a list"
},
"test_answers_and_docs/test_github_issue_42_answer": {
"issue": 30,
"reason": "indented template lines also match lines at other indents"
"issue": 119,
"reason": "export_rts's second _start_ line also matches the import route-target lines"
},
"test_answers_and_docs/test_interface_template_not_collecting_all_data_reduced": {
"issue": 89,
Expand All @@ -113,16 +112,16 @@
"reason": "to_ip does not accept \"address netmask\""
},
"test_answers_and_docs/test_issue_45": {
"issue": 30,
"reason": "indented template lines match data at any indentation"
"issue": 28,
"reason": "forwarding-options { {{ _start_ }} has no variables, so its child groups are dropped"
},
"test_answers_and_docs/test_issue_45_1": {
"issue": 30,
"reason": "indented template lines match data at any indentation"
"issue": 28,
"reason": "forwarding-options { {{ _start_ }} has no variables, so its child groups are dropped"
},
"test_answers_and_docs/test_issue_45_filtering_fix": {
"issue": 30,
"reason": "indented template lines match data at any indentation"
"issue": 28,
"reason": "forwarding-options { {{ _start_ }} has no variables, so its child groups are dropped"
},
"test_answers_and_docs/test_issue_45_for_junos_cfg": {
"issue": 63,
Expand All @@ -136,10 +135,6 @@
"issue": 112,
"reason": "chain(var) naming a <vars> list is ignored"
},
"test_answers_and_docs/test_issue_50": {
"issue": 30,
"reason": "indented template lines also match the deeper ipv6 address lines"
},
"test_answers_and_docs/test_issue_52_answer": {
"issue": 79,
"reason": "_line_ with joinmatches also captures the _start_ line"
Expand Down Expand Up @@ -188,8 +183,8 @@
"reason": "lines owned by a void child group are also collected by the parent; root cause not isolated"
},
"test_answers_and_docs/test_slack_answer_2": {
"issue": 100,
"reason": "nested port/ethernet sections lose captures; root cause not isolated"
"issue": 63,
"reason": "set(true) gives the string \"true\""
},
"test_answers_and_docs/test_slack_answer_3": {
"issue": 100,
Expand Down Expand Up @@ -366,8 +361,8 @@
"reason": "group-level lookup= attribute is ignored"
},
"test_group_name_attribute/test_group_same_null_path_for_several_groups": {
"issue": 30,
"reason": "indented template lines also match the \"!\" lines at other indents"
"issue": 118,
"reason": "the import joinmatches line keeps collecting after the export group starts"
},
"test_headers_indicator/test_headers_docs_example": {
"issue": 68,
Expand Down
Loading