Skip to content

Commit

Permalink
feat(go): add SearchFor typed helpers (#3252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 authored Jun 25, 2024
1 parent e28d829 commit d4bd0f2
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions templates/go/search_helpers.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
/*
SearchForHits calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
Disclaimer: We don't assert that the parameters you pass to this method only contains `hits` requests to prevent impacting search performances, this helper is purely for typing purposes.

@param ctx context.Context - The context that will be drilled down to the actual request.
@param r ApiSearchRequest - Body of the `search` operation.
@param opts ...Option - Optional parameters for the request.
@return []SearchResponse - List of hits.
@return error - Error if any.
*/
func (c *APIClient) SearchForHits(ctx context.Context, r ApiSearchRequest, opts ...Option) ([]SearchResponse, error) {
res, err := c.SearchWithContext(ctx, r, opts...)
if err != nil {
return nil, err
}

hits := make([]SearchResponse, 0, len(res.GetResults()))

for _, hit := range res.GetResults() {
if hit.SearchResponse != nil {
hits = append(hits, *hit.SearchResponse)
}
}

return slices.Clip(hits), nil
}

/*
SearchForFacets calls the `search` method but with certainty that we will only request Algolia facets and not records (hits).
Disclaimer: We don't assert that the parameters you pass to this method only contains `facets` requests to prevent impacting search performances, this helper is purely for typing purposes.

@param ctx context.Context - The context that will be drilled down to the actual request.
@param r ApiSearchRequest - Body of the `search` operation.
@param opts ...Option - Optional parameters for the request.
@return []SearchForFacetValuesResponse - List of facet hits.
@return error - Error if any.
*/
func (c *APIClient) SearchForFacets(ctx context.Context, r ApiSearchRequest, opts ...Option) ([]SearchForFacetValuesResponse, error) {
res, err := c.SearchWithContext(ctx, r, opts...)
if err != nil {
return nil, err
}

facetHits := make([]SearchForFacetValuesResponse, 0, len(res.GetResults()))

for _, hit := range res.GetResults() {
if hit.SearchForFacetValuesResponse != nil {
facetHits = append(facetHits, *hit.SearchForFacetValuesResponse)
}
}

return slices.Clip(facetHits), nil
}

/*
WaitForTask waits for a task to be published.
Wraps WaitForTaskWithContext with context.Background().
Expand Down

1 comment on commit d4bd0f2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.