Skip to content

Commit 330c5f4

Browse files
authored
Fix withValue on record column type (#92)
This PR fixes issue #87 where `WithValue()` wasn't unmarshaling the record.
1 parent f9e0f03 commit 330c5f4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

column_record.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ func ForRecord[T recordType](new func() T, opts ...func(*option[T])) Column {
7171
}
7272
}
7373

74+
// Value returns the value at the given index
75+
// TODO: should probably get rid of this and use an `rdRecord` instead
76+
func (c *columnRecord) Value(idx uint32) (out any, has bool) {
77+
if v, ok := c.columnString.Value(idx); ok {
78+
out = c.pool.New()
79+
has = out.(encoding.BinaryUnmarshaler).UnmarshalBinary(s2b(v.(string))) == nil
80+
}
81+
return
82+
}
83+
7484
// --------------------------- Writer ----------------------------
7585

7686
// rwRecord represents read-write accessor for primary keys.

column_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,24 @@ func TestNumberMerge(t *testing.T) {
734734
})
735735
}
736736

737+
func TestIssue87(t *testing.T) {
738+
table := NewCollection()
739+
table.CreateColumn("birthdate", ForRecord(func() *time.Time { return new(time.Time) }))
740+
741+
srcDate := time.Date(1999, 3, 2, 12, 0, 0, 0, time.Local)
742+
table.Insert(func(r Row) error {
743+
r.SetRecord("birthdate", srcDate)
744+
return nil
745+
})
746+
747+
table.Query(func(txn *Txn) error {
748+
assert.Equal(t, txn.WithValue("birthdate", func(v any) bool {
749+
return v.(*time.Time).Equal(srcDate)
750+
}).Count(), 1)
751+
return nil
752+
})
753+
}
754+
737755
// Tests the case where a column is created after inserting a large enough amount of data
738756
func TestIssue89(t *testing.T) {
739757
coll := NewCollection()

0 commit comments

Comments
 (0)