From 8537192b854cb4abde7c7ca2d58c201df060504a Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 8 May 2024 17:10:36 +0200 Subject: refactor: reduce new variables when looping over hits --- internal/search/search.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'internal/search/search.go') 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]{ -- cgit 1.4.1