diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-11-10 00:45:07 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-11-10 00:45:07 +0100 |
| commit | c477d02ff44583661a22515237230aa7fd2eacfb (patch) | |
| tree | fa7a1ad8787e8d33f66cb8ddd85e5968f1fb4aa7 | |
| parent | 3117df7f4e2703e17f7840c4f948569e72f57219 (diff) | |
| download | go-gg-c477d02ff44583661a22515237230aa7fd2eacfb.tar.gz | |
oblast: add type RuntimeIndex from go.xyrillian.de/oblast
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | oblast/runtimeindex.go | 65 | ||||
| -rw-r--r-- | oblast/runtimeindex_test.go | 73 | ||||
| -rw-r--r-- | oblast/select.go | 4 |
4 files changed, 147 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 475d26e..e79c742 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> SPDX-License-Identifier: Apache-2.0 --> +# v1.21.0 (TBD) + +Changes: + +- oblast: Add type RuntimeIndex. + I am done contemplating the method naming on this type, and decided that I am fine with it as-is. + # v1.20.0 (2026-11-10) Changes: diff --git a/oblast/runtimeindex.go b/oblast/runtimeindex.go new file mode 100644 index 0000000..bc0051b --- /dev/null +++ b/oblast/runtimeindex.go @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> +// SPDX-License-Identifier: Apache-2.0 + +package oblast + +// RuntimeIndex provides methods for sorting records (R) by some type of key (K) at runtime. +// It is most commonly used with any of the functions returning a [Selection], +// to build a lookup table for or partition of the retrieved records. +type RuntimeIndex[R any, K comparable] func(R) K + +// TODO: add additional methods like Index() and Partition() that take an iter.Seq[] + +// NewRuntimeIndex casts a function into type [RuntimeIndex]. +// +// In practice, this is more compact than writing the cast directly +// because type arguments can be inferred for function calls, but not type casts. +func NewRuntimeIndex[R any, K comparable](f func(R) K) RuntimeIndex[R, K] { + return RuntimeIndex[R, K](f) +} + +// Index builds a lookup table of the provided records. +// +// This should only be used when the index yields unique values for each record. +// If there can be duplicates, use [RuntimeIndex.Partition] instead. +func (i RuntimeIndex[R, K]) Index(records []R) map[K]R { + result := make(map[K]R, len(records)) + for _, r := range records { + result[i(r)] = r + } + return result +} + +// IndexFrom is like Index, but can directly wrap a call that returns a [Selection]. +// If there is an error, it is passed through unchanged. +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. +// Within each partition, the original order of records is retained. +func (i RuntimeIndex[R, K]) Partition(records []R) map[K][]R { + result := make(map[K][]R, len(records)) + for _, r := range records { + key := i(r) + result[key] = append(result[key], r) + } + return result +} + +// PartitionFrom is like Partition, but can directly wrap a call that returns a [Selection]. +// If there is an error, it is passed through unchanged. +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 +} diff --git a/oblast/runtimeindex_test.go b/oblast/runtimeindex_test.go new file mode 100644 index 0000000..ee4f8f9 --- /dev/null +++ b/oblast/runtimeindex_test.go @@ -0,0 +1,73 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> +// SPDX-License-Identifier: Apache-2.0 + +package oblast_test + +import ( + "database/sql" + "testing" + + "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/gsql" + "go.xyrillian.de/gg/oblast" + "go.xyrillian.de/gg/oblast/internal/testhelpers/mock" + "go.xyrillian.de/gg/oblast/internal/testhelpers/must" +) + +func TestRuntimeIndex(t *testing.T) { + ctx := t.Context() + md := mock.NewDriver() + db := gsql.NewDB(sql.OpenDB(md)) + + type basicRecord struct { + ID int64 `db:"id"` + Name string `db:"name"` + } + store := oblast.MustNewStore[basicRecord]( + oblast.SqliteDialect(), + oblast.TableNameIs("basic_records"), + oblast.PrimaryKeyIs("id"), + ) + + commonSetup := func() { + md.ForQuery(`SELECT "id", "name" FROM "basic_records" WHERE id > 0`). + ExpectQueryWithArgs(). + AndReturnColumns("id", "name"). + WithRow(1, "foo"). + WithRow(2, "bar"). + WithRow(3, "baz") + } + + t.Run("Index", func(t *testing.T) { + byName := oblast.NewRuntimeIndex(func(r basicRecord) string { return r.Name }) + + // test success path + commonSetup() + result := must.Return(byName.IndexFrom(store.SelectWhere(ctx, db, `id > 0`)))(t) + assert.Equal(t, result, map[string]basicRecord{ + "foo": {1, "foo"}, + "bar": {2, "bar"}, + "baz": {3, "baz"}, + }) + + // test error path + _, err := byName.IndexFrom(store.SelectWhere(ctx, db, `id = 1`)) + assert.ErrEqual(t, err, `during Query(): unexpected query: SELECT "id", "name" FROM "basic_records" WHERE id = 1`) + }) + + t.Run("Partition", func(t *testing.T) { + byFirstLetter := oblast.NewRuntimeIndex(func(r basicRecord) string { return r.Name[0:1] }) + + // test success path + commonSetup() + result := must.Return(byFirstLetter.PartitionFrom(store.SelectWhere(ctx, db, `id > 0`)))(t) + assert.Equal(t, result, map[string][]basicRecord{ + "f": {{1, "foo"}}, + "b": {{2, "bar"}, {3, "baz"}}, + }) + + // test error path + _, err := byFirstLetter.PartitionFrom(store.SelectWhere(ctx, db, `id = 1`)) + assert.ErrEqual(t, err, `during Query(): unexpected query: SELECT "id", "name" FROM "basic_records" WHERE id = 1`) + }) +} diff --git a/oblast/select.go b/oblast/select.go index 66ba9e8..a0d10d5 100644 --- a/oblast/select.go +++ b/oblast/select.go @@ -308,10 +308,10 @@ func selectOneValue(ctx context.Context, db gsql.Handle, target any, query strin //////////////////////////////////////////////////////////////////////////////// // type Selection -// Selection provides access to the result set from a [Select], [Store.Select], [Store.SelectWhere] or [PreparedSelectQuery.Select] call. +// Selection provides access to the result set from a [Select], [TupleSelect], [Store.Select], [Store.SelectWhere] or [PreparedSelectQuery.Select] call. // // Instances of this type are not meant to be held in variables. -// Instead, chain one of its method calls directly after the Select or SelectWhere call to choose how to process the result set. +// Instead, chain one of its method calls directly after the Select/SelectWhere/TupleSelect call to choose how to process the result set. type Selection[R any] struct { selection } |
