Skip to content

Commit 254b57d

Browse files
committed
coal: clarified item base embedding rules
1 parent 0d27ced commit 254b57d

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎coal/meta.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,24 @@ func GetItemMeta(typ reflect.Type) *ItemMeta {
430430
}
431431

432432
// check if embedding item
433-
if typ.NumField() == 0 || typ.Field(0).Type != itemBaseType || !typ.Field(0).Anonymous {
433+
if typ.NumField() == 0 || typ.Field(0).Type != itemBaseType {
434434
return nil
435435
}
436436

437-
// TODO: Validate json and bson tags.
437+
// check if field is anonymous
438+
if !typ.Field(0).Anonymous {
439+
panic(`coal: expected an embedded "coal.ItemBase" as the first struct field`)
440+
}
441+
442+
// check JSON tag
443+
if typ.Field(0).Tag.Get("json") != "" && typ.Field(0).Tag.Get("json") != "-" {
444+
panic(`coal: expected to find no tag or a tag of the form 'json:"-"' on "coal.ItemBase"`)
445+
}
446+
447+
// check BSON tag
448+
if typ.Field(0).Tag.Get("bson") != ",inline" && typ.Field(0).Tag.Get("bson") != "-" {
449+
panic(`coal: expected to find a tag of the form 'bson:",inline" or 'bson:"-" on "coal.ItemBase"`)
450+
}
438451

439452
// prepare meta
440453
meta = &ItemMeta{

‎coal/model_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ func TestList(t *testing.T) {
6060
list[1].ItemID = "b"
6161

6262
var out []stick.Map
63+
err = stick.BSON.Transfer(list, &out)
64+
assert.NoError(t, err)
65+
assert.Equal(t, []stick.Map{
66+
{
67+
"_id": "a",
68+
"title": "Foo",
69+
"done": false,
70+
}, {
71+
"_id": "b",
72+
"title": "Bar",
73+
"done": true,
74+
},
75+
}, out)
76+
77+
out = nil
6378
err = stick.JSON.Transfer(list, &out)
6479
assert.NoError(t, err)
6580
assert.Equal(t, []stick.Map{
@@ -74,6 +89,8 @@ func TestList(t *testing.T) {
7489
},
7590
}, out)
7691

92+
/* move items */
93+
7794
out[0], out[1] = out[1], out[0]
7895
out = append(out, stick.Map{
7996
"id": "c",

‎coal/utils_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func (m *noteModel) Validate() error {
5959
}
6060

6161
type listItem struct {
62-
ItemBase
63-
Title string `json:"title"`
64-
Done bool `json:"done"`
62+
ItemBase `bson:",inline"`
63+
Title string `json:"title"`
64+
Done bool `json:"done"`
6565
}
6666

6767
func (i *listItem) Validate() error {

0 commit comments

Comments
 (0)