Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func (c *Container) analyzeFunction(fnType reflect.Type) (fnSignature, error) {
}

firstOut := fnType.Out(0)
if firstOut.Kind() != reflect.Pointer {
return fnSignature{}, fmt.Errorf("constructor must return pointer value as first result")
if firstOut.Kind() != reflect.Pointer && firstOut.Kind() != reflect.Interface {
return fnSignature{}, fmt.Errorf("constructor must return pointer or interface value as first result")
}

if fnType.NumOut() == 2 {
Expand Down Expand Up @@ -247,6 +247,12 @@ func (c *Container) resolveInterfaces() error {

interfaceType := signature.args[i]

// If there's already a constructor that directly returns this interface type, skip resolution
if _, exists := c.typesCtors[interfaceType]; exists {
c.debugf("interface %s has a direct constructor, skipping resolution", interfaceType)
continue
}

// Find implementation
implementations := c.findImplementations(interfaceType)
if len(implementations) == 0 {
Expand Down
63 changes: 63 additions & 0 deletions tests/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,69 @@ var _ = Describe("Container", func() {
})
})

Describe("Interface-Returning Constructors", func() {
It("should support constructor returning interface", func() {
newStorage := func() Storage {
return &FileStorage{path: "/data"}
}

Expect(container.Provide(newStorage)).To(Succeed())

var storage Storage
Expect(container.Resolve(&storage)).To(Succeed())
Expect(storage).ToNot(BeNil())

fs, ok := storage.(*FileStorage)
Expect(ok).To(BeTrue())
Expect(fs.path).To(Equal("/data"))
})

It("should support constructor returning (interface, error)", func() {
newStorage := func() (Storage, error) {
return &FileStorage{path: "/data"}, nil
}

Expect(container.Provide(newStorage)).To(Succeed())

var storage Storage
Expect(container.Resolve(&storage)).To(Succeed())
Expect(storage).ToNot(BeNil())

fs, ok := storage.(*FileStorage)
Expect(ok).To(BeTrue())
Expect(fs.path).To(Equal("/data"))
})

It("should propagate error from (interface, error) constructor", func() {
newStorage := func() (Storage, error) {
return nil, errors.New("storage init failed")
}

Expect(container.Provide(newStorage)).To(Succeed())

var storage Storage
Expect(container.Resolve(&storage)).To(MatchError(ContainSubstring("storage init failed")))
})

It("should use interface-returning constructor as dependency", func() {
newStorage := func() Storage {
return &FileStorage{path: "/injected"}
}

Expect(container.Provide(newStorage)).To(Succeed())
Expect(container.Provide(NewDataProcessor)).To(Succeed())

var processor *DataProcessor
Expect(container.Resolve(&processor)).To(Succeed())
Expect(processor).ToNot(BeNil())
Expect(processor.storage).ToNot(BeNil())

fs, ok := processor.storage.(*FileStorage)
Expect(ok).To(BeTrue())
Expect(fs.path).To(Equal("/injected"))
})
})

Describe("Error Handling", func() {
It("should handle constructor errors", func() {
Expect(container.Provide(NewErrorDatabase)).To(Succeed())
Expand Down
Loading