本文整理匯總了Golang中github.com/fgrosse/goldi.Container類的典型用法代碼示例。如果您正苦於以下問題:Golang Container類的具體用法?Golang Container怎麽用?Golang Container使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Container類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handlerFunc
func handlerFunc(endpointTypeID string, container *goldi.Container) (http.HandlerFunc, error) {
h, err := container.Get(endpointTypeID)
if err != nil {
return nil, err
}
if h, ok := h.(func(http.ResponseWriter, *http.Request)); ok {
return h, nil
}
if h, ok := h.(http.Handler); ok {
return func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}, nil
}
return nil, fmt.Errorf("Type %q is neither a http.Handler not a http.HandlerFunc but a %T", endpointTypeID, h)
}
示例2:
Expect(t).To(MatchError("the struct MockType has only 2 fields but 3 arguments where provided"))
})
})
Describe("Arguments()", func() {
It("should return all factory arguments", func() {
args := []interface{}{"foo", true}
typeDef := goldi.NewStructType(MockType{}, args...)
Expect(typeDef.Arguments()).To(Equal(args))
})
})
Describe("Generate()", func() {
var (
config = map[string]interface{}{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
Context("without struct arguments", func() {
Context("when the factory is a struct (no pointer)", func() {
It("should generate the type", func() {
typeDef := goldi.NewStructType(MockType{})
Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
})
})
示例3:
var factory goldi.TypeFactory
factory = goldi.NewAliasType("foo")
// if this compiles the test passes (next expectation only to make compiler happy)
Expect(factory).NotTo(BeNil())
})
Describe("Arguments()", func() {
It("should return the aliased service ID", func() {
typeDef := goldi.NewAliasType("foo")
Expect(typeDef.Arguments()).To(Equal([]interface{}{"@foo"}))
})
})
Describe("Generate()", func() {
var (
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
config := map[string]interface{}{}
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
It("should act as alias for the actual type", func() {
container.Register("foo", goldi.NewStructType(Foo{}, "I was created by @foo"))
alias := goldi.NewAliasType("foo")
generated, err := alias.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generated).To(BeAssignableToTypeOf(&Foo{}))
示例4:
Describe("Arguments()", func() {
It("should return the arguments of the embedded type and the configurator as type ID", func() {
embeddedType = goldi.NewStructType(Foo{}, "%param_of_embedded%", "another param")
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
Expect(typeDef.Arguments()).NotTo(BeNil())
Expect(typeDef.Arguments()).To(HaveLen(3))
Expect(typeDef.Arguments()).To(ContainElement("%param_of_embedded%"))
Expect(typeDef.Arguments()).To(ContainElement("another param"))
Expect(typeDef.Arguments()).To(ContainElement("@configurator_type"))
})
})
Describe("Generate()", func() {
var (
config = map[string]interface{}{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
It("should get the embedded type and configurator and configure it", func() {
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
configurator := &MyConfigurator{ConfiguredValue: "success!"}
container.Register("configurator_type", goldi.NewInstanceType(configurator))
generatedType, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
示例5:
})
})
})
Describe("Arguments()", func() {
It("should return all factory arguments", func() {
args := []interface{}{"foo", true}
typeDef := goldi.NewType(NewMockTypeWithArgs, args...)
Expect(typeDef.Arguments()).To(Equal(args))
})
})
Describe("Generate()", func() {
var (
config = map[string]interface{}{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
Context("without factory function arguments", func() {
It("should generate the type", func() {
typeDef := goldi.NewType(NewMockType)
Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
})
})
示例6:
package goldi_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/fgrosse/goldi"
"reflect"
)
var _ = Describe("ParameterResolver", func() {
var (
config map[string]interface{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
config = map[string]interface{}{}
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
It("should return static parameters", func() {
parameter := reflect.ValueOf(42)
expectedType := parameter.Type()
Expect(resolver.Resolve(parameter, expectedType)).To(Equal(parameter))
})
Context("with dynamic parameters", func() {
示例7:
t := goldi.NewProxyType("logger_provider", "getLogger", "My logger")
Expect(goldi.IsValid(t)).To(BeFalse())
Expect(t).To(MatchError(`can not use unexported method "getLogger" as second argument to NewProxyType`))
})
})
Describe("Arguments()", func() {
It("should return the referenced service ID", func() {
typeDef := goldi.NewProxyType("logger_provider", "GetLogger", "My logger")
Expect(typeDef.Arguments()).To(Equal([]interface{}{"@logger_provider", "My logger"}))
})
})
Describe("Generate()", func() {
var (
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
config := map[string]interface{}{}
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
It("should get the correct method of the referenced type", func() {
container.Register("logger_provider", goldi.NewStructType(LoggerProvider{}))
typeDef := goldi.NewProxyType("logger_provider", "GetLogger", "My logger")
generated, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generated).To(BeAssignableToTypeOf(&SimpleLogger{}))
示例8:
if err != nil {
fmt.Println(err.Error())
return
}
// do stuff with the logger. usually you need a type assertion
fmt.Printf("%T", l.(*NullLogger))
// Output:
// *goldi_test.NullLogger
}
var _ = Describe("Container", func() {
var (
registry goldi.TypeRegistry
config map[string]interface{}
container *goldi.Container
)
BeforeEach(func() {
registry = goldi.NewTypeRegistry()
config = map[string]interface{}{}
container = goldi.NewContainer(registry, config)
})
It("should panic if a type can not be resolved", func() {
Expect(func() { container.MustGet("foo.bar") }).To(Panic())
})
It("should return an error if there was an issue generating the type", func() {
container.Register("foo", goldi.NewStructType(nil))
示例9:
package goldi_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"github.com/fgrosse/goldi"
)
var _ = Describe("TypeConfigurator", func() {
var container *goldi.Container
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
})
Describe("Configure", func() {
Context("when the type configurator has not been defined correctly", func() {
It("should return an error", func() {
someType := new(Foo)
configurator := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator type "@configurator" has not been defined`))
})
})
Context("when the type configurator is no struct or pointer to struct", func() {
It("should return an error", func() {
container.InjectInstance("configurator", 42)
someType := new(Foo)
configurator := goldi.NewTypeConfigurator("configurator", "Configure")