Skip to content

Commit 54782b1

Browse files
committed
Merge branch 'master' into pub
2 parents 44bf447 + 45c386f commit 54782b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5282
-112
lines changed

.tools/cpex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@ cp sealed_example/bin/sealed/nullsafe/data/simple/weather.dart sealed_annotation
66
cp sealed_example/bin/sealed/nullsafe/data/simple/weather.sealed.dart sealed_annotations/example/
77
cp sealed_example/bin/sealed/nullsafe/data/generic/result.dart sealed_annotations/example/
88
cp sealed_example/bin/sealed/nullsafe/data/generic/result.sealed.dart sealed_annotations/example/
9-
cp sealed_example/bin/sealed/nullsafe/data/generic/mixed.dart sealed_annotations/example/
10-
cp sealed_example/bin/sealed/nullsafe/data/generic/mixed.sealed.dart sealed_annotations/example/
11-
cp sealed_example/bin/sealed/nullsafe/data/generic/meta.dart sealed_annotations/example/
12-
cp sealed_example/bin/sealed/nullsafe/data/generic/meta.sealed.dart sealed_annotations/example/
9+
cp sealed_example/bin/sealed/nullsafe/data/simple/mixed.dart sealed_annotations/example/
10+
cp sealed_example/bin/sealed/nullsafe/data/simple/mixed.sealed.dart sealed_annotations/example/
11+
cp sealed_example/bin/sealed/nullsafe/data/simple/meta.dart sealed_annotations/example/
12+
cp sealed_example/bin/sealed/nullsafe/data/simple/meta.sealed.dart sealed_annotations/example/
13+
cp sealed_example/bin/sealed/nullsafe/data/simple/common.dart sealed_annotations/example/
14+
cp sealed_example/bin/sealed/nullsafe/data/simple/common.sealed.dart sealed_annotations/example/
1315
cp sealed_example/bin/sealed/nullsafe/data/generic/result_complex.dart sealed_annotations/example/
1416
cp sealed_example/bin/sealed/nullsafe/data/generic/result_complex.sealed.dart sealed_annotations/example/
1517

1618
cp sealed_example/bin/sealed/nullsafe/data/simple/weather.dart sealed_generators/example/
1719
cp sealed_example/bin/sealed/nullsafe/data/simple/weather.sealed.dart sealed_generators/example/
1820
cp sealed_example/bin/sealed/nullsafe/data/generic/result.dart sealed_generators/example/
1921
cp sealed_example/bin/sealed/nullsafe/data/generic/result.sealed.dart sealed_generators/example/
20-
cp sealed_example/bin/sealed/nullsafe/data/generic/mixed.dart sealed_generators/example/
21-
cp sealed_example/bin/sealed/nullsafe/data/generic/mixed.sealed.dart sealed_generators/example/
22-
cp sealed_example/bin/sealed/nullsafe/data/generic/meta.dart sealed_generators/example/
23-
cp sealed_example/bin/sealed/nullsafe/data/generic/meta.sealed.dart sealed_generators/example/
22+
cp sealed_example/bin/sealed/nullsafe/data/simple/mixed.dart sealed_generators/example/
23+
cp sealed_example/bin/sealed/nullsafe/data/simple/mixed.sealed.dart sealed_generators/example/
24+
cp sealed_example/bin/sealed/nullsafe/data/simple/meta.dart sealed_generators/example/
25+
cp sealed_example/bin/sealed/nullsafe/data/simple/meta.sealed.dart sealed_generators/example/
26+
cp sealed_example/bin/sealed/nullsafe/data/simple/common.dart sealed_generators/example/
27+
cp sealed_example/bin/sealed/nullsafe/data/simple/common.sealed.dart sealed_generators/example/
2428
cp sealed_example/bin/sealed/nullsafe/data/generic/result_complex.dart sealed_generators/example/
2529
cp sealed_example/bin/sealed/nullsafe/data/generic/result_complex.sealed.dart sealed_generators/example/
2630

