Repository: algolia/algoliasearch-client-go
Pull request #688 (2022-01-05 17:05:11)

fix: reRankingApplyFilter accept same format as facet filters
| Q                 | A
| ----------------- | ----------
| Bug fix?          | yes
| New feature?      | no 
| BC breaks?        | yes     
| Need Doc update   | no


## Describe your change

The `ReRankingApplyFilter` parameter can take the same values as `FacetFilters`.
This PR adjusts the behavior re-using the internal parsing implementation of FacetFilters to support `string`, `[]string` & `[][]string`.

I have removed the comments indicating that the file is generated automatically as this is what was done as well on the face filters side. I guess we only handle simple types with the generation.

I've updated the tests and also removed the go gen comment.

Note that this change is technically a breaking change as the value returned by `ReRankingApplyFilterOption.Get()` is now `[][]string` instead of `string`. However the returned value should always have been `[][]string` as this covers all possibilities.

The current impact being that all users trying to `GetSettings` will observe the error mentioned below.

## What problem is this fixing?

Currently, when the engine returns something else than a `string`, a `GetSettings` call will fail with the following message:
```
cannot deserialize response's body: cannot unmarshal settings:
json: cannot unmarshal array into Go struct field settings.reRankingApplyFilter of type string:
[...]
```

The payload containing: `{"reRankingApplyFilter":["stock:2"]}` in this instance.

shortcuts: > it shouldn't because you removed the header line // Code generated by go generate. DO NOT EDIT., but better be safe that sorry.

Agreed, and if we go with a test package it could wait confirmation from @VladislavFitz 
Ant-hem: We can try the generation with:

https://github.com/algolia/algoliasearch-client-go/blob/1ea9c3009b1ee05e075d0adab4dea7a6ab02bae5/Makefile#L3-L4 
rayrutjes: `make generate` did not yield any diff.
I can test this in a limited local setup to check that the GetSettings works as expected on the previously faulty customer.
marchelbling: Does anyone know why test-1-11 and test-1-12 are still listed and seem to prevent the build to be green entirely?
From what I see we stopped supporting Go 1.11 and 1.12 in https://github.com/algolia/algoliasearch-client-go/pull/679/files.

