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

[Documentation]: depinject examples are wrong #17743

Closed
tbruyelle opened this issue Sep 14, 2023 · 2 comments · Fixed by #17758
Closed

[Documentation]: depinject examples are wrong #17743

tbruyelle opened this issue Sep 14, 2023 · 2 comments · Fixed by #17758
Labels
T:Docs Changes and features related to documentation.

Comments

@tbruyelle
Copy link
Contributor

Summary

In depinject/README.md, all code examples use function literals as providers, but this is not authorized any more.

By leveraging `depinject` and its Configuration API, you can efficiently handle dependencies in your blockchain application, ensuring a clean, modular, and well-organised codebase.
Example:
```go
package main
import (
"fmt"
"cosmossdk.io/depinject"
)
type AnotherInt int
func main() {
var (
x int
y AnotherInt
)
fmt.Printf("Before (%v, %v)\n", x, y)
depinject.Inject(
depinject.Provide(
func() int { return 1 },
func() AnotherInt { return AnotherInt(2) },
),
&x,
&y,
)
fmt.Printf("After (%v, %v)\n", x, y)
}
```

If you execute the code in example, depinject.Inject will return an error Failed registering providers because of: function must be exported: main.main.func1.

I don't know the reason of why this isn't authorized any more, but now this example should actually looks like that :

func GetInt() int               { return 1 }
func GetAnotherInt() AnotherInt { return 2 }

func main() {
	var (
		x int
		y AnotherInt
	)

	fmt.Printf("Before (%v, %v)\n", x, y)
	depinject.Inject(
		depinject.Provide(
			GetInt,
			GetAnotherInt,
		),
		&x,
		&y,
	)
	fmt.Printf("After (%v, %v)\n", x, y)
}
@tbruyelle tbruyelle added the T:Docs Changes and features related to documentation. label Sep 14, 2023
@julienrbrt
Copy link
Member

The reason was to eventually support codegen in depinject: #12556.
That's a good catch, thanks! Do you want to submit a PR?

@tbruyelle
Copy link
Contributor Author

OK thanks for the rational.

Do you want to submit a PR?

Sure I can do that.

tbruyelle added a commit to allinbits/cosmos-sdk-fork that referenced this issue 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
T:Docs Changes and features related to documentation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants