Exemplo n.º 1
0
func handlerFunc(endpointTypeID string, container *goldi.Container) (http.HandlerFunc, error) {
	h, err := container.Get(endpointTypeID)
	if err != nil {
		return nil, err
	}

	if h, ok := h.(func(http.ResponseWriter, *http.Request)); ok {
		return h, nil
	}

	if h, ok := h.(http.Handler); ok {
		return func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}, nil
	}

	return nil, fmt.Errorf("Type %q is neither a http.Handler not a http.HandlerFunc but a %T", endpointTypeID, h)
}
Exemplo n.º 2
0
			Expect(t).To(MatchError("the struct MockType has only 2 fields but 3 arguments where provided"))
		})
	})

	Describe("Arguments()", func() {
		It("should return all factory arguments", func() {
			args := []interface{}{"foo", true}
			typeDef := goldi.NewStructType(MockType{}, args...)
			Expect(typeDef.Arguments()).To(Equal(args))
		})
	})

	Describe("Generate()", func() {
		var (
			config    = map[string]interface{}{}
			container *goldi.Container
			resolver  *goldi.ParameterResolver
		)

		BeforeEach(func() {
			container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
			resolver = goldi.NewParameterResolver(container)
		})

		Context("without struct arguments", func() {
			Context("when the factory is a struct (no pointer)", func() {
				It("should generate the type", func() {
					typeDef := goldi.NewStructType(MockType{})
					Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
				})
			})
Exemplo n.º 3
0
		var factory goldi.TypeFactory
		factory = goldi.NewAliasType("foo")
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("Arguments()", func() {
		It("should return the aliased service ID", func() {
			typeDef := goldi.NewAliasType("foo")
			Expect(typeDef.Arguments()).To(Equal([]interface{}{"@foo"}))
		})
	})

	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{}))
Exemplo n.º 4
0
	Describe("Arguments()", func() {
		It("should return the arguments of the embedded type and the configurator as type ID", func() {
			embeddedType = goldi.NewStructType(Foo{}, "%param_of_embedded%", "another param")
			typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
			Expect(typeDef.Arguments()).NotTo(BeNil())
			Expect(typeDef.Arguments()).To(HaveLen(3))
			Expect(typeDef.Arguments()).To(ContainElement("%param_of_embedded%"))
			Expect(typeDef.Arguments()).To(ContainElement("another param"))
			Expect(typeDef.Arguments()).To(ContainElement("@configurator_type"))
		})
	})

	Describe("Generate()", func() {
		var (
			config    = map[string]interface{}{}
			container *goldi.Container
			resolver  *goldi.ParameterResolver
		)

		BeforeEach(func() {
			container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
			resolver = goldi.NewParameterResolver(container)
		})

		It("should get the embedded type and configurator and configure it", func() {
			typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
			configurator := &MyConfigurator{ConfiguredValue: "success!"}
			container.Register("configurator_type", goldi.NewInstanceType(configurator))

			generatedType, err := typeDef.Generate(resolver)
			Expect(err).NotTo(HaveOccurred())
Exemplo n.º 5
0
			})
		})
	})

	Describe("Arguments()", func() {
		It("should return all factory arguments", func() {
			args := []interface{}{"foo", true}
			typeDef := goldi.NewType(NewMockTypeWithArgs, args...)
			Expect(typeDef.Arguments()).To(Equal(args))
		})
	})

	Describe("Generate()", func() {
		var (
			config    = map[string]interface{}{}
			container *goldi.Container
			resolver  *goldi.ParameterResolver
		)

		BeforeEach(func() {
			container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
			resolver = goldi.NewParameterResolver(container)
		})

		Context("without factory function arguments", func() {
			It("should generate the type", func() {
				typeDef := goldi.NewType(NewMockType)
				Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
			})
		})
Exemplo n.º 6
0
package goldi_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/fgrosse/goldi"
	"reflect"
)

var _ = Describe("ParameterResolver", func() {

	var (
		config    map[string]interface{}
		container *goldi.Container
		resolver  *goldi.ParameterResolver
	)

	BeforeEach(func() {
		config = map[string]interface{}{}
		container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
		resolver = goldi.NewParameterResolver(container)
	})

	It("should return static parameters", func() {
		parameter := reflect.ValueOf(42)
		expectedType := parameter.Type()
		Expect(resolver.Resolve(parameter, expectedType)).To(Equal(parameter))
	})

	Context("with dynamic parameters", func() {
Exemplo n.º 7
0
			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")
			Expect(typeDef.Arguments()).To(Equal([]interface{}{"@logger_provider", "My logger"}))
		})
	})

	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{}))
Exemplo n.º 8
0
	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
}

var _ = Describe("Container", func() {
	var (
		registry  goldi.TypeRegistry
		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))
Exemplo n.º 9
0
package goldi_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"fmt"
	"github.com/fgrosse/goldi"
)

var _ = Describe("TypeConfigurator", func() {
	var container *goldi.Container

	BeforeEach(func() {
		container = goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
	})

	Describe("Configure", func() {
		Context("when the type configurator has not been defined correctly", func() {
			It("should return an error", func() {
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Configure")
				Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator type "@configurator" has not been defined`))
			})
		})

		Context("when the type configurator is no struct or pointer to struct", func() {
			It("should return an error", func() {
				container.InjectInstance("configurator", 42)
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Configure")