Skip to content

Commit 89898df

Browse files
author
Anthony Regeda
committed
Added Get method to Collection
1 parent 9481ea8 commit 89898df

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

collection.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ func makeIndex() *index {
2626

2727
// Collection contains items and indexes
2828
type Collection struct {
29-
items []reflect.Value
30-
idx map[string]*index
31-
schema schema
29+
items []reflect.Value
30+
idx map[string]*index
31+
schema
3232
}
3333

3434
func (c *Collection) lookidx(name string) *index {
3535
idx, ok := c.idx[name]
3636
if !ok {
3737
log.Panicf(
3838
"y/collection: The index \"%s\" not found in collection \"%s\".",
39-
name, c.schema.table)
39+
name, c.table)
4040
}
4141
return idx
4242
}
@@ -59,10 +59,30 @@ func (c *Collection) cells(cells []int) []reflect.Value {
5959
return items
6060
}
6161

62-
// Exists returns true if a value existed in the index
63-
func (c *Collection) Exists(name string, v int64) (ok bool) {
64-
_, ok = c.lookidx(name).cells[v]
65-
return
62+
// Get returns an item by primary key
63+
func (c *Collection) Get(pk ...interface{}) interface{} {
64+
fields := c.schema.xinfo.pk
65+
flen := len(fields)
66+
if flen == 0 {
67+
log.Panicf(
68+
"y/colleciton: no primary key in the schema definition \"%s\".", c.table)
69+
}
70+
if flen != len(pk) {
71+
log.Panicln("y/collection: missing primary key parameters.")
72+
}
73+
idx := c.lookidx(fields[0])
74+
CellLoop:
75+
for _, cell := range idx.cells[pk[0].(int64)] {
76+
item := valueOf(c.items[cell])
77+
// matching by a composite primary key
78+
for i := 1; i < flen; i++ {
79+
if c.schema.fval(item, fields[i]).Interface() != pk[i] {
80+
continue CellLoop
81+
}
82+
}
83+
return item.addr().Interface()
84+
}
85+
return nil
6686
}
6787

6888
// Empty returns false if no items exist

collection_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ var _ = Describe("Collection", func() {
1010
ID int64 `y:"id,pk"`
1111
}
1212

13+
type smthComposite struct {
14+
X int64 `y:",pk"`
15+
Y string `y:",pk"`
16+
}
17+
1318
Context("when one item added", func() {
1419
var (
1520
ptr *something
@@ -28,6 +33,34 @@ var _ = Describe("Collection", func() {
2833
It("should be contain the first item", func() {
2934
Expect(c.First()).To(Equal(ptr))
3035
})
36+
37+
It("should return the correct item by a primary key", func() {
38+
Expect(c.Get(int64(1))).To(Equal(ptr))
39+
})
40+
41+
It("should return nil value by unknown primary key", func() {
42+
Expect(c.Get(int64(2))).To(BeNil())
43+
})
44+
})
45+
46+
Context("when one composite item added", func() {
47+
var (
48+
ptr *smthComposite
49+
c *Collection
50+
)
51+
52+
BeforeEach(func() {
53+
ptr = &smthComposite{X: 1, Y: "y"}
54+
c = New(ptr).Collection()
55+
})
56+
57+
It("should return the correct item by a primary key", func() {
58+
Expect(c.Get(int64(1), "y")).To(Equal(ptr))
59+
})
60+
61+
It("should return nil value by unknown primary key", func() {
62+
Expect(c.Get(int64(1), "x")).To(BeNil())
63+
})
3164
})
3265

3366
Context("when two items added", func() {

0 commit comments

Comments
 (0)