Beispiel #1
0
// RegisterTypes registers all types that have been defined in the file "types.yml"
//
// DO NOT EDIT THIS FILE: it has been generated by goldigen v0.9.9.
// It is however good practice to put this file under version control.
// See https://github.com/fgrosse/goldi for what is going on here.
func RegisterTypes(types goldi.TypeRegistry) {
	types.RegisterAll(map[string]goldi.TypeFactory{
		"kernel.http_handler":  goldi.NewAliasType("servo.routing.router::ServeHTTP"),
		"servo.routing.loader": goldi.NewStructType(new(Loader)),
		"servo.routing.router": goldi.NewType(NewRouter, "@servo.routing.loader", "@container"),
	})
}
Beispiel #2
0
// RegisterTypes registers all types that have been defined in the file "config/types.yml"
//
// DO NOT EDIT THIS FILE: it has been generated by goldigen v0.9.9.
// It is however good practice to put this file under version control.
// See https://github.com/fgrosse/goldi for what is going on here.
func RegisterTypes(types goldi.TypeRegistry) {
	types.RegisterAll(map[string]goldi.TypeFactory{
		"composed_endpoint":             goldi.NewType(handler.Composed, "@my_controller::OuterHandlerAction", "@my_controller::SecondHandlerAction", "@my_controller::FancyAction"),
		"container_aware_endpoint":      goldi.NewFuncReferenceType("my_container_aware_controller", "SomeAction"),
		"controller_endpoint":           goldi.NewFuncReferenceType("my_controller", "FancyAction"),
		"custom_error_handler":          goldi.NewStructType(new(lib.MyErrorHandler)),
		"error_endpoint":                goldi.NewFuncReferenceType("my_controller", "ErrorAction"),
		"greet_user_endpoint":           goldi.NewFuncType(endpoints.GreetUserEndpoint),
		"homepage_endpoint":             goldi.NewFuncType(endpoints.HomepageEndpoint),
		"kernel.http_handler":           goldi.NewType(handler.MiddleWareAdapter, "@servo.routing.router", "@my_app.error_handler", "@my_app.logging_adapter"),
		"my_app.error_handler":          goldi.NewType(middleware.ErrorHandlingAdapter, "@custom_error_handler::HandleEndpointError"),
		"my_app.logging_adapter":        goldi.NewType(middleware.LoggingAdapter, "@my_logger"),
		"my_container_aware_controller": goldi.NewStructType(new(endpoints.ContainerAwareController), "@container"),
		"my_controller":                 goldi.NewStructType(new(endpoints.FancyController), "@my_service_client"),
		"my_logger":                     goldi.NewProxyType("logger_provider", "GetLogger", "my_app"),
		"my_service_client":             goldi.NewType(lib.NewMockServiceClient, "%my_app.some_parameter%"),
	})
}
Beispiel #3
0
// RegisterTypes registers all types that have been defined in the file "types.yml"
//
// DO NOT EDIT THIS FILE: it has been generated by goldigen v0.9.9.
// It is however good practice to put this file under version control.
// See https://github.com/fgrosse/goldi for what is going on here.
func RegisterTypes(types goldi.TypeRegistry) {
	types.RegisterAll(map[string]goldi.TypeFactory{
		"logger_provider":    goldi.NewAliasType("logxi.logger_provider"),
		"logxi.configurator": goldi.NewType(NewLoggerConfigurator, "%logxi.level%"),
		"logxi.logger_provider": goldi.NewConfiguredType(
			goldi.NewStructType(new(LoggerProvider)),
			"logxi.configurator", "Configure",
		),
	})
}
Beispiel #4
0
func ExampleNewType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	// register the type using the factory function NewMockTypeWithArgs and pass two arguments
	container.Register("my_type", goldi.NewType(NewMockTypeWithArgs, "Hello World", true))

	t := container.MustGet("my_type").(*MockType)
	fmt.Printf("%#v", t)
	// Output:
	// &goldi_test.MockType{StringParameter:"Hello World", BoolParameter:true}
}
Beispiel #5
0
func ExampleContainer() {
	registry := goldi.NewTypeRegistry()
	config := map[string]interface{}{}
	container := goldi.NewContainer(registry, config)

	container.Register("logger", goldi.NewType(NewNullLogger))

	l := container.MustGet("logger")
	fmt.Printf("%T", l)
	// Output:
	// *goldi_test.NullLogger
}
Beispiel #6
0
func ExampleContainer_Get() {
	registry := goldi.NewTypeRegistry()
	config := map[string]interface{}{}
	container := goldi.NewContainer(registry, config)

	container.Register("logger", goldi.NewType(NewNullLogger))

	l, err := container.Get("logger")
	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
}
Beispiel #7
0
	// register the type using the factory function NewMockTypeWithArgs and pass two arguments
	container.Register("my_type", goldi.NewType(NewMockTypeWithArgs, "Hello World", true))

	t := container.MustGet("my_type").(*MockType)
	fmt.Printf("%#v", t)
	// Output:
	// &goldi_test.MockType{StringParameter:"Hello World", BoolParameter:true}
}

// ExampleNewType_ prevents godoc from printing the whole content of this file as example
func ExampleNewType_() {}

