From 356730208b8b909a9959272e99b1a79c0246cb63 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 17 Jul 2026 19:03:48 +0200 Subject: add type Selection --- runtimeindex.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'runtimeindex.go') 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 } -- cgit v1.3.1