func ExampleNewFuncReferenceType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	logger := new(SimpleLogger)
	container.Register("logger", goldi.NewInstanceType(logger))
	container.Register("log_func", goldi.NewFuncReferenceType("logger", "DoStuff"))

	f := container.MustGet("log_func").(func(string) string)
	fmt.Println(f("Hello World")) // executes logger.DoStuff
	// Output:
	// Hello World
}
Esempio n. 2
0
func ExampleNewConfiguredType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	// this example configurator accepts a Foo type and will set its Value field to the given value
	configurator := &MyConfigurator{ConfiguredValue: "success!"}

	// register the configurator under a type ID
	container.Register("configurator_type", goldi.NewInstanceType(configurator))

	// create the type that should be configured
	embeddedType := goldi.NewStructType(Foo{})
	container.Register("foo", goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure"))

	fmt.Println(container.MustGet("foo").(*Foo).Value)
	// Output:
	// success!
}
Esempio n. 3
0
	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())
			Expect(generatedType).NotTo(BeNil())
			Expect(generatedType).To(BeAssignableToTypeOf(&Foo{}))
			Expect(generatedType.(*Foo).Value).To(Equal("success!"))
		})

		It("should return an error if the embedded type can not be generated", func() {
			invalidType := goldi.NewStructType(nil)
			typeDef := goldi.NewConfiguredType(invalidType, "configurator_type", "Configure")
			configurator := &MyConfigurator{ConfiguredValue: "should not happen"}
			container.Register("configurator_type", goldi.NewInstanceType(configurator))

			generatedType, err := typeDef.Generate(resolver)
		})

		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")
				Expect(configurator.Configure(someType, container)).To(MatchError("the configurator instance is no struct or pointer to struct but a int"))
			})
		})

		Context("when the type configurator method does not exist", func() {
			It("should return an error", func() {
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Fooobar")
				container.Register("configurator", goldi.NewInstanceType(&MyConfigurator{}))

				Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator does not have a method "Fooobar"`))
			})
		})

		Context("when the type configurator has been defined properly", func() {
			var configurator *MyConfigurator
			BeforeEach(func() {
				configurator = &MyConfigurator{ConfiguredValue: "success!"}
				container.Register("configurator", goldi.NewInstanceType(configurator))
			})

			It("should return an error if the first argument is nil", func() {
				configuratorType := goldi.NewTypeConfigurator("configurator", "Configure")
				Expect(configuratorType.Configure(nil, container)).To(MatchError("can not configure nil"))