Repository: algolia/algoliasearch-client-go
Pull request #557 (2019-10-18 12:24:00)

fix: correctly type opt.RestrictSources to string instead of []string


Patch:
diff --git a/algolia/internal/gen/options.go b/algolia/internal/gen/options.go
index 24dc71df..06d42d35 100644
--- a/algolia/internal/gen/options.go
+++ b/algolia/internal/gen/options.go
@@ -139,7 +139,7 @@ var options = []Option{
 
 	// Secured API key
 	{"restrictIndices", Other, []string{}, ""},
-	{"restrictSources", Other, []string{}, ""},
+	{"restrictSources", Other, "", ""},
 	{"validUntil", Other, nil, ""},
 	{"referers", Other, []string{}, ""},
 
diff --git a/algolia/opt/restrict_sources.go b/algolia/opt/restrict_sources.go
index c1ea2558..ebcc4fa4 100644
--- a/algolia/opt/restrict_sources.go
+++ b/algolia/opt/restrict_sources.go
@@ -2,26 +2,23 @@
 
 package opt
 
-import (
-	"encoding/json"
-	"reflect"
-)
+import "encoding/json"
 
 // RestrictSourcesOption is a wrapper for an RestrictSources option parameter. It holds
 // the actual value of the option that can be accessed by calling Get.
 type RestrictSourcesOption struct {
-	value []string
+	value string
 }
 
 // RestrictSources wraps the given value into a RestrictSourcesOption.
-func RestrictSources(v ...string) *RestrictSourcesOption {
+func RestrictSources(v string) *RestrictSourcesOption {
 	return &RestrictSourcesOption{v}
 }
 
 // Get retrieves the actual value of the option parameter.
-func (o *RestrictSourcesOption) Get() []string {
+func (o *RestrictSourcesOption) Get() string {
 	if o == nil {
-		return []string{}
+		return ""
 	}
 	return o.value
 }
@@ -36,7 +33,7 @@ func (o RestrictSourcesOption) MarshalJSON() ([]byte, error) {
 // RestrictSourcesOption.
 func (o *RestrictSourcesOption) UnmarshalJSON(data []byte) error {
 	if string(data) == "null" {
-		o.value = []string{}
+		o.value = ""
 		return nil
 	}
 	return json.Unmarshal(data, &o.value)
@@ -47,12 +44,12 @@ func (o *RestrictSourcesOption) UnmarshalJSON(data []byte) error {
 // value of the option.
 func (o *RestrictSourcesOption) Equal(o2 *RestrictSourcesOption) bool {
 	if o == nil {
-		return o2 == nil || reflect.DeepEqual(o2.value, []string{})
+		return o2 == nil || o2.value == ""
 	}
 	if o2 == nil {
-		return o == nil || reflect.DeepEqual(o.value, []string{})
+		return o == nil || o.value == ""
 	}
-	return reflect.DeepEqual(o.value, o2.value)
+	return o.value == o2.value
 }
 
 // RestrictSourcesEqual returns true if the two options are equal.


Test patch:
diff --git a/algolia/internal/opt/restrict_sources_test.go b/algolia/internal/opt/restrict_sources_test.go
index de7753f4..2a5eeef6 100644
--- a/algolia/internal/opt/restrict_sources_test.go
+++ b/algolia/internal/opt/restrict_sources_test.go
@@ -17,15 +17,15 @@ func TestRestrictSources(t *testing.T) {
 	}{
 		{
 			opts:     []interface{}{nil},
-			expected: opt.RestrictSources([]string{}...),
+			expected: opt.RestrictSources(""),
 		},
 		{
-			opts:     []interface{}{opt.RestrictSources("value1")},
-			expected: opt.RestrictSources("value1"),
+			opts:     []interface{}{opt.RestrictSources("")},
+			expected: opt.RestrictSources(""),
 		},
 		{
-			opts:     []interface{}{opt.RestrictSources("value1", "value2", "value3")},
-			expected: opt.RestrictSources("value1", "value2", "value3"),
+			opts:     []interface{}{opt.RestrictSources("content of the string value")},
+			expected: opt.RestrictSources("content of the string value"),
 		},
 	} {
 		var (
@@ -36,6 +36,6 @@ func TestRestrictSources(t *testing.T) {
 		require.NoError(t, err)
 		err = json.Unmarshal(data, &out)
 		require.NoError(t, err)
-		require.ElementsMatch(t, c.expected.Get(), out.Get())
+		require.Equal(t, *c.expected, out)
 	}
 }