Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: init
- name: pub get
run: .tools/init
- name: format
run: .tools/format
Expand All @@ -41,7 +41,7 @@ jobs:
- name: coverage
run: .tools/coverage
- name: upload coverage to codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage/lcov.info
6 changes: 3 additions & 3 deletions .tools/coverage
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ rm coverage/lcov.info 2>NUL
echo NOTE sealed_annotations
pushd sealed_annotations || exit 1
dart test --coverage dart_coverage || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE sealed_writer
pushd sealed_writer || exit 1
dart test --coverage dart_coverage || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE sealed_generators
pushd sealed_generators || exit 1
dart test --coverage dart_coverage || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE combining coverage
Expand Down
6 changes: 3 additions & 3 deletions .tools/coverage.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ del coverage\lcov.info 2> NUL
echo NOTE sealed_annotations
pushd sealed_annotations || exit 1
call dart test --coverage dart_coverage || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE sealed_writer
pushd sealed_writer || exit 1
call dart test --coverage dart_coverage || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE sealed_generators
pushd sealed_generators || exit 1
call dart test --coverage dart_coverage || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages .packages --report-on lib || exit 1
call dart pub global run coverage:format_coverage -i dart_coverage -o coverage/lcov.info --lcov --packages=.dart_tool/package_config.json --report-on lib || exit 1
popd || exit 1

echo NOTE combining coverage
Expand Down
8 changes: 4 additions & 4 deletions .tools/fix
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ echo HEADER format

pushd sealed_annotations || exit 1
echo NOTE sealed_annotations
dart format --fix lib test
dart format lib test
popd || exit 1

pushd sealed_writer || exit 1
echo NOTE sealed_writer
dart format --fix lib test
dart format lib test
popd || exit 1

pushd sealed_generators || exit 1
echo NOTE sealed_generators
dart format --fix lib test
dart format lib test
popd || exit 1

pushd example || exit 1
echo NOTE example
dart format --fix test
dart format test
popd || exit 1
8 changes: 4 additions & 4 deletions .tools/fix.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ echo HEADER format

pushd sealed_annotations || exit 1
echo NOTE sealed_annotations
call dart format --fix lib test
call dart format lib test
popd || exit 1

pushd sealed_writer || exit 1
echo NOTE sealed_writer
call dart format --fix lib test
call dart format lib test
popd || exit 1

pushd sealed_generators || exit 1
echo NOTE sealed_generators
call dart format --fix lib test
call dart format lib test
popd || exit 1

pushd example || exit 1
echo NOTE example
call dart format --fix test
call dart format test
popd || exit 1
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.0.0

- TODO

## 1.14.0

- Fixed some issues

## 1.13.0

- Added referToManifest to source writer
Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MIT License

Copyright (c) 2025 Amirreza Madani
Copyright (c) 2021 6thsolution

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
76 changes: 54 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Dart Sealed Class Generator

