-
Notifications
You must be signed in to change notification settings - Fork 76
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
Implement wireResource for scala 2 #175
Merged
Merged
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
87c4e2a
init work
mbore d414d7d
wire single resource
mbore 2ed2768
Add support for multiple resources
mbore c42f895
add wireRec-like composition
mbore 5cc79d4
use default constructors & companion objects
mbore f4ad58c
Refactor crimpers
mbore 9434160
Fix test
mbore 26bfca2
Rename method
mbore 79f752c
Apply CompanionCrimper changes
mbore a038fb8
Apply ConstructorCrimper changes
mbore 46b3835
Add some tests for wireApp
adamw 6a9888a
Add assertions to tests, refactor tests
adamw 90b60e4
Add test for IOs
adamw 7e34d33
Add factory testsx
adamw 2b59d75
Failure tests with path
adamw 18cbd08
Move IO wrapped instances out of the scope for the current work
mbore 1373434
Add support for instances
mbore 0d2d0d0
Add basic support for factory methods
mbore 42b5eea
Add todo for resource factory method
mbore 0d797be
Rename
mbore 066f0a9
Update readme
mbore f190acd
Add IO support
mbore e71c561
Updated readme
mbore d08ddfc
Fix typo
mbore cd9266a
change package name
mbore 5a04285
Add subtype test case
mbore 4e42834
Updated readme
mbore 4232a19
Fix name
mbore 4b70ca2
Add support for subtyping
mbore 2bb2a00
Add tagging example
mbore 53cdea2
change package name
mbore 0446fc3
Add subtype test for resource
mbore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,53 @@ trait UserModule { | |
|
||
This feature is inspired by @yakivy's work on [jam](https://github.com/yakivy/jam). | ||
|
||
## Auto wiring | ||
|
||
In case you need to build an instance from some particular instances and factory methods it's recommended to use `autowire`. This feature is intended to interpolate with fp libraries (currently we support `cats`). | ||
|
||
`autowire` takes as an argument a list which may contain: | ||
* values (e.g. `new A()`) | ||
* factory methods (e.g. `C.create _`) | ||
* cats.effect.Resource (e.g. `cats.effect.Resource[IO].pure(new A())`) | ||
* cats.effect.IO (e.g. `cats.effect.IO.pure(new A())`) | ||
Based on the given list it creates a set of available instances and performs `wireRec` bypassing the instances search phase. The result of the wiring is always wrapped in `cats.effect.Resource`. For example: | ||
|
||
```Scala | ||
import cats.effect._ | ||
|
||
class DatabaseAccess() | ||
|
||
class SecurityFilter private (databaseAccess: DatabaseAccess) | ||
object SecurityFilter { | ||
def apply(databaseAccess: DatabaseAccess): SecurityFilter = new SecurityFilter(databaseAccess) | ||
} | ||
|
||
class UserFinder(databaseAccess: DatabaseAccess, securityFilter: SecurityFilter) | ||
class UserStatusReader(databaseAccess: DatabaseAccess, userFinder: UserFinder) | ||
|
||
object UserModule { | ||
import com.softwaremill.macwire._ | ||
|
||
val theDatabaseAccess: Resource[IO, DatabaseAccess] = Resource.pure(new DatabaseAccess()) | ||
|
||
val theUserStatusReader: Resource[IO, UserStatusReader] = autowire[UserStatusReader](theDatabaseAccess) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ this looks nice :) |
||
} | ||
``` | ||
|
||
will generate | ||
```Scala | ||
[...] | ||
object UserModule { | ||
import com.softwaremill.macwire._ | ||
|
||
val theDatabaseAccess: Resource[IO, DatabaseAccess] = Resource.pure(new DatabaseAccess()) | ||
|
||
val theUserStatusReader: Resource[IO, UserStatusReader] = UserModule.this.theDatabaseAccess.flatMap( | ||
da => Resource.pure[IO, UserStatusReader](new UserStatusReader(da, new UserFinder(da, SecurityFilter.apply(da)))) | ||
) | ||
} | ||
``` | ||
|
||
## Composing modules | ||
|
||
Modules (traits or classes containing parts of the object graph) can be combined using inheritance or composition. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what if multiple values for a given type are found? E.g. through a value and a resource? I think it should be an error
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.
Currently it searches for instances, then resources, then effects. I agree that is should fail in such case, also it may be worth to check if all passed instances/resources/factory methods are used in the generated code.
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.
I've created #181 & #182 to cover these