Giter Site home page Giter Site logo

dart-union-generator's Introduction

Union Generator

What inspired us?

We've been really happy using freezed package tool to create our union classes, which have given us the possibility of having a better solution than common enums, providing methods like map, when, maybeMap and maybeWhen that allowed us to avoid omissions when working with different possible enumerated values for an instance.

So, what's the problem? Well... Freezed package creates auto-generated code and this, of course, makes stuff to be impossible to control, while adds some extra dependencies that we wanted to avoid at all.

Now, we're going to explain how to use the extension, so you can have full control on your code while easily having all the advantajes that unions give us.

syntax

Usage

This extension works by creating a template class that will help us know what to generate next. The expected structure is as follows:

class YourClassName{
    const factory YourClassName.firstUseCase() = NameForFirstUseCaseClass;
    const factory YourClassName.secondUseCase(String arg1, int arg2, bool arg 3) = NameForSecondUseCaseClass;
    const factory YourClassName.thirdUseCase(String arg1) = NameForThirdUseCaseClass;
}

This example will generate a type YourClassName which can be of any of these subtypes:

NameForFirstUseCaseClass || NameForSecondUseCaseClass || NameForThirdUseCaseClass

Also, this will provide you the handy methods map, when, maybeMap and maybeWhen.

To activate generation you must place the cursor in the class declaration line, and press cntrl (cmd) + .

Why would I use Unions?

This is a perfect solution since you don't need to remember every single possible value that an instance can have, as the provided methods will handle this task themselves.

What to do if you can't generate the Unions

Here we'll describe some cases where you could have some troubles trying to use the extension.

First of all, as it's on a very early stage, some cases are not (yet) supported:

  • Dart version < 2.12.0. You'll need to upgrade to (at least) dart version 2.12.0 so you can use this extension.

Support

We highly encourage you to create a pull request for this extension if you want to improve it. Your help is always welcome. Also, you can open issues if there's any trouble.

dart-union-generator's People

Contributors

jennsenr avatar luiscarch11 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

abel1027

dart-union-generator's Issues

Add method syntax

description:

a shorter syntax is proposed, like:

class ExampleUnion {
  UnionCaseOne();
  UnionCaseTwo(String message);
}

instead of:

class ExampleUnion {
  factory ExampleUnion.unionCaseOne() = UnionCaseOne;
  factory ExampleUnion.unionCaseTwo(String message) = UnionCaseTwo;
}

motivation:

  • write less to improve productivity

implication:

  • current syntax support must be maintained.
  • the generated code must be the same with both syntax

Add maybe methods

currently exist map and when, maybe Map and maybeWhen should be added.

these methods must receive an orElse callback that is called in case the corresponding callback case is null

Syntax changes

Change factories syntax

from:

factory ServerErrorFailure.unknown() => ServerErrorFailureUnknown();
factory ServerErrorFailure.invalidArgument(String message, int code) =>
ServerErrorFailureInvalidArgument(message, code);

to:

factory ServerErrorFailure.unknown() = ServerErrorFailureUnknown;
factory ServerErrorFailure.invalidArgument(String message, int code) = ServerErrorFailureInvalidArgument;

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.