Skip to content

Commit 3fae27c

Browse files
committed
refactor results calculator to object
1 parent bea62ad commit 3fae27c

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

common/results.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,46 @@ type ResultRow struct {
1717
CandidateSources []string `json:"candidate_sources,omitempty"`
1818
}
1919

20-
func CalculateResults(fs Filesystem, source ValueSource) ([]ResultRow, error) {
21-
values := source.Values()
20+
type positionLookup multimap.MultiMap[string, int]
21+
22+
func duplicatePredicate(a, b int) bool { return true }
23+
24+
type ResultsCalculator struct {
25+
fs Filesystem
26+
source ValueSource
27+
pathsLookup positionLookup
28+
}
29+
30+
func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator {
31+
return &ResultsCalculator{
32+
fs: fs,
33+
source: source,
34+
pathsLookup: multimap.NewMapSet[string](duplicatePredicate),
35+
}
36+
}
37+
38+
func (r *ResultsCalculator) CalculateResults() ([]ResultRow, error) {
39+
values := r.source.Values()
2240

2341
if len(values) == 0 {
24-
return nil, errors.New(source.Source() + " is empty")
42+
return nil, errors.New(r.source.Source() + " is empty")
2543
}
2644

27-
duplicatePredicate := func(a, b int) bool { return true }
28-
pathsLookup := multimap.NewMapSet[string](duplicatePredicate)
2945
for i, path := range values {
30-
pathKey := fs.GetAbsolutePath(path)
31-
pathsLookup.Put(pathKey, i)
46+
pathKey := r.fs.GetAbsolutePath(path)
47+
r.pathsLookup.Put(pathKey, i)
3248
}
3349

34-
return calculateResultRows(fs, values, pathsLookup), nil
50+
return r.calculateResultRows(), nil
3551
}
3652

37-
func calculateResultRows(fs Filesystem, paths []string, pathsLookup multimap.MultiMap[string, int]) []ResultRow {
53+
func (r *ResultsCalculator) calculateResultRows() []ResultRow {
54+
paths := r.source.Values()
3855
res := []ResultRow{}
3956
for index, path := range paths {
40-
pathKey := fs.GetAbsolutePath(path)
41-
dup := getDuplicatesOf(pathsLookup, pathKey, index)
42-
exists, isdir := fs.PathStatus(pathKey)
57+
pathKey := r.fs.GetAbsolutePath(path)
58+
dup := getDuplicatesOf(r.pathsLookup, pathKey, index)
59+
exists, isdir := r.fs.PathStatus(pathKey)
4360
res = append(res, ResultRow{
4461
Id: index + 1,
4562
Path: path,

view/common.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ func getResults() []common.ResultRow {
1212

1313
fs := &common.OsFilesystem{}
1414

15-
results, err := common.CalculateResults(fs, source)
15+
resultsCalculator := common.NewResultsCalculator(fs, source)
16+
17+
results, err := resultsCalculator.CalculateResults()
1618
if err != nil {
1719
common.FailWith(err.Error())
1820
}

0 commit comments

Comments
 (0)