var _ = Describe("type", func() {
	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewType(NewFoo)
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("goldi.NewType()", func() {
		Context("with invalid factory function", func() {
			It("should return an invalid type if the generator is no function", func() {
				Expect(goldi.IsValid(goldi.NewType(42))).To(BeFalse())
			})

			It("should return an invalid type if the generator has no output parameters", func() {
				Expect(goldi.IsValid(goldi.NewType(func() {}))).To(BeFalse())
			})

			It("should return an invalid type if the generator has more than one output parameter", func() {
				})
			})
		})

		Context("when the type has not been registered", func() {
			It("should return an error", func() {
				parameter := reflect.ValueOf("@foo")
				expectedType := reflect.TypeOf(&Foo{})

				result, err := resolver.Resolve(parameter, expectedType)
				Expect(err).To(HaveOccurred())
				Expect(result.IsValid()).To(BeFalse())
				Expect(err).To(MatchError(`the referenced type "@foo" has not been defined`))
				Expect(err.(goldi.UnknownTypeReferenceError).TypeID).To(Equal("foo"))
			})
		})

		Context("when the type has is invalid", func() {
			It("should return an error", func() {
				parameter := reflect.ValueOf("@foo")
				expectedType := reflect.TypeOf(&Foo{})

				container.Register("foo", goldi.NewType(nil)) // foo will be invalid
				result, err := resolver.Resolve(parameter, expectedType)
				Expect(result).To(Equal(reflect.Zero(expectedType)))
				Expect(err).To(MatchError(`goldi: error while generating type "foo": the given factoryFunction is nil`))
			})
		})
	})
})
	BeforeEach(func() {
		registry = goldi.NewTypeRegistry()
		config = map[string]interface{}{}
		container = goldi.NewContainer(registry, config)
		validator = validation.NewContainerValidator()
	})

	It("should return an error if an invalid type was registered", func() {
		registry.Register("main_type", goldi.NewFuncReferenceType("not_existent", "type"))

		Expect(validator.Validate(container)).NotTo(Succeed())
	})

	It("should return an error when parameter has not been set", func() {
		typeDef := goldi.NewType(NewMockTypeWithArgs, "hello world", "%param%")
		registry.Register("main_type", typeDef)

		Expect(validator.Validate(container)).NotTo(Succeed())
	})

	It("should return an error when a dependend type has not been registered", func() {
		typeDef := goldi.NewType(NewTypeForServiceInjection, "@injected_type")
		registry.Register("main_type", typeDef)

		Expect(validator.Validate(container)).NotTo(Succeed())
	})

	It("should return an error when a direct circular type dependency exists", func() {
		injectedTypeID := "type_1"
		typeDef1 := goldi.NewType(NewTypeForServiceInjection, "@type_2")
Beispiel #10
0
		registry.RegisterType(typeID, generator.NewMockType)

		generatorWrapper, typeIsRegistered := registry[typeID]
		Expect(typeIsRegistered).To(BeTrue())
		Expect(generatorWrapper).NotTo(BeNil())

		firstResult := container.MustGet(typeID)
		secondResult := container.MustGet(typeID)
		thirdResult := container.MustGet(typeID)
		Expect(firstResult == secondResult).To(BeTrue())
		Expect(firstResult == thirdResult).To(BeTrue())
	})

	It("should pass static parameters as arguments when generating types", func() {
		typeID := "test_type"
		typeDef := goldi.NewType(NewMockTypeWithArgs, "parameter1", true)
		registry.Register(typeID, typeDef)

		generatedType := container.MustGet("test_type")
		Expect(generatedType).NotTo(BeNil())
		Expect(generatedType).To(BeAssignableToTypeOf(&MockType{}))

		generatedMock := generatedType.(*MockType)
		Expect(generatedMock.StringParameter).To(Equal("parameter1"))
		Expect(generatedMock.BoolParameter).To(Equal(true))
	})

	It("should be able to use parameters as arguments when generating types", func() {
		typeID := "test_type"
		typeDef := goldi.NewType(NewMockTypeWithArgs, "%parameter1%", "%parameter2%")
		registry.Register(typeID, typeDef)
Beispiel #11
0
			factory, typeIsRegistered := registry[typeID]
			Expect(typeIsRegistered).To(BeTrue())
			Expect(factory).NotTo(BeNil())

			generateResult, err := factory.Generate(resolver)
			Expect(err).NotTo(HaveOccurred())
			Expect(generateResult == fooInstance).To(BeTrue(),
				fmt.Sprintf("generateResult (%p) should point to the same instance as fooInstance (%p)", generateResult, fooInstance),
			)
		})
	})

	Describe("RegisterAll", func() {
		It("should register all factories", func() {
			registry.RegisterAll(map[string]goldi.TypeFactory{
				"test_type_1": goldi.NewType(NewFoo),
				"test_type_2": goldi.NewType(NewBar),
				"test_type_3": goldi.NewStructType(MockType{}),
			})

			var typeIsRegistered bool
			_, typeIsRegistered = registry["test_type_1"]
			Expect(typeIsRegistered).To(BeTrue())
			_, typeIsRegistered = registry["test_type_2"]
			Expect(typeIsRegistered).To(BeTrue())
			_, typeIsRegistered = registry["test_type_3"]
			Expect(typeIsRegistered).To(BeTrue())
		})
	})
})