Giter Club home page Giter Club logo

either_dart's Introduction

either dart · build status

The library for error handling and railway oriented programming.

This library supports async "map" and async "then" hiding the boilerplate of working with asynchronous computations Future[Either].

Installation

Add to pubspec.yml:

dependencies:
  either_dart: ... // latest package version

Documentation

https://pub.dev/documentation/either_dart/latest/either/either-library.html

Pub dev

https://pub.dev/packages/either_dart

How to use it?

Sections:

Basic usage

Create two entities for example, you can use your own abstractions for your project.

enum AppError {
  NotFound,
  // some errors codes
}

class MyError {
  final AppError key;
  final String? message;

  const MyError({
    required this.key, 
    this.message,
  });
}

We can use Either as shown below:

Either<MyError, String> getCityNameByCode(int code) {
  const cities = <int, String>{
    /// some cities
  };

  if (cities.contains(code)) {
    return Right(cities[code]!);
  } else {
    return Left(
      key: AppError.NotFound, 
      message: '[getCityNameByCode] can`t convert code:$code to city name',
    );
  }
}

Too, you can use Either.cond and Either.condLazy for simple cases:

  return Either.condLazy(cities.contains(code), 
    () => cities[code]!, 
    () => MyError(
      key: AppError.NotFound, 
      message: '[getCityNameByCode] can`t convert code:$code to city name',
    ),
  );

Either has the following methods:

note:
L - current Left type
TL - new generic Left type
R - current Right type
TR - new generic Right type

name result description
isLeft bool Represents the left side of Either class which by convention is a "Failure".
isRight bool Represents the right side of Either class which by convention is a "Success"
left L Get Left value, may throw an exception when the value is Right. read-only
right R Get Right value, may throw an exception when the value is Left. read-only
either<TL, TR>(TL fnL(L left), TR fnR(R right)) Either<TL, TR> Transform values of Left and Right, equal of bimap in fp-libraries
fold<T>(T fnL(L left), T fnR(R right)) T Fold Left and Right into the value of one type
map<TR>(TR fnR(R right)) Either<L, TR> Transform value of Right
mapLeft<TL>(TL fnL(L left)) Either<TL, R> Transform value of Left
mapAsync<TR>(FutureOr<TR> fnR(R right)) Future<Either<L, TR>> Transform value of Right
mapLeftAsync<TL>(FutureOr<TL> fnL(L left)) Future<Either<TL, R>> Transform value of Left
swap() Either<R, L> Swap Left and Right
then<TR>(Either<L, TR> fnR(R right)) Either<L, TR> Transform value of Right when transformation may be finished with an error
thenLeft<TL>(Either<TL, R> fnL(L left)) Either<TL, R> Transform value of Left when transformation may be finished with an Right
thenAsync<TR>(FutureOr<Either<L, TR>> fnR(R right)) Future<Either<L, TR>> Transform value of Right when transformation may be finished with an error
thenLeftAsync<TL>(FutureOr<Either<TL, R>> fnL(L left)) Future<Either<TL, R>> Transform value of Left when transformation may be finished with an Right

Advanced usage

This library provides an FutureEither extension which is designed to handle asynchronous computation with ease.

You don't need to import or use new classes to use it - just use Future<Either<L, R>>

name result description
either<TL, TR>(TL fnL(L left), TR fnR(R right)) Future<Either<TL, TR>> Transform values of Left and Right
fold<T>(T fnL(L left), T fnR(R right)) Future<T> Fold Left and Right into the value of one type
mapRight<TR>(FutureOr<TR> fnR(R right)) Future<Either<L, TR>> Transform value of Right
mapLeft<TL>(FutureOr<TL> fnL(L left)) Future<Either<TL, R>> Transform value of Left
swap() Future<Either<R, L>> Swap Left and Right
thenRight<TR>(FutureOr<Either<L, TR>> fnR(R right)) Future<Either<L, TR>> Async transform value of Right when transformation may be finished with an error
thenLeft<TL>(FutureOr<Either<TL, R>> fnL(L left)) Future<Either<TL, R>> Async transform value of Left when transformation may be finished with an Right

Example:

/// --- helpers ---

import 'package:either_dart/either.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import 'dart:convert';

Future<Either<AppError, http.Response>> safe(Future<http.Response> request) async {
  try {
    return Right(await request);
  } catch (e) {
    return Left(MyError(
        key: AppError.BadRequest,
        message: "Request executing with errors:$e"));
  }
}

