-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@DependencyClient
macro
#132
Conversation
with: | ||
branch: swift-5.8-release | ||
tag: 5.8-RELEASE | ||
# windows: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @brianmichel, we've been experiencing flakiness with windows CI and so have been slowly disabling them. Not sure if this is something you want to look into eventually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @mbrandonw sorry for the delay, we're in burn down mode for getting the first version of the Windows app out to a few folks, I'm not exactly sure what's going on here, but have a personal todo to take a peek once I have more free time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @brianmichel, no problem at all! We're happy to turn it back on when possible, but definitely not urgent. Thanks for responding!
@mbrandonw @stephencelis Generating this part isn't included in the macro? extension DependencyValues {
public var myService: MyService {
get { self[myService.self] }
set { self[myService.self] = newValue }
}
} |
@hadiidbouk that part cannot be generated by the macro. Macros are not capable of extending types other than the one it is attached to. |
@mbrandonw Ohh I was hoping to avoid adding this boilerplate code, it's almost the same for all our dependencies. |
Oh I've missed this, this is great! |
This PR adds a new
DependenciesMacros
1 target to the library, which houses a couple macros that aid in designing dependencies for your application and for use with this library.The main macro is
@DependencyClient
, which can be applied to the "struct of closures" style of dependency clients:This does a few things for you. First, it automatically provides a default for each endpoint that simply throws an error and triggers an XCTest failure. This means you get an "unimplemented" client for free with no additional work. This allows you to simplify the
testValue
of yourTestDependencyKey
conformance like so:This behaves the exact same as before, but now all of the code is generated for you.
Further, when you provide argument labels the client's closure endpoints, the macro turns that information into methods with argument labels. This means you can invoke the
play
endpoint like so:And finally, the macro also generates a public initializer for you with all of the client's endpoints. One typically needs to maintain this initializer when separate the interface of the dependency from the implementation (see
Separating interface and implementation for more information). But now there is no need to maintain that code as it is automatically provided for you by the macro.
Footnotes
DependenciesMacros
is defined as a new module so folks do not automatically incur the cost of building SwiftSyntax, which macros depend on and is quite heavyweight. ↩