Skip to content

Commit

Permalink
fix: Support nil+registered / non-nil+base-type instantiation of Abst…
Browse files Browse the repository at this point in the history
…ract nodes
  • Loading branch information
rlch committed Aug 28, 2023
1 parent af31477 commit 43d742a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (r *registry) bindAbstractNode(node neo4j.Node, to reflect.Value) error {
}
} else if to.Type().Elem().Implements(rAbstract) {
ptrTo = true
if !to.IsNil() {
if !to.Elem().IsNil() {
abs = to.Elem().Interface().(IAbstract)
}
} else {
Expand All @@ -275,6 +275,7 @@ func (r *registry) bindAbstractNode(node neo4j.Node, to reflect.Value) error {
if len(labels) == 0 {
continue
}
fmt.Println(labels)
for _, label := range labels {
if _, ok := isNodeLabel[label]; !ok {
continue Bases
Expand Down
37 changes: 37 additions & 0 deletions valuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,43 @@ func TestBindValue(t *testing.T) {
}, *to)
})

t.Run("Abstract using base type", func(t *testing.T) {
var to tests.Organism = &tests.BaseOrganism{}
err := r.bindValue(neo4j.Node{
Labels: []string{"Dog", "Organism"},
Props: map[string]any{
"borfs": true,
},
}, reflect.ValueOf(&to))
assert.NoError(t, err)
assert.Equal(t, &tests.Dog{
Borfs: true,
}, to)
})

t.Run("Abstract using registered types", func(t *testing.T) {
rWithAbstract := &registry{
abstractNodes: []IAbstract{
&tests.BaseOrganism{},
},
}
var to tests.Organism
err := rWithAbstract.bindValue(neo4j.Node{
Labels: []string{"Human", "Organism"},
Props: map[string]any{
"alive": true,
"name": "Raqeeb",
},
}, reflect.ValueOf(&to))
assert.NoError(t, err)
assert.Equal(t, &tests.Human{
BaseOrganism: tests.BaseOrganism{
Alive: true,
},
Name: "Raqeeb",
}, to)
})

t.Run("Any", func(t *testing.T) {
to := new(any)
err := r.bindValue(neo4j.Node{
Expand Down

0 comments on commit 43d742a

Please sign in to comment.