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

getOrElseGet() with provider as parameter for Property/Provider #10520

Closed
szpak opened this issue Sep 9, 2019 · 6 comments
Closed

getOrElseGet() with provider as parameter for Property/Provider #10520

szpak opened this issue Sep 9, 2019 · 6 comments
Labels
a:feature A new functionality

Comments

@szpak
Copy link
Contributor

szpak commented Sep 9, 2019

Expected Behavior

There are cases where it is handy to return another provider if the current one is empty.

class PitestPluginExtension {
    final SetProperty<String> targetClasses
    final SetProperty<String> targetTests
//get targetClasses or if not use targetClasses 
= extension.targetTests.getOrElseGet(extension.targetClasses) //method name should be enhanced

Current Behavior

Without that it is needed to use ifPresent():

= project.providers.provider {
    if (extension.targetTests.isPresent()) {
        return extension.targetTests.get()
    } else {
        return task.targetClasses.getOrNull()
    }

Similar approach is used in java.util.Optional

@szpak szpak added a:feature A new functionality from:contributor labels Sep 9, 2019
@big-guy
Copy link
Member

big-guy commented Sep 9, 2019

Do you have a little more detail on where/how = extension.targetTests.getOrElseGet(extension.targetClasses) is used?

@szpak
Copy link
Contributor Author

szpak commented Sep 10, 2019

Sure. If the targetTests parameter is not set by an user in the plugin configuration the value from targetClasses is taken - targetClasses is always set by the plugin logic and is a sensible default value. However, an user might want to narrow the scope of PIT execution to subset of tests. Sample usage in code and the original issue which brought me here.

@big-guy
Copy link
Member

big-guy commented Sep 10, 2019

OK, in that case, could you just do:

extension.targetTests.convention(extension.targetClasses)

If targetTests is set explicitly, then that value is used.
If targetTests is not set, by convention, you use targetClasses.

@adammurdoch
Copy link
Member

Have you looked at Provider.orElse(Provider), added in Gradle 5.6? It does what you're asking for in the description.

@szpak
Copy link
Contributor Author

szpak commented Sep 11, 2019

OK, in that case, could you just do:

extension.targetTests.convention(extension.targetClasses)

@big-guy I tried it before and with:

task.targetTests.convention(extension.targetClasses)
task.targetTests.set(extension.targetTests)

The second line overrides a default value even if extension.targetTests is a non-set provider. It's SetProperty<String> to maybe a default empty collection causes that? Is it an intended behavior?


Have you looked at Provider.orElse(Provider), added in Gradle 5.6? It does what you're asking for in the description.

@adammurdoch Yes, that's exactly what I needed, thanks. I've been looking for that, but maybe my IDE has an earlier Gradle version set, so I missed it.

@szpak szpak closed this as completed Sep 11, 2019
@szpak
Copy link
Contributor Author

szpak commented Sep 11, 2019

Yes, that's exactly what I needed, thanks. I've been looking for that, but maybe my IDE has an earlier Gradle version set, so I missed it.

Ok, I missed it as it is named orElse() and return Provider not getOrElse() (which has just plain argument accepted). But, in general it's what I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:feature A new functionality
Projects
None yet
Development

No branches or pull requests

3 participants