Skip to content

Commit

Permalink
Fix withValue on record column type (#92)
Browse files Browse the repository at this point in the history
This PR fixes issue #87 where `WithValue()` wasn't unmarshaling the
record.
  • Loading branch information
kelindar authored Jul 3, 2023
1 parent f9e0f03 commit 330c5f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions column_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func ForRecord[T recordType](new func() T, opts ...func(*option[T])) Column {
}
}

// Value returns the value at the given index
// TODO: should probably get rid of this and use an `rdRecord` instead
func (c *columnRecord) Value(idx uint32) (out any, has bool) {
if v, ok := c.columnString.Value(idx); ok {
out = c.pool.New()
has = out.(encoding.BinaryUnmarshaler).UnmarshalBinary(s2b(v.(string))) == nil
}
return
}

// --------------------------- Writer ----------------------------

// rwRecord represents read-write accessor for primary keys.
Expand Down
18 changes: 18 additions & 0 deletions column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,24 @@ func TestNumberMerge(t *testing.T) {
})
}

func TestIssue87(t *testing.T) {
table := NewCollection()
table.CreateColumn("birthdate", ForRecord(func() *time.Time { return new(time.Time) }))

srcDate := time.Date(1999, 3, 2, 12, 0, 0, 0, time.Local)
table.Insert(func(r Row) error {
r.SetRecord("birthdate", srcDate)
return nil
})

table.Query(func(txn *Txn) error {
assert.Equal(t, txn.WithValue("birthdate", func(v any) bool {
return v.(*time.Time).Equal(srcDate)
}).Count(), 1)
return nil
})
}

// Tests the case where a column is created after inserting a large enough amount of data
func TestIssue89(t *testing.T) {
coll := NewCollection()
Expand Down

0 comments on commit 330c5f4

Please sign in to comment.