Skip to content

Commit

Permalink
fix: Add constraint to abstract nodes being interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rlch committed Sep 15, 2023
1 parent d3efcf1 commit fb143d9
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ func bindCasted[C any](
var emptyInterface = reflect.TypeOf((*any)(nil)).Elem()

func (r *registry) bindValue(from any, to reflect.Value) error {
if to.Kind() == reflect.Ptr && to.Type().Elem() == emptyInterface {
toT := to.Type()
if to.Kind() == reflect.Ptr && toT.Elem() == emptyInterface {
to.Elem().Set(reflect.ValueOf(from))
return nil
} else if to.Type() == emptyInterface && to.CanSet() {
} else if toT == emptyInterface && to.CanSet() {
to.Set(reflect.ValueOf(from))
return nil
}
Expand All @@ -120,8 +121,15 @@ func (r *registry) bindValue(from any, to reflect.Value) error {
if ok {
return nil
}
if to.Type().Implements(rAbstract) ||
to.Elem().Type().Implements(rAbstract) {
innerT := toT
for innerT.Kind() == reflect.Ptr {
innerT = innerT.Elem()
}
if (toT.Implements(rAbstract) ||
toT.Elem().Implements(rAbstract)) &&
// We enforce that abstract nodes must be interfaces. Some hacking could
// relax this.
innerT.Kind() == reflect.Interface {
return r.bindAbstractNode(fromVal, to)
}
// TODO: Support abstract nodes
Expand Down

0 comments on commit fb143d9

Please sign in to comment.