2 Commits

Author SHA1 Message Date
a97aafca75 Merge remote-tracking branch 'origin' into refactor/name-bucket-slot-table
All checks were successful
CI / Check PR Title (pull_request) Successful in 30s
CI / Go Lint (pull_request) Successful in 48s
CI / Markdown Lint (pull_request) Successful in 34s
CI / Makefile Lint (pull_request) Successful in 49s
CI / Unit Tests (pull_request) Successful in 48s
CI / Fuzz Tests (pull_request) Successful in 1m17s
CI / Mutation Tests (pull_request) Successful in 1m23s
2026-04-15 23:13:41 -04:00
42c5b5f8f4 feat!: update get from (V, error) to (V, bool) (#20)
All checks were successful
CI / Check PR Title (push) Has been skipped
CI / Go Lint (push) Successful in 43s
CI / Makefile Lint (push) Successful in 41s
CI / Markdown Lint (push) Successful in 32s
CI / Unit Tests (push) Successful in 39s
CI / Fuzz Tests (push) Successful in 1m44s
CI / Mutation Tests (push) Successful in 1m28s
## Description

Currently, the signature for `Table.Get` is `func (K) (V, error)`. This is not very Go-idiomatic, which prefers to return a boolean instead of an error. For instance, a built-in Go map is used like so:

```go
if value, ok := users[id]; !ok {
  // ...
}
```

Updating our table to look like that is best practice. In that same vein, to support direct lookup (i.e. `v := users[id]`), this PR also adds `Table.Find`.

## Changes

- BREAKING CHANGE: Update contract of `Table.Get` to `func (K) (V, bool)`. Returns 'false' is the item cannot be found, and 'true' if it is found.
- Add `Table.Find`.
- Updated tests and documentation to match the change.

### Design Decisions

- Chose to make this decision because throwing an error implies that there is something 'wrong' with the table. There is nothing wrong with the table; it is just that the item does not exist.

## Checklist

- [x] Tests pass
- [x] Docs updated

Reviewed-on: #20
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-04-14 01:58:15 +00:00
4 changed files with 28 additions and 21 deletions

View File

@@ -73,16 +73,16 @@ func FuzzInsertLookup(f *testing.F) {
delete(expected, step.key) delete(expected, step.key)
_, err = actual.Get(step.key) _, ok := actual.Get(step.key)
assert.Error(err) assert.False(ok)
} else { } else {
err := actual.Put(step.key, step.value) err := actual.Put(step.key, step.value)
assert.NoError(err) assert.NoError(err)
expected[step.key] = step.value expected[step.key] = step.value
found, err := actual.Get(step.key) found, ok := actual.Get(step.key)
assert.NoError(err) assert.True(ok)
assert.Equal(step.value, found) assert.Equal(step.value, found)
} }

View File

@@ -108,12 +108,12 @@ func TestGetMany(t *testing.T) {
} }
for i := range 2_000 { for i := range 2_000 {
value, err := table.Get(i) value, ok := table.Get(i)
if i < 1_000 { if i < 1_000 {
assert.NoError(err) assert.True(ok)
assert.Equal(value, true) assert.Equal(value, true)
} else { } else {
assert.Error(err) assert.False(ok)
} }
} }
} }

View File

@@ -14,19 +14,19 @@ func Example_basic() {
fmt.Println("Put error:", err) fmt.Println("Put error:", err)
} }
if item, err := table.Get(1); err != nil { if item, ok := table.Get(1); !ok {
fmt.Println("Error:", err) fmt.Println("Not Found 1!")
} else { } else {
fmt.Println("Found 1:", item) fmt.Println("Found 1:", item)
} }
if item, err := table.Get(0); err != nil { if item, ok := table.Get(0); !ok {
fmt.Println("Error:", err) fmt.Println("Not Found 0!")
} else { } else {
fmt.Println("Found 0:", item) fmt.Println("Found 0:", item)
} }
// Output: // Output:
// Found 1: Hello, World! // Found 1: Hello, World!
// Error: key '0' not found // Not Found 0!
} }

View File

@@ -95,24 +95,31 @@ func (t *Table[K, V]) shrink() error {
return t.resize(t.tableA.capacity / t.growthFactor) return t.resize(t.tableA.capacity / t.growthFactor)
} }
// Get fetches the value for a key in the [Table]. Returns an error if no value // Get fetches the value for a key in the [Table]. Matches the comma-ok pattern
// is found. // of a builtin map; see [Table.Find] for plain indexing.
func (t *Table[K, V]) Get(key K) (value V, err error) { func (t Table[K, V]) Get(key K) (value V, ok bool) {
if item, ok := t.tableA.get(key); ok { if item, ok := t.tableA.get(key); ok {
return item, nil return item, true
} }
if item, ok := t.tableB.get(key); ok { if item, ok := t.tableB.get(key); ok {
return item, nil return item, true
} }
return value, fmt.Errorf("key '%v' not found", key) return
}
// Find fetches the value of a key. Matches direct indexing of a builtin map;
// see [Table.Get] for a comma-ok pattern.
func (t Table[K, V]) Find(key K) (value V) {
value, _ = t.Get(key)
return
} }
// Has returns true if a key has a value in the table. // Has returns true if a key has a value in the table.
func (t *Table[K, V]) Has(key K) (exists bool) { func (t Table[K, V]) Has(key K) (exists bool) {
_, err := t.Get(key) _, exists = t.Get(key)
return err == nil return
} }
// Put sets the value for a key. Returns error if its value cannot be set. // Put sets the value for a key. Returns error if its value cannot be set.