Patch:
diff --git a/algolia/internal/gen/options.go b/algolia/internal/gen/options.go
index 734fb819..ccbca418 100644
--- a/algolia/internal/gen/options.go
+++ b/algolia/internal/gen/options.go
@@ -121,7 +121,7 @@ var options = []Option{
 	{"personalizationImpact", Search, 100, ""},
 	{"userToken", Search, "", ""},
 	{"enableReRanking", Search | Settings, true, ""},
-	{"reRankingApplyFilter", Search | Settings, "", ""},
+	{"reRankingApplyFilter", Search | Settings, nil, ""},
 
 	// Performance
 	{"numericAttributesForFiltering", Settings, []string{}, "numericAttributesToIndex"},
diff --git a/algolia/opt/re_ranking_apply_filter.go b/algolia/opt/re_ranking_apply_filter.go
index a1e43186..5b28b324 100644
--- a/algolia/opt/re_ranking_apply_filter.go
+++ b/algolia/opt/re_ranking_apply_filter.go
@@ -1,5 +1,3 @@
-// Code generated by go generate. DO NOT EDIT.
-
 package opt
 
 import "encoding/json"
@@ -7,36 +5,44 @@ import "encoding/json"
 // ReRankingApplyFilterOption is a wrapper for an ReRankingApplyFilter option parameter. It holds
 // the actual value of the option that can be accessed by calling Get.
 type ReRankingApplyFilterOption struct {
-	value string
+	comp composableFilterOption
 }
 
 // ReRankingApplyFilter wraps the given value into a ReRankingApplyFilterOption.
 func ReRankingApplyFilter(v string) *ReRankingApplyFilterOption {
-	return &ReRankingApplyFilterOption{v}
+	return &ReRankingApplyFilterOption{composableFilter(v)}
+}
+
+// ReRankingApplyFilterOr returns an ReRankingApplyFilterOption whose value is set as an OR of the
+// given filters.
+func ReRankingApplyFilterOr(filters ...interface{}) *ReRankingApplyFilterOption {
+	return &ReRankingApplyFilterOption{composableFilterOr(filters...)}
+}
+
+// ReRankingApplyFilterAnd returns an ReRankingApplyFilterOption whose value is set as an AND of
+// the given filters.
+func ReRankingApplyFilterAnd(filters ...interface{}) *ReRankingApplyFilterOption {
+	return &ReRankingApplyFilterOption{composableFilterAnd(filters...)}
 }
 
 // Get retrieves the actual value of the option parameter.
-func (o *ReRankingApplyFilterOption) Get() string {
+func (o *ReRankingApplyFilterOption) Get() [][]string {
 	if o == nil {
-		return ""
+		return nil
 	}
-	return o.value
+	return o.comp.Get()
 }
 
 // MarshalJSON implements the json.Marshaler interface for
 // ReRankingApplyFilterOption.
 func (o ReRankingApplyFilterOption) MarshalJSON() ([]byte, error) {
-	return json.Marshal(o.value)
+	return json.Marshal(o.comp)
 }
 
 // UnmarshalJSON implements the json.Unmarshaler interface for
 // ReRankingApplyFilterOption.
 func (o *ReRankingApplyFilterOption) UnmarshalJSON(data []byte) error {
-	if string(data) == "null" {
-		o.value = ""
-		return nil
-	}
-	return json.Unmarshal(data, &o.value)
+	return json.Unmarshal(data, &o.comp)
 }
 
 // Equal returns true if the given option is equal to the instance one. In case
@@ -44,12 +50,12 @@ func (o *ReRankingApplyFilterOption) UnmarshalJSON(data []byte) error {
 // value of the option.
 func (o *ReRankingApplyFilterOption) Equal(o2 *ReRankingApplyFilterOption) bool {
 	if o == nil {
-		return o2 == nil || o2.value == ""
+		return o2 == nil || len(o2.comp.Get()) == 0
 	}
 	if o2 == nil {
-		return o == nil || o.value == ""
+		return o == nil || len(o.comp.Get()) == 0
 	}
-	return o.value == o2.value
+	return o.comp.Equal(&o2.comp)
 }
 
 // ReRankingApplyFilterEqual returns true if the two options are equal.


Test patch:
diff --git a/algolia/internal/opt/re_ranking_apply_filter_test.go b/algolia/internal/opt/re_ranking_apply_filter_test.go
index c0f128dd..f45e4822 100644
--- a/algolia/internal/opt/re_ranking_apply_filter_test.go
+++ b/algolia/internal/opt/re_ranking_apply_filter_test.go
@@ -1,5 +1,3 @@
-// Code generated by go generate. DO NOT EDIT.
-
 package opt
 
 import (
@@ -17,15 +15,71 @@ func TestReRankingApplyFilter(t *testing.T) {
 	}{
 		{
 			opts:     []interface{}{nil},
-			expected: opt.ReRankingApplyFilter(""),
+			expected: &opt.ReRankingApplyFilterOption{},
+		},
+		{
+			opts: []interface{}{opt.ReRankingApplyFilter("filter1:value1")},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+			),
+		},
+		{
+			opts: []interface{}{opt.ReRankingApplyFilterOr(
+				"filter1:value1",
+				"filter2:value2",
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1", "filter2:value2"},
+			),
+		},
+		{
+			opts: []interface{}{opt.ReRankingApplyFilterOr(
+				"filter1:value1",
+				opt.ReRankingApplyFilter("filter2:value2"),
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1", "filter2:value2"},
+			),
+		},
+		{
+			opts: []interface{}{opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+				[]string{"filter2:value2"},
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+				[]string{"filter2:value2"},
+			),
+		},
+		{
+			opts: []interface{}{opt.ReRankingApplyFilterAnd(
+				"filter1:value1",
+				[]string{"filter2:value2"},
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+				[]string{"filter2:value2"},
+			),
 		},
 		{
-			opts:     []interface{}{opt.ReRankingApplyFilter("")},
-			expected: opt.ReRankingApplyFilter(""),
+			opts: []interface{}{opt.ReRankingApplyFilterAnd(
+				"filter1:value1",
+				[]string{"filter2:value2", "filter3:value3"},
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+				[]string{"filter2:value2", "filter3:value3"},
+			),
 		},
 		{
-			opts:     []interface{}{opt.ReRankingApplyFilter("content of the string value")},
-			expected: opt.ReRankingApplyFilter("content of the string value"),
+			opts: []interface{}{opt.ReRankingApplyFilterAnd(
+				"filter1:value1",
+				opt.ReRankingApplyFilterOr("filter2:value2", "filter3:value3"),
+			)},
+			expected: opt.ReRankingApplyFilterAnd(
+				[]string{"filter1:value1"},
+				[]string{"filter2:value2", "filter3:value3"},
+			),
 		},
 	} {
 		var (