.tools/cpex.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ copy sealed_example\bin\sealed\nullsafe\data\simple\mixed.dart sealed_annotation
1010
copy sealed_example\bin\sealed\nullsafe\data\simple\mixed.sealed.dart sealed_annotations\example\
1111
copy sealed_example\bin\sealed\nullsafe\data\simple\meta.dart sealed_annotations\example\
1212
copy sealed_example\bin\sealed\nullsafe\data\simple\meta.sealed.dart sealed_annotations\example\
13+
copy sealed_example\bin\sealed\nullsafe\data\simple\common.dart sealed_annotations\example\
14+
copy sealed_example\bin\sealed\nullsafe\data\simple\common.sealed.dart sealed_annotations\example\
1315
copy sealed_example\bin\sealed\nullsafe\data\generic\result_complex.dart sealed_annotations\example\
1416
copy sealed_example\bin\sealed\nullsafe\data\generic\result_complex.sealed.dart sealed_annotations\example\
1517

@@ -21,6 +23,8 @@ copy sealed_example\bin\sealed\nullsafe\data\simple\mixed.dart sealed_generators
2123
copy sealed_example\bin\sealed\nullsafe\data\simple\mixed.sealed.dart sealed_generators\example\
2224
copy sealed_example\bin\sealed\nullsafe\data\simple\meta.dart sealed_generators\example\
2325
copy sealed_example\bin\sealed\nullsafe\data\simple\meta.sealed.dart sealed_generators\example\
26+
copy sealed_example\bin\sealed\nullsafe\data\simple\common.dart sealed_generators\example\
27+
copy sealed_example\bin\sealed\nullsafe\data\simple\common.sealed.dart sealed_generators\example\
2428
copy sealed_example\bin\sealed\nullsafe\data\generic\result_complex.dart sealed_generators\example\
2529
copy sealed_example\bin\sealed\nullsafe\data\generic\result_complex.sealed.dart sealed_generators\example\
2630

