Skip to content

Commit ff2ae2f

Browse files
committed
do not resolve not required elements
log stacktrace on panic on service start
1 parent 1ad25dc commit ff2ae2f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

godif.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func ResolveAll() errs.Errors {
134134
}
135135

136136
for target, provVar := range provided {
137+
if !targetRequired(target) {
138+
continue
139+
}
137140
if targetValue := reflect.ValueOf(target).Elem(); targetValue.IsNil() {
138141
targetValue.Set(reflect.ValueOf(provVar[0].elem))
139142
}
@@ -196,6 +199,15 @@ func ResolveAll() errs.Errors {
196199
return nil
197200
}
198201

202+
func targetRequired(target interface{}) bool {
203+
for _, elem := range required {
204+
if elem.elem == target {
205+
return true
206+
}
207+
}
208+
return false
209+
}
210+
199211
func isSlice(kind reflect.Kind) bool {
200212
return kind == reflect.Array || kind == reflect.Slice
201213
}

godif_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func TestPackageUsedIfSomethigRequired(t *testing.T) {
292292
t.Fatal()
293293
}
294294
assert.NotNil(t, injected1)
295-
assert.NotNil(t, injected2)
295+
assert.Nil(t, injected2) // not required
296296
}
297297

298298
func TestPackageNotUsedIfNothingRequired(t *testing.T) {
@@ -323,6 +323,7 @@ func TestDataInject(t *testing.T) {
323323
Reset()
324324
var injected map[string]int
325325
Provide(&injected, make(map[string]int))
326+
Require(&injected)
326327

327328
errs := ResolveAll()
328329
if errs != nil {
@@ -350,6 +351,9 @@ func TestProvideExtensionMapBasic(t *testing.T) {
350351
Provide(&bucketDefsPtr, map[string]*bucketDef{})
351352
Provide(&bucketDefs, map[string]bucketDef{})
352353
Provide(&bucketDefsByKey, map[key]bucketDef{})
354+
Require(&bucketDefsPtr)
355+
Require(&bucketDefs)
356+
Require(&bucketDefsByKey)
353357

354358
var bucketServicePtr = &bucketDef{Value: "val"}
355359
var bucketService = bucketDef{Value: "val"}
@@ -586,6 +590,9 @@ func TestProvideExtensionSliceBasic(t *testing.T) {
586590
Provide(&mySlice, make([]string, 0))
587591
Provide(&bucketDefs, make([]bucketDef, 0))
588592
Provide(&bucketDefsPtr, make([]*bucketDef, 0))
593+
Require(&mySlice)
594+
Require(&bucketDefs)
595+
Require(&bucketDefsPtr)
589596

590597
var bucketServicePtr = &bucketDef{Value: "val"}
591598
var bucketService = bucketDef{Value: "val"}

services/impl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/signal"
1616
"reflect"
17+
"runtime/debug"
1718

1819
"github.com/untillpro/godif"
1920
)
@@ -85,7 +86,7 @@ func Start(startingCtx context.Context, servicesToStart []IService, verbose bool
8586
defer func() {
8687
if r := recover(); r != nil {
8788
if verbose {
88-
logln(fmt.Sprintf("Service paniced: %v: %v", reflect.TypeOf(startingService), r))
89+
logln(fmt.Sprintf("Service paniced: %v: %v\n%s", reflect.TypeOf(startingService), r, string(debug.Stack())))
8990
}
9091
err = &EPanic{PanicData: r, PanicedService: startingService}
9192
}

0 commit comments

Comments
 (0)