Skip to content

Commit

Permalink
Generic type Repository wrapper (#318)
Browse files Browse the repository at this point in the history
* Generic type Repository wrapper

* tidy

* set go version

* fix and add more tests

* add more test

* rename record with entity

* Implement entity iterator
  • Loading branch information
Fs02 committed Oct 17, 2022
1 parent eaad046 commit bfe77a0
Show file tree
Hide file tree
Showing 25 changed files with 1,250 additions and 326 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -32,6 +32,8 @@ jobs:
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
Expand Down
32 changes: 16 additions & 16 deletions association_test.go
Expand Up @@ -18,7 +18,7 @@ func TestAssociation_Document(t *testing.T) {
)

tests := []struct {
record string
entity string
field string
data any
typ AssociationType
Expand All @@ -33,7 +33,7 @@ func TestAssociation_Document(t *testing.T) {
autoload bool
}{
{
record: "Transaction",
entity: "Transaction",
field: "Buyer",
data: transaction,
typ: BelongsTo,
Expand All @@ -47,7 +47,7 @@ func TestAssociation_Document(t *testing.T) {
autoload: true,
},
{
record: "Transaction",
entity: "Transaction",
field: "Buyer",
data: transactionLoaded,
typ: BelongsTo,
Expand All @@ -61,7 +61,7 @@ func TestAssociation_Document(t *testing.T) {
autoload: true,
},
{
record: "User",
entity: "User",
field: "Address",
data: user,
typ: HasOne,
Expand All @@ -75,7 +75,7 @@ func TestAssociation_Document(t *testing.T) {
autosave: true,
},
{
record: "User",
entity: "User",
field: "Address",
data: userLoaded,
typ: HasOne,
Expand All @@ -89,7 +89,7 @@ func TestAssociation_Document(t *testing.T) {
autosave: true,
},
{
record: "Address",
entity: "Address",
field: "User",
data: address,
typ: BelongsTo,
Expand All @@ -102,7 +102,7 @@ func TestAssociation_Document(t *testing.T) {
foreignValue: 0,
},
{
record: "Address",
entity: "Address",
field: "User",
data: addressLoaded,
typ: BelongsTo,
Expand All @@ -117,7 +117,7 @@ func TestAssociation_Document(t *testing.T) {
}

for _, test := range tests {
t.Run(test.record+"."+test.field, func(t *testing.T) {
t.Run(test.entity+"."+test.field, func(t *testing.T) {
var (
rv = reflect.ValueOf(test.data)
sf, _ = rv.Type().Elem().FieldByName(test.field)
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestAssociation_Collection(t *testing.T) {
)

tests := []struct {
record string
entity string
field string
data any
typ AssociationType
Expand All @@ -215,7 +215,7 @@ func TestAssociation_Collection(t *testing.T) {
autosave bool
}{
{
record: "User",
entity: "User",
field: "Transactions",
data: user,
typ: HasMany,
Expand All @@ -228,7 +228,7 @@ func TestAssociation_Collection(t *testing.T) {
foreignValue: nil,
},
{
record: "User",
entity: "User",
field: "Transactions",
data: userLoaded,
typ: HasMany,
Expand All @@ -241,7 +241,7 @@ func TestAssociation_Collection(t *testing.T) {
foreignValue: nil,
},
{
record: "User",
entity: "User",
field: "Roles",
data: user,
typ: HasMany,
Expand All @@ -255,7 +255,7 @@ func TestAssociation_Collection(t *testing.T) {
through: "user_roles",
},
{
record: "Role",
entity: "Role",
field: "Users",
data: role,
typ: HasMany,
Expand All @@ -269,7 +269,7 @@ func TestAssociation_Collection(t *testing.T) {
through: "user_roles",
},
{
record: "User",
entity: "User",
field: "Followers",
data: user,
typ: HasMany,
Expand All @@ -283,7 +283,7 @@ func TestAssociation_Collection(t *testing.T) {
through: "followeds",
},
{
record: "User",
entity: "User",
field: "Followings",
data: user,
typ: HasMany,
Expand All @@ -299,7 +299,7 @@ func TestAssociation_Collection(t *testing.T) {
}

for _, test := range tests {
t.Run(test.record+"."+test.field, func(t *testing.T) {
t.Run(test.entity+"."+test.field, func(t *testing.T) {
var (
rv = reflect.ValueOf(test.data)
sf, _ = rv.Type().Elem().FieldByName(test.field)
Expand Down
6 changes: 3 additions & 3 deletions changeset.go
Expand Up @@ -157,9 +157,9 @@ func (c Changeset) applyAssocMany(field string, mut *Mutation) {
}
}

// NewChangeset returns new changeset mutator for given record.
func NewChangeset(record any) Changeset {
return newChangeset(NewDocument(record))
// NewChangeset returns new changeset mutator for given entity.
func NewChangeset(entity any) Changeset {
return newChangeset(NewDocument(entity))
}

func newChangeset(doc *Document) Changeset {
Expand Down
4 changes: 2 additions & 2 deletions collection.go
Expand Up @@ -178,8 +178,8 @@ func (c Collection) Swap(i, j int) {

// NewCollection used to create abstraction to work with slice.
// COllection can be created using interface or reflect.Value.
func NewCollection(records any, readonly ...bool) *Collection {
switch v := records.(type) {
func NewCollection(entities any, readonly ...bool) *Collection {
switch v := entities.(type) {
case *Collection:
return v
case reflect.Value:
Expand Down
70 changes: 35 additions & 35 deletions collection_test.go
Expand Up @@ -32,17 +32,17 @@ func (it *Items) PrimaryValues() []any {

func TestCollection_ReflectValue(t *testing.T) {
var (
record = []User{}
doc = NewCollection(&record)
entity = []User{}
doc = NewCollection(&entity)
)

assert.Equal(t, doc.rv, doc.ReflectValue())
}

func TestCollection_Table(t *testing.T) {
var (
records = []User{}
col = NewCollection(&records)
entities = []User{}
col = NewCollection(&entities)
)

// infer table name
Expand All @@ -51,8 +51,8 @@ func TestCollection_Table(t *testing.T) {

func TestCollection_Table_usingInterface(t *testing.T) {
var (
records = Items{}
col = NewCollection(&records)
entities = Items{}
col = NewCollection(&entities)
)

// infer table name
Expand All @@ -61,8 +61,8 @@ func TestCollection_Table_usingInterface(t *testing.T) {

func TestCollection_Table_usingElemInterface(t *testing.T) {
var (
records = []Item{}
col = NewCollection(&records)
entities = []Item{}
col = NewCollection(&entities)
)

// infer table name
Expand All @@ -71,12 +71,12 @@ func TestCollection_Table_usingElemInterface(t *testing.T) {

func TestCollection_Primary(t *testing.T) {
var (
records = []User{
entities = []User{
{ID: 1},
{ID: 2},
}
rt = reflect.TypeOf(records).Elem()
col = NewCollection(&records)
rt = reflect.TypeOf(entities).Elem()
col = NewCollection(&entities)
)

// infer primary key
Expand All @@ -87,7 +87,7 @@ func TestCollection_Primary(t *testing.T) {
_, cached := primariesCache.Load(rt)
assert.True(t, cached)

records[1].ID = 4
entities[1].ID = 4

// infer primary key using cache
assert.Equal(t, "id", col.PrimaryField())
Expand All @@ -98,11 +98,11 @@ func TestCollection_Primary(t *testing.T) {

func TestCollection_Primary_usingInterface(t *testing.T) {
var (
records = Items{
entities = Items{
{UUID: "abc123"},
{UUID: "def456"},
}
col = NewCollection(&records)
col = NewCollection(&entities)
)

// infer primary key
Expand All @@ -112,12 +112,12 @@ func TestCollection_Primary_usingInterface(t *testing.T) {

func TestCollection_Primary_usingElemInterface(t *testing.T) {
var (
records = []Item{
entities = []Item{
{UUID: "abc123"},
{UUID: "def456"},
}
rt = reflect.TypeOf(records).Elem()
col = NewCollection(&records)
rt = reflect.TypeOf(entities).Elem()
col = NewCollection(&entities)
)

// infer primary key
Expand All @@ -129,13 +129,13 @@ func TestCollection_Primary_usingElemInterface(t *testing.T) {

func TestCollection_Primary_usingElemInterface_ptrElem(t *testing.T) {
var (
records = []*Item{
entities = []*Item{
{UUID: "abc123"},
{UUID: "def456"},
nil,
}
rt = reflect.TypeOf(records).Elem()
col = NewCollection(&records)
rt = reflect.TypeOf(entities).Elem()
col = NewCollection(&entities)
)

// infer primary key
Expand All @@ -147,15 +147,15 @@ func TestCollection_Primary_usingElemInterface_ptrElem(t *testing.T) {

func TestCollection_Primary_usingTag(t *testing.T) {
var (
records = []struct {
entities = []struct {
ID uint
ExternalID int `db:",primary"`
Name string
}{
{ExternalID: 1},
{ExternalID: 2},
}
col = NewCollection(&records)
col = NewCollection(&entities)
)

// infer primary key
Expand Down Expand Up @@ -190,11 +190,11 @@ func TestCollection_Primary_composite(t *testing.T) {

func TestCollection_Primary_notFound(t *testing.T) {
var (
records = []struct {
entities = []struct {
ExternalID int
Name string
}{}
col = NewCollection(&records)
col = NewCollection(&entities)
)

assert.Panics(t, func() {
Expand Down Expand Up @@ -258,45 +258,45 @@ func TestCollection_Slice(t *testing.T) {

func TestCollection(t *testing.T) {
tests := []struct {
record any
entity any
panics bool
}{
{
record: &[]User{},
entity: &[]User{},
},
{
record: NewCollection(&[]User{}),
entity: NewCollection(&[]User{}),
},
{
record: reflect.ValueOf(&[]User{}),
entity: reflect.ValueOf(&[]User{}),
},
{
record: reflect.ValueOf([]User{}),
entity: reflect.ValueOf([]User{}),
panics: true,
},
{
record: reflect.ValueOf(&User{}),
entity: reflect.ValueOf(&User{}),
panics: true,
},
{
record: reflect.TypeOf(&[]User{}),
entity: reflect.TypeOf(&[]User{}),
panics: true,
},
{
record: nil,
entity: nil,
panics: true,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%T", test.record), func(t *testing.T) {
t.Run(fmt.Sprintf("%T", test.entity), func(t *testing.T) {
if test.panics {
assert.Panics(t, func() {
NewCollection(test.record)
NewCollection(test.entity)
})
} else {
assert.NotPanics(t, func() {
NewCollection(test.record)
NewCollection(test.entity)
})
}
})
Expand Down
4 changes: 2 additions & 2 deletions document.go
Expand Up @@ -294,8 +294,8 @@ func (d Document) Flag(flag DocumentFlag) bool {

// NewDocument used to create abstraction to work with struct.
// Document can be created using interface or reflect.Value.
func NewDocument(record any, readonly ...bool) *Document {
switch v := record.(type) {
func NewDocument(entity any, readonly ...bool) *Document {
switch v := entity.(type) {
case *Document:
return v
case reflect.Value:
Expand Down

0 comments on commit bfe77a0

Please sign in to comment.