Skip to content

Commit

Permalink
Add blank behavior controls to PropertyResolver.optionalStringProvider (
Browse files Browse the repository at this point in the history
#1138)

This helps avoid subtle bugs with blank providers

<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/foundry/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
ZacSweers authored Dec 11, 2024
1 parent 34a3c3d commit 8dbfab6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
**Unreleased**
--------------

- Add blank behavior parameters to `PropertyResolver.optionalStringProvider` overloads to specify behavior for blank property values. Default behavior now is to error.

0.23.2
------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,26 @@ public final class foundry/gradle/properties/PropertyResolver {
public final fun intValue (Ljava/lang/String;I)I
public final fun intValue (Ljava/lang/String;Lorg/gradle/api/provider/Provider;)I
public static synthetic fun intValue$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;IILjava/lang/Object;)I
public final fun optionalStringProvider (Ljava/lang/String;)Lorg/gradle/api/provider/Provider;
public final fun optionalStringProvider (Ljava/lang/String;Ljava/lang/String;)Lorg/gradle/api/provider/Provider;
public static synthetic fun optionalStringProvider$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/gradle/api/provider/Provider;
public final fun optionalStringProvider (Ljava/lang/String;Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;)Lorg/gradle/api/provider/Provider;
public final fun optionalStringProvider (Ljava/lang/String;Ljava/lang/String;Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;)Lorg/gradle/api/provider/Provider;
public static synthetic fun optionalStringProvider$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;ILjava/lang/Object;)Lorg/gradle/api/provider/Provider;
public static synthetic fun optionalStringProvider$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;Ljava/lang/String;Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;ILjava/lang/Object;)Lorg/gradle/api/provider/Provider;
public final fun optionalStringValue (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun optionalStringValue$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
public final fun providerFor (Ljava/lang/String;)Lorg/gradle/api/provider/Provider;
public final fun stringValue (Ljava/lang/String;)Ljava/lang/String;
public final fun stringValue (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
}

public final class foundry/gradle/properties/PropertyResolver$BlankBehavior : java/lang/Enum {
public static final field ERROR Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;
public static final field FILTER Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;
public static final field NONE Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;
public static fun values ()[Lfoundry/gradle/properties/PropertyResolver$BlankBehavior;
}

public final class foundry/gradle/properties/PropertyResolver$Companion {
public final fun createForRootProject (Lorg/gradle/api/Project;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Lfoundry/gradle/properties/PropertyResolver;
public static synthetic fun createForRootProject$default (Lfoundry/gradle/properties/PropertyResolver$Companion;Lorg/gradle/api/Project;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lfoundry/gradle/properties/PropertyResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,40 @@ public class PropertyResolver(
return providerFor(key).orNull ?: defaultValue
}

public fun optionalStringProvider(key: String): Provider<String> {
return optionalStringProvider(key, null)
public fun optionalStringProvider(
key: String,
blankBehavior: BlankBehavior = BlankBehavior.ERROR,
): Provider<String> {
return optionalStringProvider(key, null, blankBehavior)
}

public fun optionalStringProvider(
key: String,
defaultValue: String? = null,
blankBehavior: BlankBehavior = BlankBehavior.ERROR,
): Provider<String> {
return providerFor(key)
.let { defaultValue?.let { providers.provider { defaultValue } } ?: it }
.filter {
when (blankBehavior) {
BlankBehavior.FILTER -> {
it.isNotBlank()
}
BlankBehavior.ERROR -> {
it.ifBlank { error("Unexpected blank value for property '$key'") }
true
}
BlankBehavior.NONE -> {
true
}
}
}
}

public fun optionalStringProvider(key: String, defaultValue: String? = null): Provider<String> {
return providerFor(key).let { defaultValue?.let { providers.provider { defaultValue } } ?: it }
public enum class BlankBehavior {
FILTER,
ERROR,
NONE,
}

public companion object {
Expand Down

0 comments on commit 8dbfab6

Please sign in to comment.