Skip to content

Commit

Permalink
fix: support both schemas at the same time in rule consequence params
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagriar committed Mar 30, 2023
1 parent f61b370 commit 1f720e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 10 additions & 10 deletions algolia/search/rule_consequence_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ func (q *RuleQuery) UnmarshalJSON(data []byte) error {
return nil
}

var objectQuery RuleQueryObjectQuery
if err := json.Unmarshal(data, &objectQuery); err == nil {
q.objectQuery = &objectQuery
}

// Kept for backward-compatibility only
var incrementalEdit struct {
Remove []string `json:"remove"`
}
if err := json.Unmarshal(data, &incrementalEdit); err == nil && len(incrementalEdit.Remove) > 0 {
var edits []QueryEdit
if err := json.Unmarshal(data, &incrementalEdit); err == nil {
if q.objectQuery == nil {
q.objectQuery = &RuleQueryObjectQuery{}
}
for _, word := range incrementalEdit.Remove {
edits = append(edits, RemoveEdit(word))
q.objectQuery.Edits = append(q.objectQuery.Edits, RemoveEdit(word))
}
q.objectQuery = &RuleQueryObjectQuery{Edits: edits}
return nil
}

var objectQuery RuleQueryObjectQuery
if err := json.Unmarshal(data, &objectQuery); err == nil {
q.objectQuery = &objectQuery
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions algolia/search/rule_consequence_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func Test_LegacyParsingOfQueryEdits(t *testing.T) {
input: `{"edits":[{"type":"remove","delete":"myWord"}]}`,
expected: RuleQuery{objectQuery: &RuleQueryObjectQuery{Edits: []QueryEdit{RemoveEdit("myWord")}}},
},
{
input: `{"edits":[{"type":"remove","delete":"myWord1"}],"remove":["myWord2"]}`,
expected: RuleQuery{objectQuery: &RuleQueryObjectQuery{Edits: []QueryEdit{RemoveEdit("myWord1"), RemoveEdit("myWord2")}}},
},
} {
var actual RuleQuery
err := json.Unmarshal([]byte(c.input), &actual)
Expand Down

0 comments on commit 1f720e4

Please sign in to comment.