diff --git a/README.md b/README.md index 8ec89fd..391139d 100644 --- a/README.md +++ b/README.md @@ -35,24 +35,6 @@ public void createUser(NonBlankString name, PositiveInt age, NonEmptyList badRequest(error.message()), - valid -> ok(register(valid)) -); -``` - ## Installation > *"I can just implement this myself. Why add a dependency?"* @@ -83,6 +65,24 @@ dependencies { ``` +## Quick Start + +```java +// Trusted data — just unwrap +PositiveInt confirmedAge = PositiveInt.unsafeOf(18); + +// Untrusted input — safe fallback +PositiveInt safeAge = PositiveInt.ofOrElse(userInput, 1); +``` + +```java +// Branch on success/failure +EmailString.of(email).fold( + error -> badRequest(error.message()), + valid -> ok(register(valid)) +); +``` + ## Kotlin Support Optional module with Kotlin-idiomatic extensions: @@ -90,46 +90,23 @@ Optional module with Kotlin-idiomatic extensions: ```kotlin import io.github.junggikim.refined.kotlin.* -// Scalar extensions val name = "Ada".toNonBlankStringOrThrow() val tags = listOf("java", "fp").toNonEmptyListOrThrow() - -// Validation extensions -val age = PositiveInt.ofOrElse(input, 1) -val age = PositiveInt.of(input).getOrNull() ?: PositiveInt.unsafeOf(1) -val result: Result = PositiveInt.of(input).toResult() -``` - -## Usage Patterns - -```java -// validate and get — most common -PositiveInt age = PositiveInt.ofOrElse(input, 1); ``` -```java -// to Optional -Optional maybeAge = PositiveInt.of(input).toOptional(); -``` - -```java -// transform error type -Validation mapped = PositiveInt.of(input) - .mapError(Violation::message); +```kotlin +// Safe fallback +val safeAge = PositiveInt.ofOrElse(input, 1) ``` -```java -// recover from error -Validation recovered = PositiveInt.of(input) - .recover(err -> PositiveInt.unsafeOf(1)); +```kotlin +// Kotlin-idiomatic nullable +val age = PositiveInt.of(input).getOrNull() ?: PositiveInt.unsafeOf(1) ``` -```java -// error-accumulating (multiple fields at once) -Validated left = Validated.invalid(Arrays.asList("age")); -Validated right = Validated.invalid(Arrays.asList("name")); -List errors = left.zip(right, Integer::sum).getErrors(); -// errors = ["age", "name"] +```kotlin +// Convert to kotlin.Result +val result: Result = PositiveInt.of(input).toResult() ``` ## API @@ -143,8 +120,6 @@ All refined wrappers follow the same pattern: Collection refined types implement JDK interfaces directly — `NonEmptyList` is a `List`, `NonEmptyMap` is a `Map`. No unwrapping needed. -`Violation` carries a stable `code`, human-readable `message`, and immutable `metadata` map. - ## Supported Types | Category | Examples | Count | @@ -158,9 +133,15 @@ Collection refined types implement JDK interfaces directly — `NonEmptyList` Full list: [docs/type-matrix.md](docs/type-matrix.md) +## Docs + +- [Type Matrix](docs/type-matrix.md) +- [Compatibility](docs/compatibility.md) +- [Kotlin Support Policy](docs/kotlin-support-policy.md) + ## Contributing -[CONTRIBUTING.md](CONTRIBUTING.md) · [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) · [SECURITY.md](SECURITY.md) · [docs/compatibility.md](docs/compatibility.md) · [docs/kotlin-support-policy.md](docs/kotlin-support-policy.md) +[CONTRIBUTING.md](CONTRIBUTING.md) · [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) · [SECURITY.md](SECURITY.md) ## License