Skip to content

Commit

Permalink
fix: composableFilterOption deserialization fixed (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetaligok committed Feb 16, 2023
1 parent e67fb07 commit b3a3422
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 15 deletions.
10 changes: 9 additions & 1 deletion algolia/internal/opt/facet_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ func TestFacetFilters_LegacyDeserialization(t *testing.T) {
},
{
`["filter1:value1","filter2:value2"]`,
opt.FacetFilterOr("filter1:value1", "filter2:value2"),
opt.FacetFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[" filter1:value1 "," filter2:value2 "]`,
opt.FacetFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"]]`,
opt.FacetFilterOr("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"], "filter3:value3"]`,
opt.FacetFilterAnd(opt.FacetFilterOr("filter1:value1", "filter2:value2"), "filter3:value3"),
},
} {
var got opt.FacetFiltersOption
err := json.Unmarshal([]byte(c.payload), &got)
Expand Down
10 changes: 9 additions & 1 deletion algolia/internal/opt/numeric_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ func TestNumericFilters_LegacyDeserialization(t *testing.T) {
},
{
`["filter1:value1","filter2:value2"]`,
opt.NumericFilterOr("filter1:value1", "filter2:value2"),
opt.NumericFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[" filter1:value1 "," filter2:value2 "]`,
opt.NumericFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"]]`,
opt.NumericFilterOr("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"], "filter3:value3"]`,
opt.NumericFilterAnd(opt.NumericFilterOr("filter1:value1", "filter2:value2"), "filter3:value3"),
},
} {
var got opt.NumericFiltersOption
err := json.Unmarshal([]byte(c.payload), &got)
Expand Down
10 changes: 9 additions & 1 deletion algolia/internal/opt/optional_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ func TestOptionalFilters_LegacyDeserialization(t *testing.T) {
},
{
`["filter1:value1","filter2:value2"]`,
opt.OptionalFilterOr("filter1:value1", "filter2:value2"),
opt.OptionalFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[" filter1:value1 "," filter2:value2 "]`,
opt.OptionalFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"]]`,
opt.OptionalFilterOr("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"], "filter3:value3"]`,
opt.OptionalFilterAnd(opt.OptionalFilterOr("filter1:value1", "filter2:value2"), "filter3:value3"),
},
} {
var got opt.OptionalFiltersOption
err := json.Unmarshal([]byte(c.payload), &got)
Expand Down
10 changes: 9 additions & 1 deletion algolia/internal/opt/tag_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ func TestTagFilters_LegacyDeserialization(t *testing.T) {
},
{
`["filter1:value1","filter2:value2"]`,
opt.TagFilterOr("filter1:value1", "filter2:value2"),
opt.TagFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[" filter1:value1 "," filter2:value2 "]`,
opt.TagFilterAnd("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"]]`,
opt.TagFilterOr("filter1:value1", "filter2:value2"),
},
{
`[["filter1:value1","filter2:value2"], "filter3:value3"]`,
opt.TagFilterAnd(opt.TagFilterOr("filter1:value1", "filter2:value2"), "filter3:value3"),
},
} {
var got opt.TagFiltersOption
err := json.Unmarshal([]byte(c.payload), &got)
Expand Down
32 changes: 23 additions & 9 deletions algolia/opt/composable_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opt

import (
"encoding/json"
"fmt"
"reflect"
"strings"

Expand Down Expand Up @@ -74,10 +75,11 @@ func (o *composableFilterOption) UnmarshalJSON(data []byte) error {
}

var (
ok = false
filter string
ors []string
ands [][]string
ok = false
filter string
ors []string
ands [][]string
options []interface{}
)

if json.Unmarshal(data, &filter) == nil {
Expand All @@ -86,13 +88,25 @@ func (o *composableFilterOption) UnmarshalJSON(data []byte) error {
ands = append(ands, ors)
}

if !ok && json.Unmarshal(data, &ors) == nil {
if !ok && json.Unmarshal(data, &options) == nil {
ok = true
ands = append(ands, ors)
}

if !ok && json.Unmarshal(data, &ands) == nil {
ok = true
for _, option := range options {
switch v := option.(type) {
case []interface{}:
ors = []string{}

for _, val := range v {
ors = append(ors, fmt.Sprint(val))
}

ands = append(ands, ors)
case string:
ands = append(ands, []string{v})
default:
return errs.ErrJSONDecode(data, "composableFilterOption (string or []string or [][]string)")
}
}
}

if !ok {
Expand Down
18 changes: 16 additions & 2 deletions algolia/opt/composable_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func TestComposableFilterOption_UnmarshalJSON(t *testing.T) {
{
`["color:green","color:yellow"]`,
composableFilterOption{[][]string{
{`color:green`, `color:yellow`},
{`color:green`}, {`color:yellow`},
}},
},
{
`[" color:green "," color:yellow "]`,
composableFilterOption{[][]string{
{`color:green`, `color:yellow`},
{`color:green`}, {`color:yellow`},
}},
},
{
Expand All @@ -70,6 +70,20 @@ func TestComposableFilterOption_UnmarshalJSON(t *testing.T) {
{`color:yellow`},
}},
},
{
`[["color:green","color:yellow"], ["color:blue"]]`,
composableFilterOption{[][]string{
{`color:green`, `color:yellow`},
{`color:blue`},
}},
},
{
`[["color:green","color:yellow"], "color:blue"]`,
composableFilterOption{[][]string{
{`color:green`, `color:yellow`},
{`color:blue`},
}},
},
} {
var got composableFilterOption
err := json.Unmarshal([]byte(c.payload), &got)
Expand Down

0 comments on commit b3a3422

Please sign in to comment.