[![build](https://img.shields.io/github/workflow/status/6thsolution/dart_sealed/Dart?label=build)](https://github.com/6thsolution/dart_sealed/actions/workflows/dart.yml)
[![build](https://img.shields.io/codecov/c/gh/6thsolution/dart_sealed?label=coverage)](https://codecov.io/gh/6thsolution/dart_sealed)
[![pub](https://img.shields.io/pub/v/sealed_annotations.svg?color=blue&label=sealed_annotations)](https://pub.dev/packages/sealed_annotations)
[![pub](https://img.shields.io/pub/v/sealed_generators.svg?color=blue&label=sealed_generators)](https://pub.dev/packages/sealed_generators)
[![pub](https://img.shields.io/pub/v/sealed_writer.svg?color=blue&label=sealed_writer)](https://pub.dev/packages/sealed_writer)
[![build](https://img.shields.io/github/actions/workflow/status/FatulM/dart_sealed/.github/workflows/dart.yml?branch=master&label=build)](https://github.com/FatulM/dart_sealed/actions/workflows/dart.yml)
[![build](https://img.shields.io/codecov/c/gh/FatulM/dart_sealed?label=coverage)](https://codecov.io/gh/FatulM/dart_sealed)
[![pub](https://img.shields.io/badge/sealed__annotations-v2.0.0-blue)](https://github.com/FatulM/dart_sealed/tree/master/sealed_annotations)
[![pub](https://img.shields.io/badge/sealed__generators-v2.0.0-blue)](https://github.com/FatulM/dart_sealed/tree/master/sealed_generators)
[![version](https://img.shields.io/badge/sealed__writer-v2.0.0-blue)](https://github.com/FatulM/dart_sealed/tree/master/sealed_writer)

Generate sealed class hierarchy for Dart and Flutter.

**NOTE** that this is only a clone of the upstream git repository
which I have fixed some issues and updated some dependencies.

## Features

* Generate sealed class with abstract super type and data sub-classes.
* Generate sealed class with abstract super type and data subclasses.
* Static factory methods. for example `Result.success(data: 0)`.
* Cast methods. for example `a.asSuccess`, `a.isSuccess` or `a.asSuccessOrNull`.
* Three types of equality and hashCode generation : data (like kotlin data classes), identity and distinct.
Expand All @@ -26,12 +29,41 @@ Generate sealed class hierarchy for Dart and Flutter.

Add dependencies in your `pubspec.yaml` file.

Since this is not the original repository, you can not use like this:

```yaml
# WON'T WORK LIKE THIS:
dependencies:
sealed_annotations: '2.0.0'

dev_dependencies:
sealed_generators: '2.0.0'
```

Instead, use like this:

This will override the three dependencies to be downloaded from git.

```yaml
dependencies:
sealed_annotations: ^latest.version
sealed_annotations: '2.0.0'

dev_dependencies:
sealed_generators: ^latest.version
sealed_generators: '2.0.0'

dependency_overrides:
sealed_annotations:
git:
url: https://github.com/FatulM/dart_sealed
path: sealed_annotations/
sealed_writer:
git:
url: https://github.com/FatulM/dart_sealed
path: sealed_writer/
sealed_generators:
git:
url: https://github.com/FatulM/dart_sealed
path: sealed_generators/
```

Import `sealed_annotations`.
Expand Down Expand Up @@ -163,14 +195,14 @@ class WeatherWindy extends Weather {

Notes:

- Prefer using factories in super class instead of sub-class constructors. like `Whether.rainy()` instead
- Prefer using factories in super class instead of subclass constructors. like `Whether.rainy()` instead
of `WhetherRainy()`
- Minimize usage of cast methods, most of the time they can be replaced with a match method.

## Equality and generated class names

You can choose between three types of equality using `@WithEquality(...)` annotation. Default equality is `data` if not
specified. This will become default equality for all sub-classes. You can change equality of each sub-class by using
specified. This will become default equality for all subclasses. You can change equality of each subclass by using
this annotation on individual methods.

Equality types:
Expand Down Expand Up @@ -209,14 +241,14 @@ abstract class _Weather {
```

An abstract super class is generated with name equal to name of manifest class without the underline (here `Weather`).
Each method will become a sub-class. There should be at least one method. sub-class names are based on method name
Each method will become a subclass. There should be at least one method. subclass names are based on method name
prefixed with super class name (for example `WeatherSunny`). Naming process can be tailored with use of `@WithPrefix`
and `@WithName` annotations. Each method argument will become a field in corresponding sub-class. Field names are equal
and `@WithName` annotations. Each method argument will become a field in corresponding subclass. Field names are equal
to argument names and field types are equal to argument types or dynamic if not specified. Argument types can be
overridden using `@WithType` annotation for example when type information is not available at build time. Note that you
can have nullable and non-nullable fields.

To change prefix of sub-class names which by default is top class name, you can use `@WithPrefix` annotation. for
To change prefix of subclass names which by default is top class name, you can use `@WithPrefix` annotation. for
example:

```dart
Expand All @@ -228,9 +260,9 @@ abstract class _Weather {
```

Now `sunny` will be named `HelloSunny` instead of the default `WeatherSunny`. You can use `@WithPrefix('')` to remove
all prefix from sub-class names.
all prefix from subclass names.

To change sub-class names directly you can use `@WithName` annotation. It will override `WithPrefix` if specified. for
To change subclass names directly you can use `@WithName` annotation. It will override `WithPrefix` if specified. for
example:

```dart
Expand All @@ -244,8 +276,8 @@ abstract class _Weather {
Now `sunny` will be named `Hello` instead of the default `WeatherSunny`. This is useful if you want not to use prefix
for some items.

Almost all methods on sealed classes use short names extracted from manifest method names. Full sub-class names are not
used. It is recommended not to use sub-classes directly. There are factory methods for each item on super class.
Almost all methods on sealed classes use short names extracted from manifest method names. Full subclass names are not
used. It is recommended not to use subclasses directly. There are factory methods for each item on super class.

## Generic Usage

Expand Down Expand Up @@ -371,8 +403,8 @@ abstract class _Basket {

Sometimes you need some fields to be present in all of your sealed classes. For example consider making a sealed class
for different types of errors, and all of them are required to have `code` and `message`. It is very annoying to add
code and message to all of sealeds manually. Also if you have an error object you are unable to get its code or message
without using cast or match methods. Here you can use common fields.
code and message to all of sealed classes manually. Also, if you have an error object you are unable to get its code or
message without using cast or match methods. Here you can use common fields.

To declare a common field you can add a getter or a final field to a manifest class, and it will automatically be added
to all of your sealed classes. for example:
Expand All @@ -397,9 +429,9 @@ abstract class _ApiError {

You can also use a constructor in pair with final fields equivalently.

common fields are available on `ApiError` objects as well as it's sub-classes.
common fields are available on `ApiError` objects as well as its subclasses.

If you specify common fields in your seaeld classes it has no effect. for example:
If you specify common fields in your sealed classes it has no effect. for example:

```dart
@Sealed()
Expand All @@ -413,7 +445,7 @@ abstract class _Common {
}
```

You can use sub-class of common field type in sealed classes. For example:
You can use subclass of common field type in sealed classes. For example:

```dart
@Sealed()
Expand Down
8 changes: 8 additions & 0 deletions example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.0.0

- TODO

## 1.14.0

- Fixed some issues

## 1.13.0

- Added referToManifest to source writer
Expand Down
1 change: 1 addition & 0 deletions example/LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MIT License

Copyright (c) 2025 Amirreza Madani
Copyright (c) 2021 6thsolution

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
Loading