Skip to content

Commit

Permalink
fix #1564
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jul 24, 2020
1 parent d259324 commit 18a8d13
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ Other Improvements:

![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0)

- Fix [#1564](https://github.com/kataras/iris/issues/1564).

- Fix [#1553](https://github.com/kataras/iris/issues/1553).

- New `DirOptions.Cache` to cache assets in-memory among with their compressed contents (in order to be ready to served if client ask). Learn more about this feature by reading [all #1556 comments](https://github.com/kataras/iris/issues/1556#issuecomment-661057446). Usage:
Expand Down
6 changes: 5 additions & 1 deletion hero/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,12 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, paramsCou
// fmt.Printf("Controller [%s] | Field Index: %v | Field Type: %s\n", typ, fields[i].Index, fields[i].Type)
inputs[i] = fields[i].Type
}

exportedBindings := getBindingsFor(inputs, dependencies, paramsCount)
// fmt.Printf("Controller [%s] | Inputs length: %d vs Bindings length: %d\n", typ, n, len(exportedBindings))
// fmt.Printf("Controller [%s] | Inputs length: %d vs Bindings length: %d | Stateless : %d\n", typ, n, len(exportedBindings), stateless)
// for i, b := range exportedBindings {
// fmt.Printf("[%d] [Static=%v] %s\n", i, b.Dependency.Static, b.Dependency.DestType)
// }

if stateless == 0 && len(nonZero) >= len(exportedBindings) {
// if we have not a single stateless and fields are defined then just return.
Expand Down
20 changes: 20 additions & 0 deletions hero/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,28 @@ func fromDependentFunc(v reflect.Value, dest *Dependency, funcDependencies []*De
}

bindings := getBindingsForFunc(v, funcDependencies, -1 /* parameter bindings are disabled for depent dependencies */)

numIn := typ.NumIn()
numOut := typ.NumOut()

// d1 = Logger
// d2 = func(Logger) S1
// d2 should be static: it accepts dependencies that are static
// (note: we don't check the output argument(s) of this dependnecy).
if numIn == len(bindings) {
static := true
for _, b := range bindings {
if !b.Dependency.Static && matchDependency(b.Dependency, typ.In(b.Input.Index)) {
static = false
break
}
}

if static {
dest.Static = static
}
}

firstOutIsError := numOut == 1 && isError(typ.Out(0))
secondOutIsError := numOut == 2 && isError(typ.Out(1))

Expand Down Expand Up @@ -241,5 +260,6 @@ func fromDependentFunc(v reflect.Value, dest *Dependency, funcDependencies []*De

dest.DestType = typ.Out(0)
dest.Handle = handler

return true
}
36 changes: 36 additions & 0 deletions hero/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,39 @@ func testDependencies(t *testing.T, tests []testDependencyTest) {
// t.Logf("[%d] output: %#+v", i, val.Interface())
}
}

func TestDependentDependencyInheritanceStatic(t *testing.T) {
// Tests the following case #1564:
// Logger
// func(Logger) S1
// ^ Should be static because Logger
// is a structure, a static dependency.
//
// func(Logger) S2
// func(S1, S2) S3
// ^ Should be marked as static dependency
// because everything that depends on are static too.

type S1 struct {
msg string
}

type S2 struct {
msg2 string
}

serviceDep := NewDependency(&testServiceImpl{prefix: "1"})
d1 := NewDependency(func(t testService) S1 {
return S1{t.Say("2")}
}, serviceDep)
if !d1.Static {
t.Fatalf("d1 dependency should be static: %#+v", d1)
}

d2 := NewDependency(func(t testService, s S1) S2 {
return S2{"3"}
}, serviceDep, d1)
if !d2.Static {
t.Fatalf("d2 dependency should be static: %#+v", d2)
}
}

0 comments on commit 18a8d13

Please sign in to comment.