Skip to content

Commit 4f39a73

Browse files
committed
feat(fs): add MultiFileReadFS.AttachOrReplace
1 parent 3fd1684 commit 4f39a73

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

errgoengine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
3838
// initial context data extraction
3939
contextData := NewContextData(e.SharedStore, workingPath)
4040
contextData.Analyzer = template.Language.AnalyzerFactory(contextData)
41-
e.FS.Attach(template.Language.stubFs, 1)
41+
e.FS.AttachOrReplace(template.Language.stubFs, 1)
4242

4343
groupNames := template.Pattern.SubexpNames()
4444
for _, submatches := range template.Pattern.FindAllStringSubmatch(msg, -1) {

fs.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (mfs *MultiReadFileFS) LastAttachedIdx() int {
1515
return len(mfs.FSs) - 1
1616
}
1717

18-
func (mfs *MultiReadFileFS) Attach(fs fs.ReadFileFS, idx int) {
18+
func (mfs *MultiReadFileFS) AttachOrReplace(fs fs.ReadFileFS, idx int) {
1919
if idx < len(mfs.FSs) && idx >= 0 {
2020
if mfs.FSs[idx] == fs {
2121
return
@@ -25,14 +25,29 @@ func (mfs *MultiReadFileFS) Attach(fs fs.ReadFileFS, idx int) {
2525
return
2626
}
2727

28+
mfs.Attach(fs, idx)
29+
}
30+
31+
func (mfs *MultiReadFileFS) Attach(instance fs.ReadFileFS, idx int) {
32+
if idx < len(mfs.FSs) && idx >= 0 {
33+
if mfs.FSs[idx] == instance {
34+
return
35+
}
36+
37+
mfs.FSs = append(
38+
append(append([]fs.ReadFileFS{}, mfs.FSs[:idx]...), instance),
39+
mfs.FSs[idx+1:]...)
40+
return
41+
}
42+
2843
// check first if the fs is already attached
2944
for _, f := range mfs.FSs {
30-
if f == fs {
45+
if f == instance {
3146
return
3247
}
3348
}
3449

35-
mfs.FSs = append(mfs.FSs, fs)
50+
mfs.FSs = append(mfs.FSs, instance)
3651
}
3752

3853
func (mfs *MultiReadFileFS) Open(name string) (fs.File, error) {

0 commit comments

Comments
 (0)