Skip to content

Commit

Permalink
Fix incorrect marker removal (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytswd committed Oct 1, 2021
1 parent cee3b7f commit e03ea99
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
49 changes: 41 additions & 8 deletions internal/file/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"

"github.com/upsidr/importer/internal/marker"
"github.com/upsidr/importer/internal/regexpplus"
)

const br = byte('\n')
Expand Down Expand Up @@ -78,20 +79,52 @@ func (f *File) RemoveMarkers() {
scanner := bufio.NewScanner(bytes.NewReader(f.ContentAfter))
for scanner.Scan() {
currentLine := scanner.Bytes()
if s := importerRe.Find(currentLine); s != nil {
// Importer Marker found, ignore
continue

if s := importerRe.Find(currentLine); len(s) != 0 {
matches, err := regexpplus.MapWithNamedSubgroupsRegexp(string(currentLine), importerRe)
if err != nil {
panic(err) // Unknown error, should not happen
}
precedingData := []byte("")
// If regexp contains importer_marker_indentation, keep that untouched.
if m, ok := matches["importer_marker_indentation"]; ok {
precedingData = []byte(m)
}

markerRemoved := importerRe.ReplaceAll(currentLine, precedingData)

// If the given line only contains marker and some spaces, simply
// remove the entire line.
if b := bytes.TrimSpace(markerRemoved); len(b) == 0 {
continue
}
currentLine = markerRemoved
}
if s := exporterRe.Find(currentLine); s != nil {
// Exporter Marker found, ignore
continue

if s := exporterRe.Find(currentLine); len(s) != 0 {
matches, err := regexpplus.MapWithNamedSubgroupsRegexp(string(currentLine), exporterRe)
if err != nil {
panic(err) // Unknown error, should not happen
}
precedingData := []byte("")
// If regexp contains export_marker_indent, keep that untouched.
if m, ok := matches["export_marker_indent"]; ok {
precedingData = []byte(m)
}

markerRemoved := exporterRe.ReplaceAll(currentLine, precedingData)

// If the given line only contains marker and some spaces, simply
// remove the entire line.
if b := bytes.TrimSpace(markerRemoved); len(b) == 0 {
continue
}
currentLine = markerRemoved
}

currentLine = append(currentLine, []byte("\n")...)
newResult = append(newResult, currentLine...)
}

f.ContentAfter = newResult

return
}
42 changes: 41 additions & 1 deletion internal/file/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ a:
a:
b:
c: data
`),
},
"yaml: importer marker is removed but the rest of the line is kept": {
file: &File{
FileName: "test-file.yaml",
ContentAfter: []byte(`
data:
- # == i: abc / begin from: some-file.yaml#[abc] ==
a:
b:
c: data
# == i: abc / end ==
`),
},
want: []byte(`
data:
-
a:
b:
c: data
`),
},
"yaml: exporter marker is removed but the rest of the line is kept": {
file: &File{
FileName: "test-file.yaml",
ContentAfter: []byte(`
data:
- # == e: abc / begin ==
a:
b:
c: data
# == i: abc / end ==
`),
},
want: []byte(`
data:
-
a:
b:
c: data
`),
},
"unknown file type: keep input as is": {
Expand Down Expand Up @@ -214,7 +254,7 @@ a:
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tc.file.RemoveMarkers()
if diff := cmp.Diff(tc.want, tc.file.ContentAfter); diff != "" {
if diff := cmp.Diff(string(tc.want), string(tc.file.ContentAfter)); diff != "" {
t.Errorf("parsed result didn't match (-want / +got)\n%s", diff)
}
})
Expand Down
2 changes: 1 addition & 1 deletion internal/marker/marker_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ var (
// # == export: random_data / begin ==
// random-data: this is exported
// # == export: random_data / end ==
ExporterMarkerYAML = `(?P<export_marker_indent>\s*)# == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
ExporterMarkerYAML = `(?P<export_marker_indent>.*)# == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
)
13 changes: 13 additions & 0 deletions internal/regexpplus/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ var (
// create a map that will not have all the matched components.
func MapWithNamedSubgroups(targetLine string, expression string) (map[string]string, error) {
re := regexp.MustCompile(expression)
return MapWithNamedSubgroupsRegexp(targetLine, re)
}

// MapWithNamedSubgroupsRegexp runs regexp FindStringSubmatch against
// `targetLine` input, and returns a map representation. The map contains the
// key as the subgroup name, and value for the matched data.
//
// If there is no match found, an error of ErrNoMatch will be returned.
//
// This can be used for regular expression which does not have any subgroup,
// but as it is designed specifically for subgroup based use cases, it will
// create a map that will not have all the matched components.
func MapWithNamedSubgroupsRegexp(targetLine string, re *regexp.Regexp) (map[string]string, error) {
ms := re.FindStringSubmatch(targetLine)

if len(ms) == 0 {
Expand Down

0 comments on commit e03ea99

Please sign in to comment.