From a199a1c6039014cc261e538391cee4b65145834f Mon Sep 17 00:00:00 2001 From: rmanach Date: Wed, 28 Aug 2024 11:07:42 +0200 Subject: [PATCH] fix lint issues + check hits length before access --- docs/data-sources/transport_product.md | 3 +-- internal/data_sources/access_product_data_source.go | 2 +- internal/data_sources/cloud_product_data_source.go | 2 +- internal/data_sources/transport_product_data_source.go | 7 ++++++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/data-sources/transport_product.md b/docs/data-sources/transport_product.md index 0dc705d..e6cd5e9 100644 --- a/docs/data-sources/transport_product.md +++ b/docs/data-sources/transport_product.md @@ -49,7 +49,7 @@ data "autonomi_transport_product" "transport" { - `facet_distribution` (Attributes) The **facet_distribution** attribute provides an overview of the distribution of various facets within the transport products returned by the Meilisearch query. This attribute allows you to analyze the frequency of different categories or attributes in the search results. (see [below for nested schema](#nestedatt--facet_distribution)) -- `hit` (Attributes) The **hits** attribute contains the list of transport products returned by the Meilisearch query. +- `hit` (Attributes) The **hit** attribute contains the transport product returned by the Meilisearch query. Each hit represents a transport product that matches the specified search criteria. (see [below for nested schema](#nestedatt--hit)) @@ -71,7 +71,6 @@ Read-Only: - `location_to` (Map of Number) - `provider` (Map of Number) - ### Nested Schema for `hit` diff --git a/internal/data_sources/access_product_data_source.go b/internal/data_sources/access_product_data_source.go index 616a20c..fcc97aa 100644 --- a/internal/data_sources/access_product_data_source.go +++ b/internal/data_sources/access_product_data_source.go @@ -181,7 +181,7 @@ func (d *accessProductDataSource) Read(ctx context.Context, req datasource.ReadR return } - if accessProducts.Hits == nil { + if len(accessProducts.Hits) == 0 { resp.Diagnostics.AddError("Not hit found", "") return } diff --git a/internal/data_sources/cloud_product_data_source.go b/internal/data_sources/cloud_product_data_source.go index 56ad7e0..ef5bb82 100644 --- a/internal/data_sources/cloud_product_data_source.go +++ b/internal/data_sources/cloud_product_data_source.go @@ -184,7 +184,7 @@ func (d *cloudProductDataSource) Read(ctx context.Context, req datasource.ReadRe return } - if cloudProducts.Hits == nil { + if len(cloudProducts.Hits) == 0 { resp.Diagnostics.AddError("Not hit found", "") return } diff --git a/internal/data_sources/transport_product_data_source.go b/internal/data_sources/transport_product_data_source.go index 008d766..919d768 100644 --- a/internal/data_sources/transport_product_data_source.go +++ b/internal/data_sources/transport_product_data_source.go @@ -69,7 +69,7 @@ func (d *transportProductDataSource) Schema(_ context.Context, _ datasource.Sche }, }, "hit": schema.SingleNestedAttribute{ - MarkdownDescription: `The **hits** attribute contains the list of transport products returned by the Meilisearch query. + MarkdownDescription: `The **hit** attribute contains the transport product returned by the Meilisearch query. Each hit represents a transport product that matches the specified search criteria.`, Computed: true, Attributes: map[string]schema.Attribute{ @@ -181,6 +181,11 @@ func (d *transportProductDataSource) Read(ctx context.Context, req datasource.Re return } + if len(transportProducts.Hits) == 0 { + resp.Diagnostics.AddError("Not hit found", "") + return + } + // If Meiliesearch return more than one hit, check if `cheapest` filter has been set. // If not, an error is returned, otherwise a sort will be done to order the list by price mrc. The first entry will be returned if len(transportProducts.Hits) > 1 {