func (c *TypeReferencesConstraint) validateTypeReferences(typeID string, container *goldi.Container, allArguments []interface{}) error {
	typeRefParameters := c.typeReferenceArguments(allArguments)
	for _, referencedTypeID := range typeRefParameters {
		if c.checkedTypes.Contains(referencedTypeID) {
			// TEST: test this for improved code coverage
			continue
		}

		referencedTypeFactory, err := c.checkTypeIsDefined(goldi.NewTypeID(typeID).ID, goldi.NewTypeID(referencedTypeID).ID, container)
		if err != nil {
			return err
		}

		c.circularDependencyCheckMap = gotility.StringSet{}
		c.circularDependencyCheckMap.Set(typeID)
		if err = c.checkCircularDependency(referencedTypeFactory, referencedTypeID, container); err != nil {
			return err
		}

		c.checkedTypes.Set(referencedTypeID)
	}
	return nil
}
func (c *TypeReferencesConstraint) checkCircularDependency(typeFactory goldi.TypeFactory, typeID string, container *goldi.Container) error {
	allArguments := typeFactory.Arguments()
	typeRefParameters := c.typeReferenceArguments(allArguments)

	for _, referencedTypeID := range typeRefParameters {
		referencedType, err := c.checkTypeIsDefined(goldi.NewTypeID(typeID).ID, goldi.NewTypeID(referencedTypeID).ID, container)
		if err != nil {
			// TEST: test this for improved code coverage
			return nil
		}

		if c.circularDependencyCheckMap.Contains(referencedTypeID) {
			return fmt.Errorf("detected circular dependency for type %q (referenced by %q)", referencedTypeID, typeID)
		}

		c.circularDependencyCheckMap.Set(typeID)
		if err = c.checkCircularDependency(referencedType, referencedTypeID, container); err != nil {
			return err
		}
	}

	return nil
}
예제 #3
0
package goldi_test

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

	"github.com/fgrosse/goldi"
)

var _ = Describe("TypeID", func() {
	Describe("NewTypeID", func() {
		It("should panic if given an empty string", func() {
			Expect(func() { goldi.NewTypeID("") }).To(Panic())
		})
	})

	Describe("String", func() {
		It("should return the raw type if present", func() {
			t := goldi.TypeID{Raw: "@foo"}
			Expect(t.String()).To(Equal("@foo"))
		})

		It("should work if just the ID is set", func() {
			t := goldi.TypeID{ID: "foo"}
			Expect(t.String()).To(Equal("@foo"))
		})

		It("should use FuncReferenceMethod if it is not empty", func() {
			t := goldi.TypeID{ID: "foo", FuncReferenceMethod: "DoStuff"}
			Expect(t.String()).To(Equal("@foo::DoStuff"))
		})