FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

handle arbitrary slice types in sort builtin by netliomax25-code · Pull Request #968 · expr-lang/expr · GitHub

/ expr Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions builtin/builtin.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,19 @@ var Builtins = []*Function{
for i, v := range in {
array[i] = v
}
default:
v := reflect.ValueOf(args[0])
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
switch v.Type().Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool:
array = make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
array[i] = v.Index(i).Interface()
}
}
}
}

var desc bool
Expand Down
20 changes: 20 additions & 0 deletions builtin/builtin_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,26 @@ func TestBuiltin_sort_i64(t *testing.T) {
assert.Equal(t, []any{int64(1), int64(1), int64(1)}, out)
}

func TestBuiltin_sort_non_standard_slice(t *testing.T) {
tests := []struct {
input string
env any
want any
}{
{`sort(x)`, map[string]any{"x": []int64{3, 1, 2}}, []any{int64(1), int64(2), int64(3)}},
{`sort(x)`, map[string]any{"x": []uint{3, 1, 2}}, []any{uint(1), uint(2), uint(3)}},
{`sort(x, "desc")`, map[string]any{"x": []int32{1, 3, 2}}, []any{int32(3), int32(2), int32(1)}},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
out, err := expr.Eval(test.input, test.env)
require.NoError(t, err)
assert.Equal(t, test.want, out)
})
}
}

func TestBuiltin_bitOpsFunc(t *testing.T) {
tests := []struct {
input string
Expand Down

Back | FazBrowse Home | New Git URL