wire
wire copied to clipboard
wire: allow function literals to be used as provider functions
Related to #7 but not quite the same. The documentation for NewSet says that the argument can be a function value. A function literal is a function value, and so should be allowed by that rule, but cannot be used:
For example, this code yields an "unknown pattern" error.
var fwire = wire.NewSet(
func() string {
return "x"
},
)
It shouldn't be hard to support function literals (and method expressions too) as long as they don't refer to external variables.
Thanks @rogpeppe for the suggestion! This needs some design work we want to at some point revisit. We think this might provide shortcuts as an alternative to the use cases described in #188 and #193 . Meanwhile, if there is any example of an actual use case you have please feel free to post as we consider the design.
Meanwhile, if there is any example of an actual use case you have please feel free to post as we consider the design.
My use case (#241) is to have functions that return the functions wire
should execute when it builds the set.
An example of such a function is:
func ProvideDDBClientWith(ddbEndpoint string) func(client.ConfigProvider) dynamodbiface.DynamoDBAPI {
return func(awsSession client.ConfigProvider) dynamodbiface.DynamoDBAPI {
return dynamodbclient.New(awsSession, ddbEndpoint)
}
}
While in wire.Build
this would be used as:
func Inject(cfg Config) (Server, func(), error) (
wire.Build(
...
ProvideDDBClientWith(cfg.DDBEndpoint),
...
)
return Server{}, nil, nil
}
More information and discussion of this use case can be found over in that issue (#241).