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

refactor(depinject)!: require exported functions & types #12797

Merged
merged 32 commits into from
Aug 31, 2022

Conversation

aaronc
Copy link
Member

@aaronc aaronc commented Aug 2, 2022

Description

Ref #12556

This PR:

  • adds the requirement that all provider and invoker functions and their parameters must be exported and not in internal packages so that these functions are available to codegen
  • makes ProviderDescriptor unexported so that custom providers (which aren't declared a compile time) cannot be built

It does not attempt to handle generic parameters because that involves some complex parsing. It can be added in a future PR if desired.

In a follow-up PR, we will need to make all existing module providers exported and change the core/appconfig code.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

depinject/check_type.go Fixed Show fixed Hide fixed
@aaronc aaronc marked this pull request as ready for review August 10, 2022 16:35
@aaronc aaronc requested a review from a team as a code owner August 10, 2022 16:35
@aaronc aaronc changed the title refactor(depinject)!: require exported functions refactor(depinject)!: require exported functions & types Aug 10, 2022
@aaronc aaronc requested a review from kocubinski August 11, 2022 10:17
}

loc := LocationFromPC(val.Pointer())
loc := LocationFromPC(val.Pointer()).(*location)
Copy link
Member

Choose a reason for hiding this comment

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

Exported Providers and Invokers are a requirement for codegen, did you consider allowing them for them for runtime DI? Maybe this would be too confusing for users of this lib though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you mean allowing unexported providers at runtime. I think it would be too confusing to have different behavior for runtime and compile time. Also, we want to ensure that all code can be executed at compile time if desired so doing this check at runtime ensures that.

@kocubinski
Copy link
Member

kocubinski commented Aug 11, 2022

Overall the direction makes sense given the constraints of codegen. It seems too complex to maintain side by side compatibility with the current runtime API, right?

Also I guess we can’t merge this without some refactoring (you mentioned this in the PR description). Is the plan to branch that work off of this PR?

@aaronc
Copy link
Member Author

aaronc commented Aug 15, 2022

Overall the direction makes sense given the constraints of codegen. It seems too complex to maintain side by side compatibility with the current runtime API, right?

That's my thinking

Also I guess we can’t merge this without some refactoring (you mentioned this in the PR description). Is the plan to branch that work off of this PR?

No, this PR can be merged as is. The other code references a tag of depinject so we can do it in a totally separate PR either with a new tag or a replace directive

// option.Provide(ProviderDescriptor{ ... })
type ProviderDescriptor struct {
// option.Provide(providerDescriptor{ ... })
type providerDescriptor struct {
Copy link
Member Author

Choose a reason for hiding this comment

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

Yes there was a replace directive but when I remove it go can't resolve the tagged module

@julienrbrt
Copy link
Member

julienrbrt commented Aug 17, 2022

Don't we need to go work sync?

@aaronc
Copy link
Member Author

aaronc commented Aug 17, 2022

Maybe the go.work file is inserting an automatic replace directive in the build?

@julienrbrt
Copy link
Member

julienrbrt commented Aug 17, 2022

I believe we always use the latest version with go.work. I can check too.

@aaronc
Copy link
Member Author

aaronc commented Aug 17, 2022

I'm checking to see if removing go.work fixes the issues here

@codecov
Copy link

codecov bot commented Aug 30, 2022

Codecov Report

Merging #12797 (0c8e64e) into main (372a8f1) will increase coverage by 0.92%.
The diff coverage is 80.64%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #12797      +/-   ##
==========================================
+ Coverage   53.17%   54.10%   +0.92%     
==========================================
  Files         641      662      +21     
  Lines       54871    56426    +1555     
==========================================
+ Hits        29180    30528    +1348     
- Misses      23365    23515     +150     
- Partials     2326     2383      +57     
Impacted Files Coverage Δ
depinject/module_dep.go 92.30% <ø> (ø)
depinject/simple.go 100.00% <ø> (ø)
depinject/provider_desc.go 83.87% <74.57%> (ø)
depinject/check_type.go 76.31% <76.31%> (ø)
depinject/config.go 89.41% <100.00%> (ø)
depinject/container.go 89.85% <100.00%> (ø)
depinject/struct_args.go 98.41% <100.00%> (ø)
x/group/keeper/keeper.go 56.25% <0.00%> (-0.40%) ⬇️
depinject/debug.go 96.49% <0.00%> (ø)
depinject/inject.go 73.91% <0.00%> (ø)
... and 18 more

@github-actions github-actions bot added the C:orm label Aug 30, 2022
depinject/config.go Outdated Show resolved Hide resolved
Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

LGTM, one nit on docstrings.

@aaronc aaronc enabled auto-merge (squash) August 31, 2022 17:21
@aaronc aaronc merged commit 7728516 into main Aug 31, 2022
@aaronc aaronc deleted the aaronc/depinject-codegen-require-export branch August 31, 2022 17:37
Wryhder pushed a commit to Wryhder/cosmos-sdk that referenced this pull request Oct 26, 2022
* refactor(depinject)!: require exported functions

* unexport ProviderDescriptor

* WIP on tests

* fix tests and check for bound instance methods

* address merge issues

* WIP on checking valid types

* WIP on checking valid types

* WIP

* tests passing

* revert changes outside module

* docs

* docs

* docs

* add comment

* revert

* update depinject go.mod versions

* remove go.work

* add go.work back

* go mod tidy

* fix docs

Co-authored-by: Julien Robert <julien@rbrt.fr>
tbruyelle added a commit to allinbits/cosmos-sdk-fork that referenced this pull request Sep 15, 2023
Close cosmos#11743

Since cosmos#12797, it's no longer possible to use non-exported functions as
providers. The README used to contain examples with function literals as
provider, so this change replace them with exported functions.

An additional change updates the `BindInterface` arguments, which wasn't
working on my side when running this code in go module called "duck".
tbruyelle added a commit to allinbits/cosmos-sdk-fork that referenced this pull request Sep 15, 2023
Close cosmos#11743

Since cosmos#12797, it's no longer possible to use non-exported functions as
providers. The README used to contain examples with function literals as
provider, so this change replaces them with exported functions.

An additional change updates the `BindInterface` arguments, which wasn't
working on my side when running this code in go module called "duck".
tbruyelle added a commit to allinbits/cosmos-sdk-fork that referenced this pull request Sep 15, 2023
Close cosmos#17743

Since cosmos#12797, it's no longer possible to use non-exported functions as
providers. The README used to contain examples with function literals as
provider, so this change replaces them with exported functions.

An additional change updates the `BindInterface` arguments, which wasn't
working on my side when running this code in go module called "duck".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants