Skip to content

Commit

Permalink
[Refactor] use composition for InnerAssociations() function
Browse files Browse the repository at this point in the history
  • Loading branch information
larrymjordan committed Feb 2, 2018
1 parent 54b736d commit 6760b9f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 65 deletions.
10 changes: 10 additions & 0 deletions associations/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ type Association interface {
InnerAssociations() InnerAssociations
}

// associationComposite adds the ability for a Association to
// have nested associations.
type associationComposite struct {
innerAssociations InnerAssociations
}

func (a *associationComposite) InnerAssociations() InnerAssociations {
return a.innerAssociations
}

// InnerAssociation is a struct that represents a deep level
// association. per example Song.Composer, Composer is an inner
// association for Song.
Expand Down
24 changes: 10 additions & 14 deletions associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
// belongsToAssociation is the implementation for the belongs_to
// association type in a model.
type belongsToAssociation struct {
ownerModel reflect.Value
ownerType reflect.Type
ownerID reflect.Value
owner interface{}
innerAssociations InnerAssociations
ownerModel reflect.Value
ownerType reflect.Type
ownerID reflect.Value
owner interface{}
*associationComposite
}

func init() {
Expand All @@ -30,18 +30,14 @@ func belongsToAssociationBuilder(p associationParams) (Association, error) {
}

return &belongsToAssociation{
ownerModel: fval,
ownerType: fval.Type(),
ownerID: p.modelValue.FieldByName(ownerIDField),
owner: p.model,
innerAssociations: p.innerAssociations,
ownerModel: fval,
ownerType: fval.Type(),
ownerID: p.modelValue.FieldByName(ownerIDField),
owner: p.model,
associationComposite: &associationComposite{innerAssociations: p.innerAssociations},
}, nil
}

func (b *belongsToAssociation) InnerAssociations() InnerAssociations {
return b.innerAssociations
}

func (b *belongsToAssociation) Kind() reflect.Kind {
if b.ownerType.Kind() == reflect.Ptr {
return b.ownerType.Elem().Kind()
Expand Down
36 changes: 16 additions & 20 deletions associations/has_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
// hasManyAssociation is the implementation for the has_many
// association type in a model.
type hasManyAssociation struct {
tableName string
field reflect.StructField
value reflect.Value
ownerName string
ownerID interface{}
fkID string
orderBy string
innerAssociations InnerAssociations
tableName string
field reflect.StructField
value reflect.Value
ownerName string
ownerID interface{}
fkID string
orderBy string
*associationComposite
}

func init() {
Expand All @@ -26,21 +26,17 @@ func init() {

func hasManyAssociationBuilder(p associationParams) (Association, error) {
return &hasManyAssociation{
tableName: p.popTags.Find("has_many").Value,
field: p.field,
value: p.modelValue.FieldByName(p.field.Name),
ownerName: p.modelType.Name(),
ownerID: p.modelValue.FieldByName("ID").Interface(),
fkID: p.popTags.Find("fk_id").Value,
orderBy: p.popTags.Find("order_by").Value,
innerAssociations: p.innerAssociations,
tableName: p.popTags.Find("has_many").Value,
field: p.field,
value: p.modelValue.FieldByName(p.field.Name),
ownerName: p.modelType.Name(),
ownerID: p.modelValue.FieldByName("ID").Interface(),
fkID: p.popTags.Find("fk_id").Value,
orderBy: p.popTags.Find("order_by").Value,
associationComposite: &associationComposite{innerAssociations: p.innerAssociations},
}, nil
}

func (b *hasManyAssociation) InnerAssociations() InnerAssociations {
return b.innerAssociations
}

func (a *hasManyAssociation) Kind() reflect.Kind {
if a.field.Type.Kind() == reflect.Ptr {
return a.field.Type.Elem().Kind()
Expand Down
32 changes: 14 additions & 18 deletions associations/has_one_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

type hasOneAssociation struct {
ownedModel reflect.Value
ownedType reflect.Type
ownerID interface{}
ownerName string
owner interface{}
fkID string
innerAssociations InnerAssociations
ownedModel reflect.Value
ownedType reflect.Type
ownerID interface{}
ownerName string
owner interface{}
fkID string
*associationComposite
}

func init() {
Expand All @@ -24,20 +24,16 @@ func init() {
func hasOneAssociationBuilder(p associationParams) (Association, error) {
fval := p.modelValue.FieldByName(p.field.Name)
return &hasOneAssociation{
owner: p.model,
ownedModel: fval,
ownedType: fval.Type(),
ownerID: p.modelValue.FieldByName("ID").Interface(),
ownerName: p.modelType.Name(),
fkID: p.popTags.Find("fk_id").Value,
innerAssociations: p.innerAssociations,
owner: p.model,
ownedModel: fval,
ownedType: fval.Type(),
ownerID: p.modelValue.FieldByName("ID").Interface(),
ownerName: p.modelType.Name(),
fkID: p.popTags.Find("fk_id").Value,
associationComposite: &associationComposite{innerAssociations: p.innerAssociations},
}, nil
}

func (h *hasOneAssociation) InnerAssociations() InnerAssociations {
return h.innerAssociations
}

func (h *hasOneAssociation) Kind() reflect.Kind {
return h.ownedType.Kind()
}
Expand Down
22 changes: 9 additions & 13 deletions associations/many_to_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@ type manyToManyAssociation struct {
owner interface{}
fkID string
orderBy string
innerAssociations InnerAssociations
*associationComposite
}

func init() {
associationBuilders["many_to_many"] = func(p associationParams) (Association, error) {
return &manyToManyAssociation{
fieldType: p.modelValue.FieldByName(p.field.Name).Type(),
fieldValue: p.modelValue.FieldByName(p.field.Name),
owner: p.model,
model: p.modelValue,
manyToManyTableName: p.popTags.Find("many_to_many").Value,
fkID: p.popTags.Find("fk_id").Value,
orderBy: p.popTags.Find("order_by").Value,
innerAssociations: p.innerAssociations,
fieldType: p.modelValue.FieldByName(p.field.Name).Type(),
fieldValue: p.modelValue.FieldByName(p.field.Name),
owner: p.model,
model: p.modelValue,
manyToManyTableName: p.popTags.Find("many_to_many").Value,
fkID: p.popTags.Find("fk_id").Value,
orderBy: p.popTags.Find("order_by").Value,
associationComposite: &associationComposite{innerAssociations: p.innerAssociations},
}, nil
}
}

func (m *manyToManyAssociation) InnerAssociations() InnerAssociations {
return m.innerAssociations
}

func (m *manyToManyAssociation) Kind() reflect.Kind {
return m.fieldType.Kind()
}
Expand Down

0 comments on commit 6760b9f

Please sign in to comment.