Repository: algolia/algoliasearch-client-go
Pull request #674 (2021-07-09 08:46:51)

feat(RenderingContent): Add the rendering content as a settings field
| Q                 | A
| ----------------- | ----------
| Bug fix?          | no
| New feature?      | yes
| BC breaks?        | no     
| Need Doc update   | yes


## Describe your change

- Add the `RenderingContent` as a Settings field.
- Add a convenient `FacetValuesOrder` constructor


Patch:
diff --git a/algolia/internal/gen/templates/settings.go.tmpl b/algolia/internal/gen/templates/settings.go.tmpl
index 9f2adfc8..fc2691ae 100644
--- a/algolia/internal/gen/templates/settings.go.tmpl
+++ b/algolia/internal/gen/templates/settings.go.tmpl
@@ -16,6 +16,7 @@ type Settings struct {
     {{- range $_, $setting := . }}
     {{ title $setting.Name }} *opt.{{ title $setting.Name }}Option `json:"{{ $setting.Name }},omitempty"`
     {{- end }}
+    RenderingContent *RenderingContent `json:"renderingContent,omitempty"`
     CustomSettings map[string]interface{} `json:"-"`
 }
 
@@ -105,6 +106,8 @@ func (s Settings) String() string {
     settingsStr += fmt.Sprintf("\t{{ title $setting.Name }}: %v,\n", stringifyReturnValues(s.{{ title $setting.Name }}.Get()))
     {{- end }}
 
+    settingsStr += fmt.Sprintf("\tRenderingContent: %+v\n", s.RenderingContent)
+
     settingsStr += "\tCustomSettings{\n"
     for k, v := range s.CustomSettings {
         settingsStr += fmt.Sprintf("\t\t%s: %v,\n", k, v)
diff --git a/algolia/search/rendering_content.go b/algolia/search/rendering_content.go
index ad6572b8..2d624c5d 100644
--- a/algolia/search/rendering_content.go
+++ b/algolia/search/rendering_content.go
@@ -30,6 +30,10 @@ type FacetValuesOrder struct {
 	SortRemainingBy *SortRule `json:"sortRemainingBy"`
 }
 
+func NewFacetValuesOrder(order []string, sortRemainingBy SortRule) FacetValuesOrder {
+	return FacetValuesOrder{Order: order, SortRemainingBy: &sortRemainingBy}
+}
+
 // Rule defining the sort order of facet values.
 type SortRule string
 
diff --git a/algolia/search/settings.go b/algolia/search/settings.go
index 2014ba2e..1eb673e7 100644
--- a/algolia/search/settings.go
+++ b/algolia/search/settings.go
@@ -73,6 +73,7 @@ type Settings struct {
 	Advanced                                *opt.AdvancedOption                                `json:"advanced,omitempty"`
 	AttributeCriteriaComputedByMinProximity *opt.AttributeCriteriaComputedByMinProximityOption `json:"attributeCriteriaComputedByMinProximity,omitempty"`
 	UserData                                *opt.UserDataOption                                `json:"userData,omitempty"`
+	RenderingContent                        *RenderingContent                                  `json:"renderingContent"`
 	CustomSettings                          map[string]interface{}                             `json:"-"`
 }
 
@@ -512,6 +513,8 @@ func (s Settings) String() string {
 	settingsStr += fmt.Sprintf("\tAttributeCriteriaComputedByMinProximity: %v,\n", stringifyReturnValues(s.AttributeCriteriaComputedByMinProximity.Get()))
 	settingsStr += fmt.Sprintf("\tUserData: %v,\n", stringifyReturnValues(s.UserData.Get()))
 
+	settingsStr += fmt.Sprintf("\tRenderingContent: %+v\n", *s.RenderingContent)
+
 	settingsStr += "\tCustomSettings{\n"
 	for k, v := range s.CustomSettings {
 		settingsStr += fmt.Sprintf("\t\t%s: %v,\n", k, v)


Test patch:
diff --git a/algolia/search/rendering_content_test.go b/algolia/search/rendering_content_test.go
index 290aeab9..35fe384c 100644
--- a/algolia/search/rendering_content_test.go
+++ b/algolia/search/rendering_content_test.go
@@ -34,3 +34,21 @@ func TestUnmarshalRenderingContent(t *testing.T) {
 	require.Equal(t, r.FacetOrdering.Values["color"].Order, []string{"red", "green"})
 	require.Nil(t, r.FacetOrdering.Values["color"].SortRemainingBy)
 }
+
+func TestBuildRenderingContent(t *testing.T) {
+
+	var r = RenderingContent{FacetOrdering: &FacetOrdering{
+		Facets: &FacetsOrder{Order: []string{"size", "brand"}},
+		Values: map[string]FacetValuesOrder{
+			"brand": NewFacetValuesOrder([]string{"Uniqlo"}, Count),
+			"size":  NewFacetValuesOrder([]string{"S", "M", "L"}, Hidden),
+		},
+	}}
+
+	require.Equal(t, r.FacetOrdering.Facets.Order, []string{"size", "brand"})
+	require.Equal(t, r.FacetOrdering.Values["brand"].Order, []string{"Uniqlo"})
+	require.Equal(t, *r.FacetOrdering.Values["brand"].SortRemainingBy, Count)
+	require.Equal(t, r.FacetOrdering.Values["size"].Order, []string{"S", "M", "L"})
+	require.Equal(t, *r.FacetOrdering.Values["size"].SortRemainingBy, Hidden)
+
+}