Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scanning embedded assoc #289

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions association.go
Expand Up @@ -43,6 +43,7 @@ var associationCache sync.Map
type Association struct {
data associationData
rv reflect.Value
doc *Document
}

// Type of association.
Expand All @@ -63,7 +64,11 @@ func (a Association) LazyDocument() (*Document, bool) {
return a.document(true)
}

func (a Association) document(lazy bool) (*Document, bool) {
func (a *Association) document(lazy bool) (*Document, bool) {
if a.doc != nil {
return a.doc, a.doc.Persisted()
}

var (
rv = reflectValueFieldByIndex(a.rv, a.data.targetIndex, !lazy)
)
Expand All @@ -78,17 +83,11 @@ func (a Association) document(lazy bool) (*Document, bool) {
return NewDocument(rv), false
}

var (
doc = NewDocument(rv)
)

return doc, doc.Persisted()
a.doc = NewDocument(rv)
return a.doc, a.doc.Persisted()
default:
var (
doc = NewDocument(rv.Addr())
)

return doc, doc.Persisted()
a.doc = NewDocument(rv.Addr())
return a.doc, a.doc.Persisted()
}
}

Expand Down
65 changes: 47 additions & 18 deletions document.go
Expand Up @@ -73,10 +73,11 @@ type documentData struct {

// Document provides an abstraction over reflect to easily works with struct for database purpose.
type Document struct {
v interface{}
rv reflect.Value
rt reflect.Type
data documentData
v interface{}
rv reflect.Value
rt reflect.Type
data documentData
cachedAssocs map[string]Association
}

// ReflectValue of referenced document.
Expand Down Expand Up @@ -142,6 +143,11 @@ func (d Document) PrimaryValue() interface{} {

// Persisted returns true if document primary key is not zero.
func (d Document) Persisted() bool {
// doc hasn't been initialized yet
if d.rv.Kind() == reflect.Ptr && d.rv.IsNil() {
return false
}

var (
pValues = d.PrimaryValues()
)
Expand Down Expand Up @@ -253,23 +259,34 @@ func (d Document) Scanners(fields []string) []interface{} {
)

for index, field := range fields {
if structIndex, ok := d.data.index[field]; ok {
var (
fv = reflectValueFieldByIndex(d.rv, structIndex, true)
ft = fv.Type()
)
result[index] = d.scanner(field)
}

if ft.Kind() == reflect.Ptr {
result[index] = fv.Addr().Interface()
} else {
result[index] = Nullable(fv.Addr().Interface())
}
return result
}

func (d Document) scanner(field string) interface{} {
if structIndex, ok := d.data.index[field]; ok {
var (
fv = reflectValueFieldByIndex(d.rv, structIndex, true)
ft = fv.Type()
)

if ft.Kind() == reflect.Ptr {
return fv.Addr().Interface()
} else {
result[index] = &sql.RawBytes{}
return Nullable(fv.Addr().Interface())
}
} else if split := strings.SplitN(field, ".", 2); len(split) == 2 {
if assoc, ok := d.association(split[0]); ok && assoc.Type() == BelongsTo || assoc.Type() == HasOne {
var (
doc, _ = assoc.Document()
)
return doc.scanner(split[1])
}
}

return result
return &sql.RawBytes{}
}

// BelongsTo fields of this document.
Expand All @@ -294,12 +311,24 @@ func (d Document) Preload() []string {

// Association of this document with given name.
func (d Document) Association(name string) Association {
if assoc, ok := d.association(name); ok {
return assoc
}

panic("rel: no field named (" + name + ") in type " + d.rt.String() + " found ")
}

func (d Document) association(name string) (Association, bool) {
if assoc, ok := d.cachedAssocs[name]; ok {
return assoc, ok
}

index, ok := d.data.index[name]
if !ok {
panic("rel: no field named (" + name + ") in type " + d.rt.String() + " found ")
return Association{}, false
}

return newAssociation(d.rv, index)
return newAssociation(d.rv, index), true
}

// Reset this document, this is a noop for compatibility with collection.
Expand Down
48 changes: 48 additions & 0 deletions document_test.go
Expand Up @@ -474,6 +474,54 @@ func TestDocument_Scanners(t *testing.T) {
assert.Equal(t, scanners, doc.Scanners(fields))
}

func TestDocument_Scanners_withAssoc(t *testing.T) {
var (
record = Transaction{
ID: 1,
BuyerID: 2,
Status: "SENT",
Buyer: User{
ID: 2,
Name: "user",
WorkAddress: &Address{
Street: "Takeshita-dori",
},
},
}
doc = NewDocument(&record)
fields = []string{"id", "user_id", "buyer.id", "buyer.name", "buyer.work_address.street", "status"}
scanners = []interface{}{
Nullable(&record.ID),
Nullable(&record.BuyerID),
Nullable(&record.Buyer.ID),
Nullable(&record.Buyer.Name),
Nullable(&record.Buyer.WorkAddress.Street),
Nullable(&record.Status),
}
)

assert.Equal(t, scanners, doc.Scanners(fields))
}

func TestDocument_Scanners_withUnitializedAssoc(t *testing.T) {
var (
record = Transaction{}
doc = NewDocument(&record)
fields = []string{"id", "user_id", "buyer.id", "buyer.name", "status", "buyer.work_address.street"}
result = doc.Scanners(fields)
expected = []interface{}{
Nullable(&record.ID),
Nullable(&record.BuyerID),
Nullable(&record.Buyer.ID),
Nullable(&record.Buyer.Name),
Nullable(&record.Status),
Nullable(&record.Buyer.WorkAddress.Street),
}
)

assert.Equal(t, expected, result)
}

func TestDocument_ScannersInitPointers(t *testing.T) {
type Embedded1 struct {
ID int
Expand Down