This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Floor/Ceil and TimeRange for time-based ID queries #5
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3eb9387
feat: add Floor/Ceil functions and TimeRange for time-based ID queries
klaidliadon 8a7646c
refactor: move timerange to sub-package with UUID/Int64 interfaces
klaidliadon eb0009a
refactor: remove generics from Floor/Ceil, use interface return types
klaidliadon 7e6a11f
refactor: remove generics from TimeRange, use driver.Valuer
klaidliadon 0740f1c
feat: add GetTime, Floor, Ceil methods to UUID/Int64 interfaces
klaidliadon 43a1ec6
refactor: unify UUID/Int64 into generic ID[T] struct
klaidliadon 797d89d
refactor: move timerange back into main package, eliminate ID[T]
klaidliadon 183c6ed
docs: add godoc to Floor/Ceil, GetTime, TimeRange, and constructors
klaidliadon e222349
refactor: unexport Floor/Ceil functions, move tests to internal package
klaidliadon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package typeid | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| type testUserPrefix struct{} | ||
|
|
||
| func (testUserPrefix) Prefix() string { return "user" } | ||
|
|
||
| type testOrgPrefix struct{} | ||
|
|
||
| func (testOrgPrefix) Prefix() string { return "org" } | ||
|
|
||
| func TestFloorUUID(t *testing.T) { | ||
| now := time.Now() | ||
| floor := floorUUID[testUserPrefix](now) | ||
| u := floor.UUID() | ||
|
|
||
| if u.Version() != 7 { | ||
| t.Fatalf("version = %d, want 7", u.Version()) | ||
| } | ||
| if u.Variant() != uuid.RFC4122 { | ||
| t.Fatalf("variant = %d, want RFC4122", u.Variant()) | ||
| } | ||
|
|
||
| // Any UUIDv7 generated after now must be >= floor. | ||
| for range 100 { | ||
| id, err := NewUUID[testUserPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if id.UUID().String() < u.String() { | ||
| t.Fatalf("NewUUID %s < floor %s", id.UUID(), u) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCeilUUID(t *testing.T) { | ||
| now := time.Now() | ||
| ceil := ceilUUID[testUserPrefix](now) | ||
| floor := floorUUID[testUserPrefix](now) | ||
|
|
||
| u := ceil.UUID() | ||
| if u.Version() != 7 { | ||
| t.Fatalf("version = %d, want 7", u.Version()) | ||
| } | ||
| if u.Variant() != uuid.RFC4122 { | ||
| t.Fatalf("variant = %d, want RFC4122", u.Variant()) | ||
| } | ||
| if ceil.UUID().String() < floor.UUID().String() { | ||
| t.Fatalf("ceil %s < floor %s", ceil.UUID(), floor.UUID()) | ||
| } | ||
| } | ||
|
|
||
| func TestFloorCeilUUID_Bracket(t *testing.T) { | ||
| id, err := NewUUID[testUserPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| u := id.UUID() | ||
| ts := id.GetTime() | ||
|
|
||
| floor := floorUUID[testUserPrefix](ts) | ||
| ceil := ceilUUID[testUserPrefix](ts) | ||
|
|
||
| if floor.UUID().String() > u.String() { | ||
| t.Fatalf("floor %s > id %s", floor.UUID(), u) | ||
| } | ||
| if ceil.UUID().String() < u.String() { | ||
| t.Fatalf("ceil %s < id %s", ceil.UUID(), u) | ||
| } | ||
| } | ||
|
|
||
| func TestFloorUUID_TimestampRoundTrip(t *testing.T) { | ||
| ts := time.Date(2026, 3, 27, 12, 0, 0, 0, time.UTC) | ||
| floor := floorUUID[testUserPrefix](ts) | ||
| if got := floor.GetTime(); got.UnixMilli() != ts.UnixMilli() { | ||
| t.Fatalf("GetTime() = %v, want %v", got, ts) | ||
| } | ||
| } | ||
|
|
||
| func TestFloorInt64(t *testing.T) { | ||
| now := time.Now() | ||
| floor := floorInt64[testOrgPrefix](now) | ||
|
|
||
| for range 100 { | ||
| id, err := NewInt64[testOrgPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if id.Int64() < floor.Int64() { | ||
| t.Fatalf("NewInt64 %d < floor %d", id.Int64(), floor.Int64()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCeilInt64(t *testing.T) { | ||
| now := time.Now() | ||
| ceil := ceilInt64[testOrgPrefix](now) | ||
| floor := floorInt64[testOrgPrefix](now) | ||
|
|
||
| if ceil.Int64() < floor.Int64() { | ||
| t.Fatalf("ceil %d < floor %d", ceil.Int64(), floor.Int64()) | ||
| } | ||
| } | ||
|
|
||
| func TestFloorCeilInt64_Bracket(t *testing.T) { | ||
| id, err := NewInt64[testOrgPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| v := id.Int64() | ||
| ts := id.GetTime() | ||
|
|
||
| floor := floorInt64[testOrgPrefix](ts) | ||
| ceil := ceilInt64[testOrgPrefix](ts) | ||
|
|
||
| if floor.Int64() > v { | ||
| t.Fatalf("floor %d > id %d", floor.Int64(), v) | ||
| } | ||
| if ceil.Int64() < v { | ||
| t.Fatalf("ceil %d < id %d", ceil.Int64(), v) | ||
| } | ||
| } | ||
|
|
||
| func TestInt64_GetTime(t *testing.T) { | ||
| ts := time.Date(2026, 3, 27, 12, 0, 0, 0, time.UTC) | ||
| floor := floorInt64[testOrgPrefix](ts) | ||
| if got := floor.GetTime(); got.UnixMilli() != ts.UnixMilli() { | ||
| t.Fatalf("GetTime() = %v, want %v", got, ts) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package typeid | ||
|
|
||
| import ( | ||
| "database/sql/driver" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // TimeRange holds optional floor/ceil bounds for time-based ID range queries | ||
| // against a primary key column. It satisfies squirrel.Sqlizer structurally | ||
| // via [TimeRange.ToSql], so it can be passed directly to squirrel.Where | ||
| // without importing squirrel in this package. | ||
| // | ||
| // Construct via [UUIDRange] or [Int64Range]. | ||
| type TimeRange struct { | ||
| column string | ||
| floor driver.Valuer | ||
| ceil driver.Valuer | ||
| } | ||
|
|
||
| // UUIDRange builds a [TimeRange] that brackets column with [FloorUUID] / [CeilUUID]. | ||
| // Nil since or until leaves that side unbounded. | ||
| func UUIDRange[P Prefixer](column string, since, until *time.Time) TimeRange { | ||
| r := TimeRange{column: column} | ||
| if since != nil { | ||
| r.floor = floorUUID[P](*since) | ||
| } | ||
| if until != nil { | ||
| r.ceil = ceilUUID[P](*until) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| // Int64Range builds a [TimeRange] that brackets column with [FloorInt64] / [CeilInt64]. | ||
| // Nil since or until leaves that side unbounded. | ||
| func Int64Range[P Prefixer](column string, since, until *time.Time) TimeRange { | ||
| r := TimeRange{column: column} | ||
| if since != nil { | ||
| r.floor = floorInt64[P](*since) | ||
| } | ||
| if until != nil { | ||
| r.ceil = ceilInt64[P](*until) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| // Floor returns the lower-bound ID and true, or (nil, false) if unbounded. | ||
| func (r TimeRange) Floor() (driver.Valuer, bool) { return r.floor, r.floor != nil } | ||
|
|
||
| // Ceil returns the upper-bound ID and true, or (nil, false) if unbounded. | ||
| func (r TimeRange) Ceil() (driver.Valuer, bool) { return r.ceil, r.ceil != nil } | ||
|
|
||
| // ToSql emits a SQL predicate and bind args for the range. | ||
| // Returns "column BETWEEN ? AND ?", "column >= ?", "column <= ?", | ||
| // or "1=1" depending on which bounds are set. | ||
| func (r TimeRange) ToSql() (string, []any, error) { | ||
| switch { | ||
| case r.floor != nil && r.ceil != nil: | ||
| fv, err := r.floor.Value() | ||
| if err != nil { | ||
| return "", nil, fmt.Errorf("typeid: floor value: %w", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an interesting experiment. I think the code would belong somewhere in pgkit or pgx. But, this feels like a very specialized query, doesn't it? What is the use case for time range? Usually, we'd need cursor-based pagination, which can be done directly with the ID value. I guess this is for queries like "give me all items created in the last 24h"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is exactly the use case |
||
| } | ||
| cv, err := r.ceil.Value() | ||
| if err != nil { | ||
| return "", nil, fmt.Errorf("typeid: ceil value: %w", err) | ||
| } | ||
| return r.column + " BETWEEN ? AND ?", []any{fv, cv}, nil | ||
| case r.floor != nil: | ||
| fv, err := r.floor.Value() | ||
| if err != nil { | ||
| return "", nil, fmt.Errorf("typeid: floor value: %w", err) | ||
| } | ||
| return r.column + " >= ?", []any{fv}, nil | ||
| case r.ceil != nil: | ||
| cv, err := r.ceil.Value() | ||
| if err != nil { | ||
| return "", nil, fmt.Errorf("typeid: ceil value: %w", err) | ||
| } | ||
| return r.column + " <= ?", []any{cv}, nil | ||
| default: | ||
| return "1=1", nil, nil | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package typeid_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-chi/typeid" | ||
| ) | ||
|
|
||
| func TestUUID_GetTime(t *testing.T) { | ||
| id, err := typeid.NewUUID[userPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := id.GetTime() | ||
| if got.IsZero() { | ||
| t.Fatal("GetTime() returned zero") | ||
| } | ||
| if time.Since(got) > time.Second { | ||
| t.Fatalf("GetTime() = %v, too far from now", got) | ||
| } | ||
| } | ||
|
|
||
| func TestInt64_GetTime(t *testing.T) { | ||
| id, err := typeid.NewInt64[orgPrefix]() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := id.GetTime() | ||
| if got.IsZero() { | ||
| t.Fatal("GetTime() returned zero") | ||
| } | ||
| if time.Since(got) > time.Second { | ||
| t.Fatalf("GetTime() = %v, too far from now", got) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_ToSql_BothSet(t *testing.T) { | ||
| since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC) | ||
| r := typeid.UUIDRange[userPrefix]("id", &since, &until) | ||
|
|
||
| sql, args, err := r.ToSql() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if sql != "id BETWEEN ? AND ?" { | ||
| t.Fatalf("sql = %q", sql) | ||
| } | ||
| if len(args) != 2 { | ||
| t.Fatalf("args len = %d, want 2", len(args)) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_ToSql_SinceOnly(t *testing.T) { | ||
| since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| r := typeid.UUIDRange[userPrefix]("id", &since, nil) | ||
|
|
||
| sql, args, err := r.ToSql() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if sql != "id >= ?" { | ||
| t.Fatalf("sql = %q", sql) | ||
| } | ||
| if len(args) != 1 { | ||
| t.Fatalf("args len = %d, want 1", len(args)) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_ToSql_UntilOnly(t *testing.T) { | ||
| until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC) | ||
| r := typeid.UUIDRange[userPrefix]("id", nil, &until) | ||
|
|
||
| sql, args, err := r.ToSql() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if sql != "id <= ?" { | ||
| t.Fatalf("sql = %q", sql) | ||
| } | ||
| if len(args) != 1 { | ||
| t.Fatalf("args len = %d, want 1", len(args)) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_ToSql_Neither(t *testing.T) { | ||
| r := typeid.UUIDRange[userPrefix]("id", nil, nil) | ||
|
|
||
| sql, args, err := r.ToSql() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if sql != "1=1" { | ||
| t.Fatalf("sql = %q", sql) | ||
| } | ||
| if len(args) != 0 { | ||
| t.Fatalf("args len = %d, want 0", len(args)) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_ToSql_Int64(t *testing.T) { | ||
| since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC) | ||
| r := typeid.Int64Range[orgPrefix]("id", &since, &until) | ||
|
|
||
| sql, args, err := r.ToSql() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if sql != "id BETWEEN ? AND ?" { | ||
| t.Fatalf("sql = %q", sql) | ||
| } | ||
| if len(args) != 2 { | ||
| t.Fatalf("args len = %d, want 2", len(args)) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeRange_FloorCeil_Accessors(t *testing.T) { | ||
| since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| r := typeid.UUIDRange[userPrefix]("id", &since, nil) | ||
|
|
||
| if _, ok := r.Floor(); !ok { | ||
| t.Fatal("Floor() returned false") | ||
| } | ||
| if _, ok := r.Ceil(); ok { | ||
| t.Fatal("Ceil() returned true for nil until") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.