Skip to content
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

Autowire refactor #188

Merged
merged 32 commits into from
Dec 23, 2021
Merged

Autowire refactor #188

merged 32 commits into from
Dec 23, 2021

Conversation

mbore
Copy link
Contributor

@mbore mbore commented Dec 6, 2021

Changes required in #186 led me to rework of autowire, therefor I decided to create a separate PR

@adamw
Copy link
Member

adamw commented Dec 6, 2021

The formatting looks weird - e.g. in src/main/scala/com/softwaremill/macwire/autocats/package.scala - I don't think scalafmt is applied


val graph = new CatsProvidersGraphContext[c.type](c, log)
val sortedProviders = graph.buildGraph(dependencies.toList).topologicalOrder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks nice :)

@mbore mbore force-pushed the autowire-refactor branch from 77580d1 to 37c26d6 Compare December 15, 2021 20:43
@mbore mbore marked this pull request as ready for review December 15, 2021 20:44
@mbore mbore changed the title WIP: Autowire refactor Autowire refactor Dec 16, 2021

val typeCheckUtil: TypeCheckUtil[c.type]

trait Provider {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be sealed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It could be. I left it as a non-sealed trait because refactor required to introduce pure autowire is going to require adding new classes extending Provider trait. But for now, just to focus on the current use case, let's make it sealed.


}

case class Creator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between a creator and a factory? isn't a constructor/apply method just a special case of factory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah there is no real difference in terms of the dependencies graph. I was focused on the different sources of these providers and missed that they are, actually the same... Thanks for the comment!

class B() extends T

object Test {
val theA = autowire[A](new B())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this ambiguous? B is the only instance of type T that we have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -3,6 +3,7 @@ package com.softwaremill.macwire.autocats
import scala.reflect.macros.blackbox
import cats.effect.{IO, Resource => CatsResource}
import com.softwaremill.macwire.internals._
import com.softwaremill.macwire.autocats.internals._

object MacwireCatsEffectMacros {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] name doesn't match file name :)


log(s"Input providers order [${inputProvidersOrder.mkString(", ")}]")

new CatsProvidersGraph(inputProvidersOrder ++ resolvedCtx.providers.diff(inputProvidersOrder), rootProvider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's the purpose here of the inputProvidersOrder and the other providers? Can't we just pass the providers providers ++ fms to the graph and sort them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This point is necessary to sort independent providers in the same way they were passed (I think it's a good initial approach to the problem, because it makes the result order predictable).
I was thinking about two other approaches to this problem:

  • implicit order of providers that needs to be preserved during the whole resolution process - it would be definitely harder to maintain
  • additional sorting logic in topologicalSort - I think it would harm CatsProvidersGraph encaptulation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand why we iterate over rawProviders twice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah did you ask about these providers and fms -https://github.com/softwaremill/macwire/pull/188/files/a78010f1dd0991bf326716caacf5ff14946fb3b6#diff-8923920dc067a35890d1e81851c52f2387bb9d3e173c036c9a714372d7146558R61 ?
The fms name is actually incorrect, because it's a list of FactoryMethodTree that needs to be resolved in resolveFactoryMethods method. We cannot use providers from BuilderContext directly, because resolveFactoryMethods does not preserve the input order, so I think we cannot avoid the second iteration in a simple way.

I extracted a common part to a variable.

@adamw
Copy link
Member

adamw commented Dec 17, 2021

Don't we need to update something in the readme?

Otherwise I think we're good to merge & release :)

@mbore mbore merged commit e67171e into master Dec 23, 2021
@adamw adamw deleted the autowire-refactor branch September 10, 2024 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants