コード例 #1
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",
		),
	})
}
コード例 #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!
}
コード例 #3
0
	// 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())
			})

			It("should return an invalid type if either the configurator ID or method is empty", func() {
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", ""))).To(BeFalse())
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "configurator_type", ""))).To(BeFalse())
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", "configure"))).To(BeFalse())