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

Possible improvement on config.LoadDefaultConfig to accept existing config.LoadOptionsFunc type instead of dynamic function signature #2193

Closed
migueleliasweb opened this issue Jul 17, 2023 · 8 comments
Assignees
Labels
feature-request A feature should be added or improved. needs-review This issue or pull request needs review from a core team member. needs-triage This issue or PR still needs to be triaged. p3 This is a minor priority issue

Comments

@migueleliasweb
Copy link

Describe the bug

The function config.LoadDefaultConfig second parameter is typed optFns ...func(*LoadOptions) error instead of using the previouisly defined config.LoadOptionsFunc.

This forces users to redefine the whole type if they want to dinamically use the variadic options.

Expected Behavior

If the function config.LoadDefaultConfig used config.LoadOptionsFunc for its second parameter's type, this would make it a more elegant and concise code.

Current Behavior

If a user tries to use config.LoadOptionsFunc they will get the following message:

cannot use awsConfigOptions (variable of type []"github.com/aws/aws-sdk-go-v2/config".LoadOptionsFunc) as []func(*"github.com/aws/aws-sdk-go-v2/config".LoadOptions) error value in argument to awsConfig.LoadDefaultConfig

Reproduction Steps

// awsConfigOptions := []awsConfig.LoadOptionsFunc{} // this won't work properly as although the types are "the same" golang won't allow a runtime lambda-like function to match agaist a defined type signarture (afaik)
awsConfigOptions := []func(*awsConfig.LoadOptions) error{}

if config.Debug { // let's say config.Debug comes from the user's config
	awsConfigOptions = append(
		awsConfigOptions,
		awsConfig.WithClientLogMode(aws.LogRequestWithBody|aws.LogRequestEventMessage),
	)
}

cfg, err := awsConfig.LoadDefaultConfig(
	context.TODO(),
	awsConfigOptions...,
)

Possible Solution

Make it so the function LoadDefaultConfig accepts a variadic type of []awsConfig.LoadOptionsFunc{}.

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2 v1.18.1

Compiler and Version used

go1.20.1 darwin/arm64

Operating System and version

MacOS

@migueleliasweb migueleliasweb added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 17, 2023
@migueleliasweb
Copy link
Author

If that helps, the definition for the function:

func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error)

seems to be 3 days older (via git blame) than

type LoadOptionsFunc func(*LoadOptions) error.

Maybe this is just a matter of someone missing to come back and reuse the type defined at a later time.

@migueleliasweb
Copy link
Author

Also, note that this is not really a bug per se. The code can and will work normally. This is just a possible improvement.

@RanVaknin RanVaknin added feature-request A feature should be added or improved. needs-review This issue or pull request needs review from a core team member. p3 This is a minor priority issue and removed bug This issue is a bug. labels Jul 17, 2023
@RanVaknin RanVaknin self-assigned this Jul 17, 2023
davidjb added a commit to davidjb/aws-sdk-go-v2 that referenced this issue Jul 20, 2023
@davidjb
Copy link

davidjb commented Jul 20, 2023

I've opened a PR to address this at #2199.

@lucix-aws
Copy link
Contributor

Unfortunately the proposed change is an API break since these two types are not interchangeable.

Given the revised function definition:

func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error)

The reverse w.r.t. what is accepted by the call in question becomes true: []config.LoadOptionsFunc{} can be passed to config.LoadDefaultConfig, but []func(*config.LoadOptions) error cannot:

// this now works
awsConfigOptions := []awsConfig.LoadOptionsFunc{}
// this is now broken
// awsConfigOptions := []func(*awsConfig.LoadOptions) error{}

if config.Debug {
	awsConfigOptions = append(
		awsConfigOptions,
		awsConfig.WithClientLogMode(aws.LogRequestWithBody|aws.LogRequestEventMessage),
	)
}

cfg, err := awsConfig.LoadDefaultConfig(
	context.TODO(),
	awsConfigOptions...,
)

Closing since we're unable to make this change.

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@davidjb
Copy link

davidjb commented Jul 23, 2023

@lucix-aws Is this something that can be addressed in the next major version of the SDK?

@migueleliasweb
Copy link
Author

Would making a type alias work, @lucix-aws ?

@lucix-aws
Copy link
Contributor

Is this something that can be addressed in the next major version of the SDK?

@davidjb There won't be a new major semver version of the v2 SDK. The next effective "major version" would be a v3 of the Go SDK, which we currently have no plans to implement.

Would making a type alias work

@migueleliasweb Can you clarify/provide an example of what you're envisioning?

For the record -- I don't really see an issue with the API as it's defined now. Having to re-declare the signature may be slightly cumbersome, but you're still able to use the typedef and the explicit signature interchangeably when working with the actual slice. The example you've provided demonstrates this:

if config.Debug {
	awsConfigOptions = append(
		awsConfigOptions,
		// the return of `WithClientLogMode` is `LoadOptionsFunc`, but you can still pass it to
		// the append call even though the slice is declared as `[]func(*LoadOptions) error`
		awsConfig.WithClientLogMode(aws.LogRequestWithBody|aws.LogRequestEventMessage),
	)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. needs-review This issue or pull request needs review from a core team member. needs-triage This issue or PR still needs to be triaged. p3 This is a minor priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants