コード例 #1
0
ファイル: types.go プロジェクト: fgrosse/servo-gorilla
// 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"),
	})
}
コード例 #2
0
ファイル: struct_type_test.go プロジェクト: roger2000hk/goldi
func ExampleNewStructType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	// all of the following types are semantically identical
	container.Register("foo_1", goldi.NewStructType(Foo{}))
	container.Register("foo_2", goldi.NewStructType(&Foo{}))
	container.Register("foo_3", goldi.NewStructType(new(Foo)))

	// each reference to the "logger" type will now be resolved to that instance
	fmt.Printf("foo_1: %T\n", container.MustGet("foo_1"))
	fmt.Printf("foo_2: %T\n", container.MustGet("foo_2"))
	fmt.Printf("foo_3: %T\n", container.MustGet("foo_3"))
	// Output:
	// foo_1: *goldi_test.Foo
	// foo_2: *goldi_test.Foo
	// foo_3: *goldi_test.Foo
}
コード例 #3
0
ファイル: types.go プロジェクト: fgrosse/servo-example
// 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%"),
	})
}
コード例 #4
0
ファイル: types.go プロジェクト: fgrosse/servo-logxi
// 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",
		),
	})
}
コード例 #5
0
ファイル: proxy_type_test.go プロジェクト: roger2000hk/goldi
// 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
}
コード例 #6
0
ファイル: alias_type_test.go プロジェクト: roger2000hk/goldi
func ExampleNewAliasType() {
	container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})

	container.Register("logger", goldi.NewStructType(SimpleLogger{}))
	container.Register("default_logger", goldi.NewAliasType("logger"))
	container.Register("logging_func", goldi.NewAliasType("logger::DoStuff"))

	fmt.Printf("logger:         %T\n", container.MustGet("logger"))
	fmt.Printf("default_logger: %T\n", container.MustGet("default_logger"))
	fmt.Printf("logging_func:   %T\n", container.MustGet("logging_func"))
	// Output:
	// logger:         *goldi_test.SimpleLogger
	// default_logger: *goldi_test.SimpleLogger
	// logging_func:   func(string) string
}
コード例 #7
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!
}
コード例 #8
0
ファイル: struct_type_test.go プロジェクト: roger2000hk/goldi
	fmt.Printf("foo_1: %T\n", container.MustGet("foo_1"))
	fmt.Printf("foo_2: %T\n", container.MustGet("foo_2"))
	fmt.Printf("foo_3: %T\n", container.MustGet("foo_3"))
	// Output:
	// foo_1: *goldi_test.Foo
	// foo_2: *goldi_test.Foo
	// foo_3: *goldi_test.Foo
}

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

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

	Describe("goldi.NewStructType()", func() {
		Context("with invalid arguments", func() {
			It("should return an invalid type if the generator is no struct or pointer to a struct", func() {
				Expect(goldi.IsValid(goldi.NewStructType(42))).To(BeFalse())
			})

			It("should return an invalid type if the generator is a pointer to something other than a struct", func() {
				something := "Hello Pointer World!"
				Expect(goldi.IsValid(goldi.NewStructType(&something))).To(BeFalse())
			})
		})
コード例 #9
0
ファイル: alias_type_test.go プロジェクト: roger2000hk/goldi
	})

	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{}))
			Expect(generated.(*Foo).Value).To(Equal("I was created by @foo"))
		})

		It("should work with func reference types", func() {
			container.Register("foo", goldi.NewStructType(Foo{}, "I was created by @foo"))
			alias := goldi.NewAliasType("foo::ReturnString")

			generated, err := alias.Generate(resolver)
			Expect(err).NotTo(HaveOccurred())
			Expect(generated).To(BeAssignableToTypeOf(func(string) string { return "" }))
コード例 #10
0
	// 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!
}

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

var _ = Describe("configuredType", func() {
	var embeddedType goldi.TypeFactory
	BeforeEach(func() {
		embeddedType = goldi.NewStructType(Foo{})
	})

	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("NewConfiguredType()", func() {
		Context("with invalid argument", func() {
			It("should return an invalid type if the embedded type is nil", func() {
				typeDef := goldi.NewConfiguredType(nil, "configurator_type", "Configure")
				Expect(goldi.IsValid(typeDef)).To(BeFalse())
			})
コード例 #11
0
ファイル: proxy_type_test.go プロジェクト: roger2000hk/goldi
	})

	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{}))
			Expect(generated.(*SimpleLogger).Name).To(Equal("My logger"))
		})

		It("should return an error if the referenced type has no such method", func() {
			typeDef := goldi.NewProxyType("foobar", "DoStuff")

			_, err := typeDef.Generate(resolver)
			Expect(err).To(MatchError("could not generate proxy type @foobar::DoStuff : type foobar does not exist"))
		})
コード例 #12
0
ファイル: container_test.go プロジェクト: roger2000hk/goldi
		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))
		_, err := container.Get("foo")
		Expect(err).To(MatchError(`goldi: error while generating type "foo": the given struct is nil`))
	})

	It("should resolve simple types", func() {
		registry.RegisterType("test_type", NewMockType)
		Expect(container.MustGet("test_type")).To(BeAssignableToTypeOf(&MockType{}))
	})

	It("should build the types lazily", func() {
		typeID := "test_type"
		generator := &MockTypeFactory{}
		registry.RegisterType(typeID, generator.NewMockType)

		generatorWrapper, typeIsRegistered := registry[typeID]
コード例 #13
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())
		})
	})
})