diff options
author | Alan Pearce | 2024-05-08 17:10:36 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-08 17:10:36 +0200 |
commit | 8537192b854cb4abde7c7ca2d58c201df060504a (patch) | |
tree | fe93504a2f0a4d4ba1bc4ec1de234cb030070971 /internal | |
parent | 89f7b84789d2bf1453bda0f0a7b3673126fc2406 (diff) | |
download | searchix-8537192b854cb4abde7c7ca2d58c201df060504a.tar.lz searchix-8537192b854cb4abde7c7ca2d58c201df060504a.tar.zst searchix-8537192b854cb4abde7c7ca2d58c201df060504a.zip |
refactor: reduce new variables when looping over hits
Diffstat (limited to 'internal')
-rw-r--r-- | internal/search/search.go | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/internal/search/search.go b/internal/search/search.go index 596c1fb..da5865c 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -159,14 +159,17 @@ func (index *Index[T]) Search(ctx context.Context, keyword string, from uint64) } results := make([]T, min(ResultsPerPage, bleveResult.Total)) + var buf bytes.Buffer for i, result := range bleveResult.Hits { - data := result.Fields["data"] - byt := bytes.NewBuffer([]byte(data.(string))) - dec := gob.NewDecoder(byt) - err := dec.Decode(&results[i]) + _, err = buf.WriteString(result.Fields["data"].(string)) if err != nil { - return nil, errors.WithMessagef(err, "error decoding gob data: %s", byt.String()) + return nil, errors.WithMessage(err, "error fetching result data") } + err = gob.NewDecoder(&buf).Decode(&results[i]) + if err != nil { + return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String()) + } + buf.Reset() } return &Result[T]{ |