Exemple #1
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%"),
	})
}
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
}
	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
}

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

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

	Describe("NewFuncReferenceType()", func() {
		It("should return an invalid type if the method name is not exported", func() {
			t := goldi.NewFuncReferenceType("foo", "doStuff")
			Expect(goldi.IsValid(t)).To(BeFalse())
			Expect(t).To(MatchError(`can not use unexported method "doStuff" as second argument to NewFuncReferenceType`))
		})
	})

	Describe("Arguments()", func() {
		It("should return the referenced service ID", func() {
			typeDef := goldi.NewFuncReferenceType("my_controller", "FancyAction")
	var (
		registry  goldi.TypeRegistry
		config    map[string]interface{}
		container *goldi.Container
		validator *validation.ContainerValidator
	)

	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)