Either<AppError, http.Response> checkHttpStatus(http.Response response) {
  if (response.statusCode == 200)
    return Right(response);
  if (response.statusCode >= 500)
    return Left(MyError(
        key: AppError.ServerError,
        message: "Server error with http status ${response.statusCode}"));
  return Left(MyError(
      key: AppError.BadResponse,
      message: "Bad http status ${response.statusCode}"));
}

Future<Either<AppError, dynamic>> parseJson(http.Response response) async {
  try {
    
    return Right(await compute((body) {
      final json = JsonCodec();
      return json.decode(body)); 
    }, response.body);
  } catch (e) {
    return Left(MyError(
      key: AppError.JsonParsing, 
      message: 'failed on json parsing'));
  }
}

/// --- app code ---

//// network
Future<Either<AppError, Data>> getDataFromServer() {
  return 
    safe(http.get(Uri('some uri')))
      .thenRight(checkHttpStatus)
      .thenRight(parseJson) 
      .mapRight(Data.fromJson)
}

Case - Solution

  • How I can use the value of Either?

You can use right or left getters, but you should check what value is stored inside (isLeft or isRight)

Also, my favorite methods fold, either

  • fold - used when you need to transform two rails to one type
  • either - used for two situations: 1. when you need transform left and right. 2. when you need to use stored value without next usage (see example).

Example:

/// either method
showNotification(Either<MyError, String> value) {
  return value.either(
    (left) => showWarning(left.message ?? left.key.toString()),
    (right) => showInfo(right.toString()),
  );
  /// equal
  if (value.isLeft) {
    final left = value.left;
    showWarning(left.message ?? left.key.toString()
  } else {
    showInfo(value.right.toString())
  }
}
/// fold method
class MyWidget {
  final Either<MyError, List<String>> value;

  const MyWidget(this.value);

  Widget build(BuildContext context) {
    return Text(
      value.fold(
        (left) => left.message, 
        (right) => right.join(', ')),
    );
    /// or
    return value.fold(
      (left) => _buildSomeErrorWidget(context, left),
      (right) => _buildSomeRightWidget(context, right),
    );
  }
}

either_dart's People

Contributors

avdosev avatar jacobaraujo7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

either_dart's Issues

Better type for Future<T> fold<T>

Future<T> fold<T>(T Function(L left) fnL, T Function(R right) fnR) {
return this.then((either) => either.fold(fnL, fnR));
}

I am quite new to dart so I may be wrong here (that's why I am creating an issue rather then pull request) but shouldn't fnL and fnR return FutureOr<T> instead of T?

Future<T> fold<T>(FutureOr<T> Function(L left) fnL, FutureOr<T> Function(R right) fnR) {
  return this.then((either) => either.fold(fnL, fnR));
}

This is what this.then() actually expects and it would allow us to do something like this:

bool success = await getStatusAsync()
  .fold(
    (error) async {
      await saveErrorAsync(error);
      return false;
    },
    (status) async {
      await saveStatusAsync(status);
      return true;
    },
);

(it's just and example and my usecase is actually quite different but I also need to fold both left and right to different futures of the same type)

Cannot mock (mockito) functions which return Either

In 1.0.0 the Either class was sealed. This breaked the generated mocking code for unit tests.

Is there a need for this class to be sealed?

We are forced to remain on version 0.4.0 - please reconsider sealing.

Thank you

Sealed Classes

https://dart.dev/language/class-modifiers#sealed

Admittedly I haven't dug too deep here - but seems like a sealed annotation would be a nice addition for this lib.

Might need to be a separate package to maintain compatibility with Dart < 3.0.

Happy to take a stab at it at some point in the next week or two if you'd like!

About add data to list in left part while pagination

I'm using either_dart in getter-setter.
But I'm stuck while adding data to the list. I want to add data to the list using pagination So can you please guide me that how can i add data to the list?

Here is my code,

 Either<List<NotificationResponseData>, Exception> _notificationData = Right(SuccessResultException());
  Either<List<NotificationResponseData>, Exception> get notificationData => _notificationData;
  set notificationData(Either<List<NotificationResponseData>, Exception> value) {
    _notificationData = value;
    // _notificationData.fold((left) => value.left, (right) => value.right);
    notifyListeners();
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.