Skip to content

Commit

Permalink
Merge pull request #41 from k-yomo/fix-not-imported-attributes
Browse files Browse the repository at this point in the history
Fix `highlight_and_snippet_config` is not imported
  • Loading branch information
k-yomo authored Oct 11, 2021
2 parents 10a9772 + 86cd8ce commit 3f556e4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
41 changes: 40 additions & 1 deletion internal/provider/resource_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func resourceIndex() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
Computed: true,
Description: "List of attributes to snippet, with an optional maximum number of words to snippet.",
},
"highlight_pre_tag": {
Expand All @@ -174,7 +175,6 @@ func resourceIndex() *schema.Resource {
"snippet_ellipsis_text": {
Type: schema.TypeString,
Optional: true,
Default: "…",
Description: "String used as an ellipsis indicator when a snippet is truncated.",
},
"restrict_highlight_and_snippet_arrays": {
Expand Down Expand Up @@ -677,6 +677,14 @@ func refreshIndexState(ctx context.Context, d *schema.ResourceData, m interface{
"max_values_per_facet": settings.MaxValuesPerFacet.Get(),
"sort_facet_values_by": settings.SortFacetValuesBy.Get(),
}},
"highlight_and_snippet_config": []interface{}{map[string]interface{}{
"attributes_to_highlight": settings.AttributesToHighlight.Get(),
"attributes_to_snippet": settings.AttributesToSnippet.Get(),
"highlight_pre_tag": settings.HighlightPreTag.Get(),
"highlight_post_tag": settings.HighlightPostTag.Get(),
"snippet_ellipsis_text": settings.SnippetEllipsisText.Get(),
"restrict_highlight_and_snippet_arrays": settings.RestrictHighlightAndSnippetArrays.Get(),
}},
"pagination_config": []interface{}{map[string]interface{}{
"hits_per_page": settings.HitsPerPage.Get(),
"pagination_limited_to": settings.PaginationLimitedTo.Get(),
Expand Down Expand Up @@ -749,6 +757,9 @@ func mapToIndexSettings(d *schema.ResourceData) search.Settings {
if v, ok := d.GetOk("faceting_config"); ok {
unmarshalFacetingConfig(v, &settings)
}
if v, ok := d.GetOk("highlight_and_snippet_config"); ok {
unmarshalHighlightAndSnippetConfig(v, &settings)
}
if v, ok := d.GetOk("pagination_config"); ok {
unmarshalPaginationConfig(v, &settings)
}
Expand Down Expand Up @@ -816,6 +827,34 @@ func unmarshalFacetingConfig(configured interface{}, settings *search.Settings)
}
}

func unmarshalHighlightAndSnippetConfig(configured interface{}, settings *search.Settings) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
}

config := l[0].(map[string]interface{})

if v, ok := config["attributes_to_highlight"]; ok {
settings.AttributesToHighlight = opt.AttributesToHighlight(castStringSet(v)...)
}
if v, ok := config["attributes_to_snippet"]; ok {
settings.AttributesToSnippet = opt.AttributesToSnippet(castStringSet(v)...)
}
if v, ok := config["highlight_pre_tag"]; ok {
settings.HighlightPreTag = opt.HighlightPreTag(v.(string))
}
if v, ok := config["highlight_post_tag"]; ok {
settings.HighlightPostTag = opt.HighlightPostTag(v.(string))
}
if v, ok := config["snippet_ellipsis_text"]; ok {
settings.SnippetEllipsisText = opt.SnippetEllipsisText(v.(string))
}
if v, ok := config["restrict_highlight_and_snippet_arrays"]; ok {
settings.RestrictHighlightAndSnippetArrays = opt.RestrictHighlightAndSnippetArrays(v.(bool))
}
}

func unmarshalPaginationConfig(configured interface{}, settings *search.Settings) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/resource_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func TestAccResourceIndex(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "attributes_config.0.attributes_to_retrieve.0", "*"),
testCheckResourceListAttr(resourceName, "ranking_config.0.ranking", []string{"typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom"}),
resource.TestCheckNoResourceAttr(resourceName, "ranking_config.0.replicas.0"),
testCheckResourceListAttr(resourceName, "highlight_and_snippet_config.0.attributes_to_highlight", []string{}),
testCheckResourceListAttr(resourceName, "highlight_and_snippet_config.0.attributes_to_snippet", []string{}),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.highlight_pre_tag", "<em>"),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.highlight_post_tag", "</em>"),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.snippet_ellipsis_text", ""),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.restrict_highlight_and_snippet_arrays", "false"),
),
},
{
Expand All @@ -40,6 +46,12 @@ func TestAccResourceIndex(t *testing.T) {
resource.TestCheckNoResourceAttr(resourceName, "ranking_config.0.replicas.0"),
resource.TestCheckResourceAttr(resourceName, "faceting_config.0.max_values_per_facet", "50"),
resource.TestCheckResourceAttr(resourceName, "faceting_config.0.sort_facet_values_by", "alpha"),
testCheckResourceListAttr(resourceName, "highlight_and_snippet_config.0.attributes_to_highlight", []string{"title"}),
testCheckResourceListAttr(resourceName, "highlight_and_snippet_config.0.attributes_to_snippet", []string{"description:100"}),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.highlight_pre_tag", "<b>"),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.highlight_post_tag", "</b>"),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.snippet_ellipsis_text", "..."),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.restrict_highlight_and_snippet_arrays", "true"),
),
},
{
Expand All @@ -65,6 +77,7 @@ func testAccResourceIndexUpdate(name string) string {
return fmt.Sprintf(`
resource "algolia_index" "%s" {
name = "%s"
attributes_config {
searchable_attributes = [
"title",
Expand All @@ -85,17 +98,28 @@ resource "algolia_index" "%s" {
"body"
]
}
ranking_config {
ranking = [
"words",
"proximity"
]
}
faceting_config {
max_values_per_facet = 50
sort_facet_values_by = "alpha"
}
highlight_and_snippet_config {
attributes_to_highlight = ["title"]
attributes_to_snippet = ["description:100"]
highlight_pre_tag = "<b>"
highlight_post_tag = "</b>"
snippet_ellipsis_text = "..."
restrict_highlight_and_snippet_arrays = true
}
languages_config {
remove_stop_words_for = ["en"]
}
Expand Down

0 comments on commit 3f556e4

Please sign in to comment.