Skip to content

Commit

Permalink
Merge pull request #1157 from nyaruka/more_generics
Browse files Browse the repository at this point in the history
Convert `StringSet` functions to be generic
  • Loading branch information
rowanseymour authored Feb 7, 2023
2 parents 7a13fbb + 65e2dad commit 81ac7a8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions contactql/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func Inspect(query *ContactQuery) *Inspection {
allowAsGroup := !(attributes[AttributeID] || attributes[AttributeStatus] || attributes[AttributeGroup] || attributes[AttributeFlow] || attributes[AttributeHistory])

return &Inspection{
Attributes: utils.StringSetKeys(attributes),
Schemes: utils.StringSetKeys(schemes),
Attributes: utils.SortedKeys(attributes),
Schemes: utils.SortedKeys(schemes),
Fields: fieldRefs,
Groups: groupRefs,
AllowAsGroup: allowAsGroup,
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (f *flow) extract() ([]flows.ExtractedTemplate, []flows.ExtractedReference,
})
}

return templates, assetRefs, utils.StringSetKeys(parentRefs)
return templates, assetRefs, utils.SortedKeys(parentRefs)
}

// extracts all result specs
Expand Down
2 changes: 1 addition & 1 deletion flows/translation/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ExtractFromFlows(initialComment string, translationsLanguage envs.Language,
}

func findLocalizedText(translationsLanguage envs.Language, excludeProperties []string, sources []flows.Flow) []*localizedText {
exclude := utils.StringSet(excludeProperties)
exclude := utils.Set(excludeProperties)
extracted := make([]*localizedText, 0)

for _, flow := range sources {
Expand Down
18 changes: 18 additions & 0 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"reflect"

"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

// IsNil returns whether the given object is nil or an interface to a nil
Expand Down Expand Up @@ -39,6 +41,22 @@ func Min[T constraints.Ordered](x, y T) T {
return y
}

// Set converts a slice to a set (a K > bool map)
func Set[K constraints.Ordered](s []K) map[K]bool {
m := make(map[K]bool, len(s))
for _, v := range s {
m[v] = true
}
return m
}

// SortedKeys returns the keys of a set in lexical order
func SortedKeys[K constraints.Ordered](m map[K]bool) []K {
keys := maps.Keys(m)
slices.Sort(keys)
return keys
}

// Typed is an interface of objects that are marshalled as typed envelopes
type Typed interface {
Type() string
Expand Down
12 changes: 12 additions & 0 deletions utils/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func TestMin(t *testing.T) {
assert.Equal(t, uint16(0), utils.Min(uint16(0), uint16(1)))
}

func TestSortedKeys(t *testing.T) {
assert.Equal(t, []string{}, utils.SortedKeys(map[string]bool{}))
assert.Equal(t, []string{"a", "x", "y"}, utils.SortedKeys(map[string]bool{"x": true, "y": true, "a": true}))
assert.Equal(t, []int{3, 5, 6}, utils.SortedKeys(map[int]bool{6: true, 3: true, 5: true}))
}

func TestSet(t *testing.T) {
assert.Equal(t, map[string]bool{}, utils.Set[string](nil))
assert.Equal(t, map[string]bool{}, utils.Set([]string{}))
assert.Equal(t, map[string]bool{"x": true, "y": true, "a": true}, utils.Set([]string{"a", "x", "y"}))
}

func TestReadTypeFromJSON(t *testing.T) {
_, err := utils.ReadTypeFromJSON([]byte(`{}`))
assert.EqualError(t, err, "field 'type' is required")
Expand Down
20 changes: 0 additions & 20 deletions utils/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"regexp"
"sort"
"strings"

"github.com/blevesearch/segment"
Expand Down Expand Up @@ -83,25 +82,6 @@ func StringSliceContains(slice []string, str string, caseSensitive bool) bool {
return false
}

// StringSet converts a slice of strings to a set (a string > bool map)
func StringSet(s []string) map[string]bool {
m := make(map[string]bool, len(s))
for _, v := range s {
m[v] = true
}
return m
}

// StringSetKeys returns the keys of string set in lexical order
func StringSetKeys(m map[string]bool) []string {
vals := make([]string, 0, len(m))
for v := range m {
vals = append(vals, v)
}
sort.Strings(vals)
return vals
}

// Indent indents each non-empty line in the given string
func Indent(s string, prefix string) string {
output := strings.Builder{}
Expand Down
11 changes: 0 additions & 11 deletions utils/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,3 @@ func TestIndent(t *testing.T) {
assert.Equal(t, " x\n\n y", utils.Indent("x\n\ny", " "))
assert.Equal(t, ">>>x", utils.Indent("x", ">>>"))
}

func TestStringSet(t *testing.T) {
assert.Equal(t, map[string]bool{}, utils.StringSet(nil))
assert.Equal(t, map[string]bool{}, utils.StringSet([]string{}))
assert.Equal(t, map[string]bool{"x": true, "y": true, "a": true}, utils.StringSet([]string{"a", "x", "y"}))
}

func TestStringSetKeys(t *testing.T) {
assert.Equal(t, []string{}, utils.StringSetKeys(map[string]bool{}))
assert.Equal(t, []string{"a", "x", "y"}, utils.StringSetKeys(map[string]bool{"x": true, "y": true, "a": true}))
}

0 comments on commit 81ac7a8

Please sign in to comment.