Skip to content

Commit e03ea99

Browse files
authored
Fix incorrect marker removal (#64)
1 parent cee3b7f commit e03ea99

File tree

4 files changed

+96
-10
lines changed

4 files changed

+96
-10
lines changed

internal/file/process.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99

1010
"github.com/upsidr/importer/internal/marker"
11+
"github.com/upsidr/importer/internal/regexpplus"
1112
)
1213

1314
const br = byte('\n')
@@ -78,20 +79,52 @@ func (f *File) RemoveMarkers() {
7879
scanner := bufio.NewScanner(bytes.NewReader(f.ContentAfter))
7980
for scanner.Scan() {
8081
currentLine := scanner.Bytes()
81-
if s := importerRe.Find(currentLine); s != nil {
82-
// Importer Marker found, ignore
83-
continue
82+
83+
if s := importerRe.Find(currentLine); len(s) != 0 {
84+
matches, err := regexpplus.MapWithNamedSubgroupsRegexp(string(currentLine), importerRe)
85+
if err != nil {
86+
panic(err) // Unknown error, should not happen
87+
}
88+
precedingData := []byte("")
89+
// If regexp contains importer_marker_indentation, keep that untouched.
90+
if m, ok := matches["importer_marker_indentation"]; ok {
91+
precedingData = []byte(m)
92+
}
93+
94+
markerRemoved := importerRe.ReplaceAll(currentLine, precedingData)
95+
96+
// If the given line only contains marker and some spaces, simply
97+
// remove the entire line.
98+
if b := bytes.TrimSpace(markerRemoved); len(b) == 0 {
99+
continue
100+
}
101+
currentLine = markerRemoved
84102
}
85-
if s := exporterRe.Find(currentLine); s != nil {
86-
// Exporter Marker found, ignore
87-
continue
103+
104+
if s := exporterRe.Find(currentLine); len(s) != 0 {
105+
matches, err := regexpplus.MapWithNamedSubgroupsRegexp(string(currentLine), exporterRe)
106+
if err != nil {
107+
panic(err) // Unknown error, should not happen
108+
}
109+
precedingData := []byte("")
110+
// If regexp contains export_marker_indent, keep that untouched.
111+
if m, ok := matches["export_marker_indent"]; ok {
112+
precedingData = []byte(m)
113+
}
114+
115+
markerRemoved := exporterRe.ReplaceAll(currentLine, precedingData)
116+
117+
// If the given line only contains marker and some spaces, simply
118+
// remove the entire line.
119+
if b := bytes.TrimSpace(markerRemoved); len(b) == 0 {
120+
continue
121+
}
122+
currentLine = markerRemoved
88123
}
89124

90125
currentLine = append(currentLine, []byte("\n")...)
91126
newResult = append(newResult, currentLine...)
92127
}
93128

94129
f.ContentAfter = newResult
95-
96-
return
97130
}

internal/file/process_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ a:
184184
a:
185185
b:
186186
c: data
187+
`),
188+
},
189+
"yaml: importer marker is removed but the rest of the line is kept": {
190+
file: &File{
191+
FileName: "test-file.yaml",
192+
ContentAfter: []byte(`
193+
data:
194+
- # == i: abc / begin from: some-file.yaml#[abc] ==
195+
a:
196+
b:
197+
c: data
198+
# == i: abc / end ==
199+
`),
200+
},
201+
want: []byte(`
202+
data:
203+
-
204+
a:
205+
b:
206+
c: data
207+
`),
208+
},
209+
"yaml: exporter marker is removed but the rest of the line is kept": {
210+
file: &File{
211+
FileName: "test-file.yaml",
212+
ContentAfter: []byte(`
213+
data:
214+
- # == e: abc / begin ==
215+
a:
216+
b:
217+
c: data
218+
# == i: abc / end ==
219+
`),
220+
},
221+
want: []byte(`
222+
data:
223+
-
224+
a:
225+
b:
226+
c: data
187227
`),
188228
},
189229
"unknown file type: keep input as is": {
@@ -214,7 +254,7 @@ a:
214254
for name, tc := range cases {
215255
t.Run(name, func(t *testing.T) {
216256
tc.file.RemoveMarkers()
217-
if diff := cmp.Diff(tc.want, tc.file.ContentAfter); diff != "" {
257+
if diff := cmp.Diff(string(tc.want), string(tc.file.ContentAfter)); diff != "" {
218258
t.Errorf("parsed result didn't match (-want / +got)\n%s", diff)
219259
}
220260
})

internal/marker/marker_regex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ var (
4444
// # == export: random_data / begin ==
4545
// random-data: this is exported
4646
// # == export: random_data / end ==
47-
ExporterMarkerYAML = `(?P<export_marker_indent>\s*)# == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
47+
ExporterMarkerYAML = `(?P<export_marker_indent>.*)# == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
4848
)

internal/regexpplus/map.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ var (
2121
// create a map that will not have all the matched components.
2222
func MapWithNamedSubgroups(targetLine string, expression string) (map[string]string, error) {
2323
re := regexp.MustCompile(expression)
24+
return MapWithNamedSubgroupsRegexp(targetLine, re)
25+
}
26+
27+
// MapWithNamedSubgroupsRegexp runs regexp FindStringSubmatch against
28+
// `targetLine` input, and returns a map representation. The map contains the
29+
// key as the subgroup name, and value for the matched data.
30+
//
31+
// If there is no match found, an error of ErrNoMatch will be returned.
32+
//
33+
// This can be used for regular expression which does not have any subgroup,
34+
// but as it is designed specifically for subgroup based use cases, it will
35+
// create a map that will not have all the matched components.
36+
func MapWithNamedSubgroupsRegexp(targetLine string, re *regexp.Regexp) (map[string]string, error) {
2437
ms := re.FindStringSubmatch(targetLine)
2538

2639
if len(ms) == 0 {

0 commit comments

Comments
 (0)