Skip to content

Commit 486b1e3

Browse files
committed
Add more documentation
1 parent 501af51 commit 486b1e3

8 files changed

Lines changed: 44 additions & 7 deletions

File tree

lib/src/pick_bool.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extension BoolPick on RequiredPick {
1212
}
1313

1414
extension NullableBoolPick on Pick {
15+
// This deprecation is used to promote the `.required()` in auto-completion.
16+
// Therefore it is not intended to be ever removed
1517
@Deprecated(
1618
'By default values are optional and can only be converted when a fallback is provided '
1719
'i.e. .asBoolOrNull() which falls back to `null`. '

lib/src/pick_datetime.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ extension DateTimePick on RequiredPick {
3030
}
3131

3232
extension NullableDateTimePick on Pick {
33+
// This deprecation is used to promote the `.required()` in auto-completion.
34+
// Therefore it is not intended to be ever removed
3335
@Deprecated(
3436
'By default values are optional and can only be converted when a fallback is provided '
3537
'i.e. .asDateTimeOrNull() which falls back to `null`. '

lib/src/pick_double.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ extension DoublePick on RequiredPick {
1818
}
1919

2020
extension NullableDoublePick on Pick {
21+
// This deprecation is used to promote the `.required()` in auto-completion.
22+
// Therefore it is not intended to be ever removed
2123
@Deprecated(
2224
'By default values are optional and can only be converted when a fallback is provided '
2325
'i.e. .asDoubleOrNull() which falls back to `null`. '

lib/src/pick_int.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import 'package:deep_pick/src/pick.dart';
22

33
extension IntPick on RequiredPick {
4+
/// Returns the picked [value] as [int]
5+
///
6+
/// {@template Pick.asInt}
7+
/// Parses the picked value as [int]. Other types are parsable as well
8+
/// - [String] is gets parsed via [int.tryParse]
9+
/// - [double] is gets converted to [int] via [num.toInt()]
10+
/// {@endtemplate}
411
int asInt() {
512
if (value is int) {
613
return value as int;
@@ -18,6 +25,8 @@ extension IntPick on RequiredPick {
1825
}
1926

2027
extension NullableIntPick on Pick {
28+
// This deprecation is used to promote the `.required()` in auto-completion.
29+
// Therefore it is not intended to be ever removed
2130
@Deprecated(
2231
'By default values are optional and can only be converted when a fallback is provided '
2332
'i.e. .asIntOrNUll() which falls back to `null`. '
@@ -31,6 +40,10 @@ extension NullableIntPick on Pick {
3140
return required().asInt();
3241
}
3342

43+
/// Returns the picked [value] as [int?] or returns `null` when the picked
44+
/// value is absent
45+
///
46+
/// {@macro Pick.asInt}
3447
int? asIntOrNull() {
3548
if (value == null) return null;
3649
try {

lib/src/pick_list.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extension ListPick on RequiredPick {
1919
}
2020

2121
extension NullableListPick on Pick {
22+
// This deprecation is used to promote the `.required()` in auto-completion.
23+
// Therefore it is not intended to be ever removed
2224
@Deprecated(
2325
'By default values are optional and can only be converted when a fallback is provided '
2426
'i.e. .asListOrNull() which falls back to `null`. '

lib/src/pick_map.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extension MapPick on RequiredPick {
1515
}
1616

1717
extension NullableMapPick on Pick {
18+
// This deprecation is used to promote the `.required()` in auto-completion.
19+
// Therefore it is not intended to be ever removed
1820
@Deprecated(
1921
'By default values are optional and can only be converted when a fallback is provided '
2022
'i.e. .asMapOrNull() which falls back to `null`. '

lib/src/pick_string.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import 'package:deep_pick/src/pick.dart';
22

33
extension StringPick on RequiredPick {
4+
/// Returns the picked [value] as String
5+
///
6+
/// {@template Pick.asString}
7+
/// Parses the picked value as String. If the value is not already a [String]
8+
/// its [Object.toString()] will be called. This means that this method works
9+
/// for [int], [double] and any other [Object] which isn't a collection of
10+
/// values such as a [List] or [Map]
11+
/// {@endtemplate}
412
String asString() {
513
if (value is String) {
614
return value as String;
@@ -17,19 +25,23 @@ extension StringPick on RequiredPick {
1725
}
1826

1927
extension NullableStringPick on Pick {
28+
// This deprecation is used to promote the `.required()` in auto-completion.
29+
// Therefore it is not intended to be ever removed
2030
@Deprecated(
21-
'By default values are optional and can only be converted when a fallback is provided '
22-
'i.e. .asStringOrNull() which falls back to `null`. '
23-
'Use .required().asString() in cases the value is mandatory. '
24-
"It will crash when the value couldn't be picked.")
31+
'Use .required().asString() or .asRequiredString() when you require the value to be non-null. '
32+
'Use .asStringOrNull() when you expect the value to be nullable')
2533
String asString() {
2634
if (value == null) {
2735
throw PickException(
28-
'value at location ${location()} is null and not an instance of String');
36+
'value at location ${location()} is null and not a String. '
37+
'Use asStringOrNull() when null is a valid value (String?)');
2938
}
3039
return required().asString();
3140
}
3241

42+
/// Returns the picked [value] as [String] or returns `null` when the picked value isn't available
43+
///
44+
/// {@macro Pick.asString}
3345
String? asStringOrNull() {
3446
if (value == null) return null;
3547
try {

test/src/required_pick_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ void main() {
3030
expect(picked('adam').asString(), 'adam');
3131
expect(picked(1).asString(), '1');
3232
expect(picked(2.0).asString(), '2.0');
33-
expect(() => nullPick().asString(),
34-
throwsA(pickException(containing: ['unknownKey', 'null', 'String'])));
33+
expect(
34+
() => nullPick().asString(),
35+
throwsA(pickException(
36+
containing: ['unknownKey', 'null', 'String', 'asStringOrNull'])));
3537
});
3638

3739
test("asString() doesn't transform Maps and Lists with toString", () {

0 commit comments

Comments
 (0)