Beispiel #1
0
// Let's assume that we have a LoggerProvider type that produces configured instances
// of a Logger each time we call LoggerProvider.GetLogger(loggerName string).
//
// The example shows how to register a `logger` as proxy for a specific call to this LoggerProvider.
func ExampleNewProxyType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	// register some type as always
	container.Register("logger_provider", goldi.NewStructType(LoggerProvider{}))

	// register a proxy type that references the method of previously defined type and append call arguments if any
	container.Register("logger", goldi.NewProxyType("logger_provider", "GetLogger", "My logger"))

	l := container.MustGet("logger").(*SimpleLogger)
	fmt.Printf("%s: %T", l.Name, l)
	// Output:
	// My logger: *goldi_test.SimpleLogger
}
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
	// register a proxy type that references the method of previously defined type and append call arguments if any
	container.Register("logger", goldi.NewProxyType("logger_provider", "GetLogger", "My logger"))

	l := container.MustGet("logger").(*SimpleLogger)
	fmt.Printf("%s: %T", l.Name, l)
	// Output:
	// My logger: *goldi_test.SimpleLogger
}

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

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

	Describe("NewProxyType()", func() {
		It("should return an invalid type if the method name is not exported", func() {
			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")