aboutsummaryrefslogtreecommitdiff
path: root/runtimeindex.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-07-17 19:03:48 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-07-17 19:03:48 +0200
commit356730208b8b909a9959272e99b1a79c0246cb63 (patch)
treefd169f43cac9edc27b5a86037b2893487d5f736b /runtimeindex.go
parent1078ea0c66ff4ee848e512768e793de885cf209d (diff)
downloadgo-oblast-356730208b8b909a9959272e99b1a79c0246cb63.tar.gz
add type Selection
Diffstat (limited to 'runtimeindex.go')
-rw-r--r--runtimeindex.go25
1 files changed, 15 insertions, 10 deletions
diff --git a/runtimeindex.go b/runtimeindex.go
index a5f1f67..394c6a8 100644
--- a/runtimeindex.go
+++ b/runtimeindex.go
@@ -29,11 +29,13 @@ func (i RuntimeIndex[R, K]) Index(records []R) map[K]R {
// IndexFrom is like Index, but can directly wrap a [Store.Select] or [Store.SelectWhere] call.
// If there is an error, it is passed through unchanged.
-func (i RuntimeIndex[R, K]) IndexFrom(records []R, err error) (map[K]R, error) {
- if err != nil {
- return nil, err
- }
- return i.Index(records), nil
+func (i RuntimeIndex[R, K]) IndexFrom(s Selection[R]) (map[K]R, error) {
+ result := make(map[K]R)
+ err := s.Foreach(func(record R) error {
+ result[i(record)] = record
+ return nil
+ })
+ return result, err
}
// Partition builds a partition of the resulting records by their index value.
@@ -49,9 +51,12 @@ func (i RuntimeIndex[R, K]) Partition(records []R) map[K][]R {
// PartitionFrom is like Partition, but can directly wrap a [Store.Select] or [Store.SelectWhere] call.
// If there is an error, it is passed through unchanged.
-func (i RuntimeIndex[R, K]) PartitionFrom(records []R, err error) (map[K][]R, error) {
- if err != nil {
- return nil, err
- }
- return i.Partition(records), nil
+func (i RuntimeIndex[R, K]) PartitionFrom(s Selection[R]) (map[K][]R, error) {
+ result := make(map[K][]R)
+ err := s.Foreach(func(record R) error {
+ key := i(record)
+ result[key] = append(result[key], record)
+ return nil
+ })
+ return result, err
}