CHANGELOG.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
## 1.10.0
2+
3+
- Fixed @WithPrefix allowing empty string as argument (#6)
4+
- Added capability of adding common fields (#8)
5+
- Updated docs according to ignore/exclude generated files (#5)
6+
17
## 1.9.0
28

3-
- Changed not using `required` on nullable fields and arguments
9+
- Changed not using `required` on nullable fields and arguments (#4)
410

511
## 1.8.0
612

@@ -10,31 +16,31 @@
1016

1117
- Removed @immutable, @factory and @sealed generation
1218
- Removed meta export from sealed_annotations
13-
- Added support for functional types as field types
19+
- Added support for functional types as field types (#3)
1420

1521
## 1.6.0
1622

17-
- Changed cast methods to getters
23+
- Changed cast methods to getters (#1)
1824
- Downgrade meta to 1.3.0
1925

2026
## 1.5.0
2127

2228
- Removed support for super_enum API
2329
- Removed support for non null-safe projects
2430
- Removed whenOrThrow, whenOrNull and whenOrDefault
25-
- Removed wrapping functionality
26-
- Rename whenOrElse to maybeWhen
27-
- Rename whenPartial to partialWhen
31+
- Removed wrapping functionality (#1)
32+
- Rename whenOrElse to maybeWhen (#1)
33+
- Rename whenPartial to partialWhen (#1)
2834
- Refactor partialWhen implementation
29-
- Added map, maybeMap and partialMap methods
35+
- Added map, maybeMap and partialMap methods (#1)
3036

3137
## 1.4.0
3238

3339
- Fix version problems
3440

3541
## 1.3.0
3642

37-
- Using const factories
43+
- Using const factories (#1)
3844
- Updated analyzer to 2.0.0
3945

4046
## 1.2.0

README.md

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ abstract class _Weather {
5959
}
6060
```
6161

62-
Then run the following command to generate code for you.
62+
Then run the following command to generate code for you. If you are developer for flutter:
63+
64+
```bash
65+
flutter pub run build_runner build
66+
```
67+
68+
And if you are developing for pure dart:
6369

6470
```bash
6571
dart run build_runner build
@@ -157,7 +163,8 @@ class WeatherWindy extends Weather {
157163

158164
Notes:
159165

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

163170
## Equality and generated class names
@@ -313,3 +320,111 @@ abstract class _WeatherInfo {
313320
void nullable(@WithType('Result<WeatherData>?') result);
314321
}
315322
```
323+
324+
## Common Fields
325+
326+
Sometimes you need some fields to be present in all of your sealed classes. For example consider making a sealed class
327+
for different types of errors, and all of them are required to have `code` and `message`. It is very annoying to add
328+
code and message to all of sealeds manually. Also if you have an error object you are unable to get its code or message
329+
without using cast or match methods. Here you can use common fields.
330+
331+
To declare a common field you can add a getter or a final field to a manifest class, and it will automatically be added
332+
to all of your sealed classes. for example:
333+
334+
```dart
335+
@Sealed()
336+
abstract class _ApiError {
337+
// using getter
338+
String get message;
339+
340+
// using final field
341+
final String? code = null;
342+
343+
// code and message will be added to this automatically
344+
void internetError();
345+
346+
void badRequest();
347+
348+
void internalError(Object? error);
349+
}
350+
```
351+
352+
common fields are available on `ApiError` objects as well as it's sub-classes.
353+
354+
If you specify common fields in your seaeld classes it has no effect. for example:
355+
356+
```dart
357+
@Sealed()
358+
abstract class _Common {
359+
Object get x;
360+
361+
// one and two will have identical signatures
362+
void one(Object x);
363+
364+
void two();
365+
}
366+
```
367+
368+
You can use sub-class of common field type in sealed classes. For example:
369+
370+
```dart
371+
@Sealed()
372+
abstract class _Common {
373+
Object get x;
374+
375+
// x has type int
376+
void one(int x);
377+
378+
// x has type String
379+
void one(String x);
380+
381+
// x has type Object
382+
void three();
383+
}
384+
```
385+
386+
common fields also works with other constructs of dart_sealed like generics and @WithType. for example:
387+
388+
```dart
389+
@Sealed()
390+
abstract class _Common {
391+
@WithType('num')
392+
dynamic get x; // you can omit dynamic
393+
394+
// x has type int
395+
void one(@WithType('int') dynamic x); // you can omit dynamic
396+
397+
// x has type num
398+
void two();
399+
}
400+
```
401+
402+
and, for example:
403+
404+
```dart
405+
@Sealed()
406+
abstract class _Result<D extends num> {
407+
Object? get value;
408+
409+
void success(D value);
410+
411+
void error();
412+
}
413+
```
414+
415+
## Ignoring Generated Files
416+
417+
It is recommended to ignore generated files on Git. Add this to your `.gitignore` file:
418+
419+
```
420+
*.sealed.dart
421+
```
422+
423+
It is NOT recommended to exclude generated files from analysis. But if you decide to do so, add this to
424+
your `analysis_options.yaml` file:
425+
426+
```
427+
analyzer:
428+
exclude:
429+
- **.sealed.dart
430+
```

sealed_annotations/CHANGELOG.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
## 1.10.0
2+
3+
- Fixed @WithPrefix allowing empty string as argument (#6)
4+
- Added capability of adding common fields (#8)
5+
- Updated docs according to ignore/exclude generated files (#5)
6+
17
## 1.9.0
28

3-
- Changed not using `required` on nullable fields and arguments
9+
- Changed not using `required` on nullable fields and arguments (#4)
410

511
## 1.8.0
612

@@ -10,31 +16,31 @@
1016

1117
- Removed @immutable, @factory and @sealed generation
1218
- Removed meta export from sealed_annotations
13-
- Added support for functional types as field types
19+
- Added support for functional types as field types (#3)
1420

1521
## 1.6.0
1622

17-
- Changed cast methods to getters
23+
- Changed cast methods to getters (#1)
1824
- Downgrade meta to 1.3.0
1925

2026
## 1.5.0
2127

2228
- Removed support for super_enum API
2329
- Removed support for non null-safe projects
2430
- Removed whenOrThrow, whenOrNull and whenOrDefault
25-
- Removed wrapping functionality
26-
- Rename whenOrElse to maybeWhen
27-
- Rename whenPartial to partialWhen
31+
- Removed wrapping functionality (#1)
32+
- Rename whenOrElse to maybeWhen (#1)
33+
- Rename whenPartial to partialWhen (#1)
2834
- Refactor partialWhen implementation
29-
- Added map, maybeMap and partialMap methods
35+
- Added map, maybeMap and partialMap methods (#1)
3036

3137
## 1.4.0
3238

3339
- Fix version problems
3440

3541
## 1.3.0
3642

37-
- Using const factories
43+
- Using const factories (#1)
3844
- Updated analyzer to 2.0.0
3945

4046
## 1.2.0

0 commit comments

Comments
 (0)