Replies: 4 comments 3 replies
-
I recommend inlining func NewServer() Server {
srv := Server{}
server.AddRoute("/login")
return srv
} Wire assumes that the provider functions do any necessary initialization before returning the value. |
Beta Was this translation helpful? Give feedback.
-
Hi @khanakia , you can use a factory to build a server and then depend on the factory to create the server, add customizations and return it. Here is an example: // other.go
type Dependency struct{}
type Dependency2 struct{}
// server.go
// server needs 2 dependencies
type Server struct {
Dep Dependency
Dep2 Dependency2
}
func (server Server) AddRoute(path string) {}
func (server Server) Start() {
fmt.Println("started")
}
func NewAuth(server Server) {
server.AddRoute("/login")
}
// server factory holds Dependency2 which will be injected into the server
type ServerFactory struct{
Dep2 Dependency2
}
func (s ServerFactory) Create() Server {
return CreateServer(s.Dep2)
}
// without any customization
func provideDefaultServer(factory ServerFactory) Server {
return factory.Create()
}
// our hook for customizations
func provideCustomizedServer(factory ServerFactory) Server {
server := factory.Create()
NewAuth(server)
return server
}
// default set to create the server and dep1
var serverSet = wire.NewSet(
wire.Struct(new(Server), "*"),
wire.Struct(new(Dependency), "*"),
)
// set to create server facotry and dep2
var serverFactorySet = wire.NewSet(
wire.Struct(new(ServerFactory), "*"),
wire.Struct(new(Dependency2), "*"),
)
// default set to create server without any customization
var defaultServerSet = wire.NewSet(
serverFactorySet,
provideDefaultServer,
)
// wire.go
func CreateDefaultServer() (Server, error) {
panic(wire.Build(defaultServerSet))
}
func CreateCustomizedServer() (Server, error) {
panic(wire.Build(serverFactorySet, provideCustomizedServer))
}
func CreateServer(Dep2 Dependency2) Server {
panic(wire.Build(serverSet))
}
// wire_gen.go
func CreateDefaultServer() (Server, error) {
dependency2 := Dependency2{}
serverFactory := ServerFactory{
Dep2: dependency2,
}
server := provideDefaultServer(serverFactory)
return server, nil
}
func CreateCustomizedServer() (Server, error) {
dependency2 := Dependency2{}
serverFactory := ServerFactory{
Dep2: dependency2,
}
server := provideCustomizedServer(serverFactory)
return server, nil
}
func CreateServer(Dep2 Dependency2) Server {
dependency := Dependency{}
server := Server{
Dep: dependency,
Dep2: Dep2,
}
return server
}
|
Beta Was this translation helpful? Give feedback.
-
my thinking was you'll use one of the function based on what you need.
if not the factory could return the same instance if that's what you need
…On Sat, May 15, 2021, 19:24 Aman Bansal ***@***.***> wrote:
@zlymeda <https://github.com/zlymeda>
You example looks good but the problem is want to pass the same *Server
Pointer* i do not want to create new *Server* instance everytime i call
CreateDefaultServer, CreateCustomizedServer and CreateServer
FYI: You can assume Server as HTTP server
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#288 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEUA2H3MTDT2RYZJ63UCI3TN2U35ANCNFSM44FCDETA>
.
|
Beta Was this translation helpful? Give feedback.
-
I personally think this error is unnececery, because wire already have provider missing error to prevent this case. In my case I want to pass in a Register object that newly created object can init and self-register, example: //register.go
type Register struct {
M map[string]Thing
}
func New() Register {
return Register{make()}
}
//thing.go
var Set = wire.NewSet(register.New, Init)
func Init(reg Register) {
thing
//init thing
reg.M["a"] = thing
} that I can make it happen, actully this is a kind of workaround of binding multiple type to same interface which wire don't support. by the way, if wire support bind multiple type to same interface, I can acheive the same thing by //register.go
var Set = wire.NewSet(New, wire.Bind(thing1.New, Thing), wire.Bind(thing2.New, Thing)
type Register struct {
M map[int]Thing
}
type Thing interface{} // some methods
func New(ts ...Thing) Register {
r := Register{make(map[int]Thing)}
for i, t := range ts {
r.M[i] = t
}
return r
}
//thing1.go
type Thing1 struct{}
func New() *Thing1 {} the problem is, I use wire because I want to write less code and make my code looks clean, but with this two limitations, I end up writing more code than before. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
I want to achieve this
wire.go
returns error
Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
How do i use the
NewAuth(server)
without returning any value ?Beta Was this translation helpful? Give feedback.
All reactions