-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from callius/maintenance/dart-3
Feature: Dart 3 & Raise DSL
- Loading branch information
Showing
42 changed files
with
483 additions
and
982 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
abstract class LengthRange { | ||
abstract interface class LengthRange { | ||
abstract final int minLength; | ||
abstract final int maxLength; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import 'package:dartz/dartz.dart'; | ||
|
||
abstract class Modelable<Failure, Model> { | ||
abstract interface class Modelable<Failure, Model> { | ||
/// Creates a model from this. | ||
Either<Failure, Model> toModel(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
/// DSL for computing an [Either] from the given effect [block]. Based on the | ||
/// arrow-kt implementation. | ||
Either<Error, A> either<Error, A>(A Function(Raise<Error> r) block) { | ||
return foldOrThrow(block, Left.new, Right.new); | ||
} | ||
|
||
B foldOrThrow<Error, A, B>( | ||
A Function(Raise<Error> r) block, | ||
B Function(Error error) recover, | ||
B Function(A value) transform, | ||
) { | ||
return fold(block, (it) => throw it, recover, transform); | ||
} | ||
|
||
B fold<Error, A, B>( | ||
A Function(Raise<Error> r) block, | ||
B Function(Exception throwable) onCatch, | ||
B Function(Error error) recover, | ||
B Function(A value) transform, | ||
) { | ||
final raise = _DefaultRaise<Error>(); | ||
try { | ||
final res = block(raise); | ||
raise.complete(); | ||
return transform(res); | ||
} on RaiseCancellationException<Error> catch (e) { | ||
raise.complete(); | ||
return recover(e.raised); | ||
} on Exception catch (e) { | ||
raise.complete(); | ||
return onCatch(e); | ||
} | ||
} | ||
|
||
/// DSL for computing an [Either] async from the given effect [block]. Based on | ||
/// the arrow-kt implementation. | ||
Future<Either<Error, A>> eitherAsync<Error, A>( | ||
Future<A> Function(Raise<Error> r) block, | ||
) { | ||
return foldOrThrowAsync(block, Left.new, Right.new); | ||
} | ||
|
||
Future<B> foldOrThrowAsync<Error, A, B>( | ||
Future<A> Function(Raise<Error> r) block, | ||
B Function(Error error) recover, | ||
B Function(A value) transform, | ||
) { | ||
return foldAsync(block, (it) => throw it, recover, transform); | ||
} | ||
|
||
Future<B> foldAsync<Error, A, B>( | ||
Future<A> Function(Raise<Error> r) block, | ||
B Function(Exception throwable) onCatch, | ||
B Function(Error error) recover, | ||
B Function(A value) transform, | ||
) async { | ||
final raise = _DefaultRaise<Error>(); | ||
try { | ||
final res = await block(raise); | ||
raise.complete(); | ||
return transform(res); | ||
} on RaiseCancellationException<Error> catch (e) { | ||
raise.complete(); | ||
return recover(e.raised); | ||
} on Exception catch (e) { | ||
raise.complete(); | ||
return onCatch(e); | ||
} | ||
} | ||
|
||
/// Implementation of arrow-kt raise dsl. | ||
abstract class Raise<Error> { | ||
Never raise(Error r); | ||
|
||
A bind<A, E extends Error>(Either<E, A> r) { | ||
return r.fold(raise, id); | ||
} | ||
} | ||
|
||
final class _DefaultRaise<Error> extends Raise<Error> { | ||
bool _isActive = true; | ||
|
||
bool complete() { | ||
final result = _isActive; | ||
_isActive = false; | ||
return result; | ||
} | ||
|
||
@override | ||
Never raise(Error r) { | ||
if (_isActive) { | ||
throw RaiseCancellationException(raised: r, raise: this); | ||
} else { | ||
throw RaiseLeakedError(); | ||
} | ||
} | ||
} | ||
|
||
final class RaiseCancellationException<Error> extends Equatable | ||
implements Exception { | ||
final Error raised; | ||
final Raise<Error> raise; | ||
|
||
const RaiseCancellationException({required this.raised, required this.raise}); | ||
|
||
@override | ||
List<Object?> get props => [raised, raise]; | ||
} | ||
|
||
final class RaiseLeakedError extends StateError { | ||
RaiseLeakedError() | ||
: super( | ||
'raise or bind was called outside of its DSL scope, and the DSL Scoped operator was leaked', | ||
); | ||
} | ||
|
||
extension RaiseEnsureExtension<Error> on Raise<Error> { | ||
void ensure(bool condition, Error Function() raise) { | ||
if (!condition) { | ||
this.raise(raise()); | ||
} | ||
} | ||
|
||
B ensureNotNull<B>(B? value, Error Function() raise) { | ||
return value ?? this.raise(raise()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
abstract class ValueFailure<T> { | ||
abstract interface class ValueFailure<T> { | ||
abstract final T failedValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
abstract class ValueObject<T> { | ||
abstract interface class ValueObject<T> { | ||
abstract final T value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
name: target | ||
description: Functional domain modeling in Dart. | ||
version: 0.1.2 | ||
version: 0.2.0 | ||
repository: https://github.com/callius/target-dart/tree/main/packages/target | ||
issue_tracker: https://github.com/callius/target-dart/issues | ||
homepage: https://github.com/callius/target-dart | ||
|
||
environment: | ||
sdk: '>=2.19.6 <3.0.0' | ||
sdk: '>=3.0.5 <4.0.0' | ||
|
||
dependencies: | ||
collection: ^1.17.0 | ||
collection: ^1.17.2 | ||
dartz: ^0.10.1 | ||
equatable: ^2.0.5 | ||
|
||
dev_dependencies: | ||
build_runner: null | ||
lints: ^2.0.1 | ||
mockito: ^5.4.0 | ||
lints: ^2.1.1 | ||
test: null |
Oops, something went wrong.