Skip to content

Commit

Permalink
fix: Export missing attributes in Settings.ToMap
Browse files Browse the repository at this point in the history
The following attributes were missing in the result Map returned by the
Settings.ToMap function:
 * `enableRules`
 * `ignorePlurals`
 * `sortFacetValuesBy`

Also, some fields may not have been properly transformed because the
Settings.clean function was not called to clean some fields with sane
defaults.

This commits fixes all the aformentioned issues and adds a test to
ensure that any new field added to the Settings struct will make the
tests fail in not handled in the Settings.ToMap function.
  • Loading branch information
aseure committed Jun 29, 2018
1 parent 9c969af commit a0ec60a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions algoliasearch/index_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package algoliasearch

import (
"reflect"
"sort"
"sync"
"testing"
"time"
"unicode"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -1072,6 +1074,31 @@ func TestSettingsToMap(t *testing.T) {
settingsAreEqual(t, settingsBefore, settingsAfter)
}

func TestSettingsToMap_allRequiredFieldsArePresent(t *testing.T) {
var settings Settings

m := settings.ToMap()

s := reflect.ValueOf(&settings).Elem()
tt := s.Type()

for i := 0; i < s.NumField(); i++ {
// Skip []string-type fields because those are discarded to avoid
// sending useless data to the API.
if tt.Field(i).Type.String() == "[]string" {
continue
}

expectedSettingName := tt.Field(i).Name
tmp := []rune(expectedSettingName)
tmp[0] = unicode.ToLower(tmp[0])
expectedSettingName = string(tmp)

_, ok := m[expectedSettingName]
require.True(t, ok, "should find '%s' setting in the result map", expectedSettingName)
}
}

func facetHitSliceAreEqual(fs1, fs2 []FacetHit) bool {
if len(fs1) != len(fs2) {
return false
Expand Down
11 changes: 11 additions & 0 deletions algoliasearch/types_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,20 @@ func (s *Settings) clean() {
if s.MaxFacetHits == 0 {
s.MaxFacetHits = 10
}

if s.SortFacetValuesBy == "" {
s.SortFacetValuesBy = "count"
}
}

// ToMap produces a `Map` corresponding to the `Settings struct`. It should
// only be used when it's needed to pass a `Settings struct` to `SetSettings`,
// typically when one needs to copy settings between two indices.
func (s *Settings) ToMap() Map {
// Guarantee that interface{}-typed fields and default values are correctly
// set.
s.clean()

// Add all fields except:
// - Distinct float64 or bool
// - IgnorePlurals []interface{} or bool
Expand Down Expand Up @@ -117,9 +125,11 @@ func (s *Settings) ToMap() Map {
"attributesToHighlight": s.AttributesToHighlight,
"attributesToRetrieve": s.AttributesToRetrieve,
"attributesToSnippet": s.AttributesToSnippet,
"enableRules": s.EnableRules,
"highlightPostTag": s.HighlightPostTag,
"highlightPreTag": s.HighlightPreTag,
"hitsPerPage": s.HitsPerPage,
"ignorePlurals": s.IgnorePlurals,
"maxFacetHits": s.MaxFacetHits,
"maxValuesPerFacet": s.MaxValuesPerFacet,
"minProximity": s.MinProximity,
Expand All @@ -132,6 +142,7 @@ func (s *Settings) ToMap() Map {
"responseFields": s.ResponseFields,
"restrictHighlightAndSnippetArrays": s.RestrictHighlightAndSnippetArrays,
"snippetEllipsisText": s.SnippetEllipsisText,
"sortFacetValuesBy": s.SortFacetValuesBy,
"typoTolerance": s.TypoTolerance,
}

Expand Down

0 comments on commit a0ec60a

Please sign in to comment.