Wire is not initializing a conditional struct

42 views
Skip to first unread message

Em

unread,
Nov 2, 2020, 1:25:37 PM11/2/20
to go-cloud
So this is current code that i am trying to modify to DI 

var logger service.Logger
if !colorLogsEnabled {
    logger = service.NewLogger(os.Stdout, ...)
} else {
    logger = service.NewColoredLogger(os.Stdout, ...)
}

The rest of the arguments which are being passed in NewLogger or NewColoredLogger is of type func(*service.Logger)

-----
I tried to change it to this 
//main.go
logger := InitializeLogger(os.Stdout, ...)

And then 
//wire.go
func InitializeLogger(colorLogsEnabled bool, writer io.Writer, options ...func(*service.Logger)) service.Logger {
if !colorLogsEnabled {
panic(wire.Build(service.New))
} else {
panic(wire.Build(service.NewColorLogger))
}
}


When i did `wire` on command line. it didn't show any errors but it didn't generate any file either. 

rvan...@google.com

unread,
Nov 2, 2020, 1:29:17 PM11/2/20
to go-cloud
https://github.com/google/wire/blob/master/docs/guide.md#injectors

The body of an injector function can only include the call to wire.Build.

Reply all
Reply to author
Forward
0 new messages