Skip to content

InvokeAs problems #75

Open
Open
@d-enk

Description

@d-enk
  1. Can't invoke interface (can't cast nil)
package main

import "github.com/samber/do/v2"

func main() {
	type I interface{}

	i := do.New()
	do.ProvideValue(i, I("some"))
	do.MustInvokeAs[I](i)
}

// panic: DI: could not find service `*main.I`, available services: `*main.I`
  1. Random invoked, other not checked ([👷] Road to v2 🚀 #45 (comment))
package main

import (
	"fmt"

	"github.com/samber/do/v2"
)

type I interface{ Name() string }

type i struct{}

func (i) Name() string { return "EXPECTED" }

type other struct{}

func (other) Other()       {}
func (other) Name() string { return "OTHER" }

// type
func main() {
	scope := do.New()

	do.ProvideValue(scope, other{})
	do.ProvideValue(scope, i{})

	for range 10 {
		fmt.Println(do.MustInvokeAs[I](scope).Name())
	}
	// output: something like
	// OTHER
	// OTHER
	// OTHER
	// EXPECTED
	// OTHER
	// OTHER
	// OTHER
	// EXPECTED
	// OTHER
	// OTHER
}

I create a function that would temporarily delete InvokedAs result by OverrideNamedValue and tried to check the next not exist.

But I came across both 1 problem and samber/go-type-to-string#2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions