all repos — searchix @ 8537192b854cb4abde7c7ca2d58c201df060504a

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: reduce new variables when looping over hits
Alan Pearce alan@alanpearce.eu
Wed, 08 May 2024 17:10:36 +0200
commit

8537192b854cb4abde7c7ca2d58c201df060504a

parent

89f7b84789d2bf1453bda0f0a7b3673126fc2406

1 files changed, 8 insertions(+), 5 deletions(-)

jump to
M internal/search/search.gointernal/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]{