Skip to content

Commit

Permalink
Update/fix stream batch funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvivien committed Jan 11, 2025
1 parent 7e42598 commit 020bd5a
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 280 deletions.
64 changes: 33 additions & 31 deletions operators/batch/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ func SortSliceByIndexFunc[SLICE ~[][]ITEM, ITEM cmp.Ordered](index int) api.Exec
// The function returns a sorted []T
func SortByStructFieldFunc[SLICE ~[]ITEM, ITEM any](name string) api.ExecFunction[SLICE, SLICE] {
return func(ctx context.Context, param0 SLICE) SLICE {
if reflect.TypeOf(param0).Elem().Kind() != reflect.Struct {
panic("SortByStructField requires struct")
}

slices.SortFunc(param0, func(i, j ITEM) int {
fieldI := reflect.ValueOf(i).FieldByName(name)
Expand Down Expand Up @@ -306,39 +309,38 @@ func SortByStructFieldFunc[SLICE ~[]ITEM, ITEM any](name string) api.ExecFunctio
// []map[K]V - where K is a comparable type
//
// The function returns sorted []map[K]
func SortByMapKeyFunc[MAPS ~[]map[K]V, K comparable, V any](key K) api.ExecFunction[MAPS, MAPS] {
func SortByMapKeyFunc[MAPS ~[]map[K]V, K comparable, V cmp.Ordered](key K) api.ExecFunction[MAPS, MAPS] {
return func(ctx context.Context, param0 MAPS) MAPS {
slices.SortFunc(param0, func(i, j map[K]V) int {
itemI, okI := i[key]
itemJ, okJ := j[key]

// Handle cases where the key might not exist in one or both maps
if !okI && !okJ {
return 0 // Both missing, consider equal
}
if !okI && okJ {
return 1 // Only j has the key, j is "greater"
}
if okI && !okJ {
return -1 // Only i has the key, i is "less"
}

if okI && okJ {
valI := reflect.ValueOf(itemI)
valJ := reflect.ValueOf(itemJ)
switch {
case util.IsEqual(valI, valJ):
return 0
case util.IsLess(valI, valJ):
return -1
case util.IsMore(valI, valJ):
return 1
default:
return 0
}
}

return 0
return cmp.Compare(i[key], j[key])

// // Handle cases where the key might not exist in one or both maps
// if !okI && !okJ {
// return 0 // Both missing, consider equal
// }
// if !okI && okJ {
// return 1 // Only j has the key, j is "greater"
// }
// if okI && !okJ {
// return -1 // Only i has the key, i is "less"
// }

// if okI && okJ {
// valI := reflect.ValueOf(itemI)
// valJ := reflect.ValueOf(itemJ)
// switch {
// case util.IsEqual(valI, valJ):
// return 0
// case util.IsLess(valI, valJ):
// return -1
// case util.IsMore(valI, valJ):
// return 1
// default:
// return 0
// }
// }

// return 0
})
return param0
}
Expand Down
27 changes: 19 additions & 8 deletions operators/batch/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cmp"
"context"
"fmt"
"slices"
"testing"
)

Expand Down Expand Up @@ -170,8 +171,7 @@ func TestSortSlice(t *testing.T) {
op := SortSliceFunc[[]string]()
data := []string{"Spirit", "Voyager", "BigFoot", "Enola", "Memphis"}
sorted := op(context.TODO(), data)

if sorted[0] != "BigFoot" && sorted[1] != "Enola" && sorted[2] != "Memphis" {
if !slices.IsSorted[[]string](sorted) {
t.Fatal("unexpected sort order for result: ", sorted)
}
}
Expand All @@ -186,7 +186,11 @@ func TestSortSliceByIndex(t *testing.T) {
{"Memphis", "plane", "propeller"},
}
sorted := op(context.TODO(), data)
if sorted[0][0] != "BigFoot" && sorted[1][0] != "Enola" && sorted[2][0] != "Memphis" {
var col []string
for _, row := range sorted {
col = append(col, row[0])
}
if !slices.IsSorted[[]string](col) {
t.Fatal("unexpected sort order for result: ", sorted)
}
}
Expand All @@ -208,8 +212,12 @@ func TestSortByStructField(t *testing.T) {
}
sorted := op(context.TODO(), data)
fmt.Printf("sorted: %v\n", sorted)
if sorted[0].Vehicle != "BigFoot" && sorted[1].Vehicle != "Enola" && sorted[2].Vehicle != "Memphis" {
t.Fatal("Unexpected sort order")
var col []string
for _, row := range sorted {
col = append(col, row.Vehicle)
}
if !slices.IsSorted[[]string](col) {
t.Fatal("unexpected sort order for result: ", sorted)
}
}

Expand All @@ -223,9 +231,12 @@ func TestSortByMapKey(t *testing.T) {
{"Vehicle": "Memphis", "Kind": "plane", "Engine": "propeller"},
}
sorted := op(context.TODO(), data)

if sorted[0]["Vehicle"] != "BigFoot" && sorted[1]["Vehicle"] != "Enola" && sorted[2]["Vehicle"] != "Memphis" {
t.Fatal("Unexpected sort order")
var col []string
for _, row := range sorted {
col = append(col, row["Vehicle"])
}
if !slices.IsSorted[[]string](col) {
t.Fatal("unexpected sort order for result: ", sorted)
}
}

Expand Down
4 changes: 2 additions & 2 deletions operators/executor/batch_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func SortByStructField[SLICE ~[]ITEM, ITEM any](name string) *FuncExecutor[SLICE
return Execute(batch.SortByStructFieldFunc[SLICE](name))
}

func SortByMapKey[MAPS ~[]map[K]V, K comparable, V any](key K) *FuncExecutor[MAPS, MAPS] {
func SortByMapKey[MAPS ~[]map[K]V, K comparable, V cmp.Ordered](key K) *FuncExecutor[MAPS, MAPS] {
return Execute(batch.SortByMapKeyFunc[MAPS](key))
}

func SortWithFunc[SLICE ~[]ITEM, ITEM any](f func(i, j ITEM) int) *FuncExecutor[SLICE, SLICE] {
return Execute(batch.SortWithFuncFunc[SLICE](f))
}
}
Loading

0 comments on commit 020bd5a

Please sign in to comment.