Skip to content

Commit

Permalink
Ensure NewForHLC returns an error if the incoming decimal is invalid
Browse files Browse the repository at this point in the history
This shouldn't be possible, but there are some indications it might be
  • Loading branch information
josephschorr committed Jan 29, 2024
1 parent 58df026 commit 37085c4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/datastore/crdb/crdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func readCRDBNow(ctx context.Context, reader pgxcommon.DBFuncQuerier) (datastore
return datastore.NoRevision, fmt.Errorf("unable to read timestamp: %w", err)
}

return revisions.NewForHLC(hlcNow), nil
return revisions.NewForHLC(hlcNow)
}

func readClusterTTLNanos(ctx context.Context, conn pgxcommon.DBFuncQuerier) (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/crdb/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ func updateCounter(ctx context.Context, tx pgx.Tx, change int64) (datastore.Revi
return datastore.NoRevision, fmt.Errorf("unable to executed upsert counter query: %w", err)
}

return revisions.NewForHLC(timestamp), nil
return revisions.NewForHLC(timestamp)
}
5 changes: 2 additions & 3 deletions internal/datastore/revisions/hlcrevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ func HLCRevisionFromString(revisionStr string) (HLCRevision, error) {
}

// NewForHLC creates a new revision for the given hybrid logical clock.
func NewForHLC(decimal decimal.Decimal) HLCRevision {
rev, _ := HLCRevisionFromString(decimal.String())
return rev
func NewForHLC(decimal decimal.Decimal) (HLCRevision, error) {
return HLCRevisionFromString(decimal.String())
}

// NewHLCForTime creates a new revision for the given time.
Expand Down
3 changes: 2 additions & 1 deletion internal/datastore/revisions/hlcrevision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestNewForHLC(t *testing.T) {
d, err := decimal.NewFromString(tc)
require.NoError(t, err)

rev := NewForHLC(d)
rev, err := NewForHLC(d)
require.NoError(t, err)
require.Equal(t, tc, rev.String())
})
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/zedtoken/zedtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,16 @@ var hlcDecodeTests = []struct {
expectError bool
}{
{
format: "V1 ZedToken",
token: "CAIaFQoTMTYyMTUzODE4OTAyODkyODAwMA==",
expectedRevision: revisions.NewForHLC(decimal.NewFromInt(1621538189028928000)),
expectError: false,
format: "V1 ZedToken",
token: "CAIaFQoTMTYyMTUzODE4OTAyODkyODAwMA==",
expectedRevision: func() datastore.Revision {
r, err := revisions.NewForHLC(decimal.NewFromInt(1621538189028928000))
if err != nil {
panic(err)
}
return r
}(),
expectError: false,
},
{
format: "V1 ZedToken",
Expand All @@ -179,7 +185,13 @@ var hlcDecodeTests = []struct {
if err != nil {
panic(err)
}
return revisions.NewForHLC(v)

r, err := revisions.NewForHLC(v)
if err != nil {
panic(err)
}

return r
})(),
expectError: false,
},
Expand Down

0 comments on commit 37085c4

Please sign in to comment.