refactor: reduce new variables when looping over hits
Alan Pearce alan@alanpearce.eu
Wed, 08 May 2024 17:10:36 +0200
1 files changed, 8 insertions(+), 5 deletions(-)
jump to
M internal/search/search.go → internal/search/search.go
@@ -159,14 +159,17 @@ return nil, errors.WithMessage(err, "failed to execute search query") } 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.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", byt.String()) + return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String()) } + buf.Reset() } return &Result[T]{