diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-11-10 00:53:34 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-11-10 00:53:34 +0100 |
| commit | 5125060a1726c795219350c01ca23f1f99d9b08c (patch) | |
| tree | a74519621a7d73fd46f5175dd0072403841259d1 | |
| parent | 1cc8b3d25658eb3ee320cb43265ddae289649786 (diff) | |
| download | go-oblast-5125060a1726c795219350c01ca23f1f99d9b08c.tar.gz | |
move RuntimeIndex
| -rw-r--r-- | CHANGELOG.md | 9 | ||||
| -rw-r--r-- | go.mod | 4 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | oblast.go | 8 | ||||
| -rw-r--r-- | runtimeindex.go | 65 | ||||
| -rw-r--r-- | runtimeindex_test.go | 73 |
6 files changed, 21 insertions, 142 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 43dc820..ecf03d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> SPDX-License-Identifier: Apache-2.0 --> +# v0.18.0 (TBD) + +API changes: + +- The remaining non-aliased symbols, `type RuntimeIndex` and `func NewRuntimeIndex`, become synonyms for the identical symbols in `go.xyrillian.de/gg/oblast` within `gg@v1.21.0`. + +Since this module has been fully superseded by `go.xyrillian.de/gg/oblast`, this is the last release of this module. +In a fitting coincidence, on its 18th "birthday", Oblast comes of age. + # v0.17.0 (2026-09-17) API changes: @@ -1,5 +1,5 @@ module go.xyrillian.de/oblast -go 1.26 +go 1.27 -require go.xyrillian.de/gg v1.16.0 +require go.xyrillian.de/gg v1.21.0 @@ -1,2 +1,2 @@ -go.xyrillian.de/gg v1.16.0 h1:bgwPxhyP2uWaZ5nYfXdBKj/TsAY6r+iHerFilGRQi38= -go.xyrillian.de/gg v1.16.0/go.mod h1:KU+qaSkaYRywgMOJtSR3O1r9RH2WQc2t8Zc3NAFsnSU= +go.xyrillian.de/gg v1.21.0 h1:JKRC1D8y6EmPah/qtyva4feq5RH48XSymfUojEHBL3I= +go.xyrillian.de/gg v1.21.0/go.mod h1:vtnzywY+fM0yVNdPZ1aunjhE/P0iBs7t/rB9rCo6hFE= @@ -79,3 +79,11 @@ func TupleSelect[R any](ctx context.Context, db gsql.Handle, query string, args func TupleSelectOne[R any](ctx context.Context, db gsql.Handle, query string, args ...any) (R, error) { return gg_oblast.TupleSelectOne[R](ctx, db, query, args...) } + +// RuntimeIndex has moved to gg/oblast (follow the link below). +type RuntimeIndex[R any, K comparable] = gg_oblast.RuntimeIndex[R, K] + +// NewRuntimeIndex has moved to gg/oblast (follow the link below). +func NewRuntimeIndex[R any, K comparable](f func(R) K) RuntimeIndex[R, K] { + return gg_oblast.NewRuntimeIndex(f) +} diff --git a/runtimeindex.go b/runtimeindex.go deleted file mode 100644 index 8167ba6..0000000 --- a/runtimeindex.go +++ /dev/null @@ -1,65 +0,0 @@ -// 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 the result of [Store.Select] or [Store.SelectWhere], to build a lookup table for or partition of the retrieved records. -type RuntimeIndex[R any, K comparable] func(R) K - -// TODO: rename Index() to Of(), IndexFrom() to From(), Partition() to PartitionOf() (in practice, instances of RuntimeIndex are often named e.g. ObjectByIDIndex, and then ObjectByIDIndex.IndexFrom() stutters) -> naming might need some workshopping -// TODO: make Index() and Partition() take an iter.Seq[], or alternatively add additional methods that do - -// 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 [Store.Select] or [Store.SelectWhere] call. -// 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 [Store.Select] or [Store.SelectWhere] call. -// 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/runtimeindex_test.go b/runtimeindex_test.go deleted file mode 100644 index dd65b07..0000000 --- a/runtimeindex_test.go +++ /dev/null @@ -1,73 +0,0 @@ -// 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/oblast" - "go.xyrillian.de/oblast/internal/testhelpers/mock" - "go.xyrillian.de/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`) - }) -} |
