Skip to content

Commit

Permalink
chore: change ValueOrAbsent to factory constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLatee committed Feb 20, 2024
1 parent a08b382 commit 9f32323
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 51 deletions.
36 changes: 9 additions & 27 deletions codegen/gql_tristate_value/lib/src/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,43 @@ sealed class Value<T extends Object> {
/// The value is absent. It will not be serialized.
const factory Value.absent() = AbsentValue;

/// If the value is non-null, it will be serialized as the value.
/// The value is absent. It will not be serialized.
const factory Value.valueOrAbsent(T? value) = ValueOrAbsent;

/// The value is present. It may still be be null.
/// If the value is null, it will be serialized as null.
/// If the value is non-null, it will be serialized as the value.
const factory Value.present(T? value) = PresentValue<T>;

/// If the value is non-null, it will be serialized as the value.
/// If the value is null. It will not be serialized.
factory Value.ofNullable(T? value) {
if (value == null) {
return Value.absent();
}
return Value.present(value);
}

/// Returns the value if present (no matter if null or non-null), otherwise throws a [StateError].
T? get requireValue => switch (this) {
PresentValue(:final value) => value,
AbsentValue() => throw StateError("Value is absent"),
ValueOrAbsent(:final value) =>
value ?? (throw StateError("Value is absent")),
};

/// return the value if present and non-null, otherwise null.
/// this is useful for cases where you don't care between the value being absent or null.
T? get valueOrNull => switch (this) {
PresentValue(:final value) => value,
AbsentValue() => null,
ValueOrAbsent(:final value) => value,
};

bool get isPresent => switch (this) {
PresentValue() => true,
AbsentValue() => false,
ValueOrAbsent(:final value) => value == null ? false : true,
};
}

class AbsentValue extends Value<Never> {
const AbsentValue() : super._();
}

class ValueOrAbsent<T extends Object> extends Value<T> {
final T? value;

const ValueOrAbsent(this.value) : super._();

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ValueOrAbsent<T> &&
runtimeType == other.runtimeType &&
value == other.value;

@override
int get hashCode => value.hashCode;

@override
String toString() => '$runtimeType(value: $value)';
}

class PresentValue<T extends Object> extends Value<T> {
final T? value;

Expand Down
36 changes: 12 additions & 24 deletions codegen/gql_tristate_value/test/value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ void main() {
expect(nullValue.requireValue, isNull);
});

test('on $ValueOrAbsent with value returns value', () {
final present = Value.valueOrAbsent(42);
test('on ${Value.ofNullable} with value returns value', () {
final present = Value.ofNullable(42);

expect(present.requireValue, equals(42));
});

test('on $ValueOrAbsent with null throws', () {
final nullValue = Value<String>.valueOrAbsent(null);
test('on ${Value.ofNullable} with null throws', () {
final nullValue = Value<String>.ofNullable(null);

expect(() => nullValue.requireValue, throwsA(isA<StateError>()));
});
Expand All @@ -52,14 +52,14 @@ void main() {
expect(present.valueOrNull, equals(42));
});

test('on $ValueOrAbsent with value returns value', () {
final present = Value.valueOrAbsent(42);
test('on ${Value.ofNullable} with value returns value', () {
final present = Value.ofNullable(42);

expect(present.valueOrNull, equals(42));
});

test('on $ValueOrAbsent with null returns null', () {
final present = Value<String>.valueOrAbsent(null);
test('on ${Value.ofNullable} with null returns null', () {
final present = Value<String>.ofNullable(null);

expect(present.valueOrNull, isNull);
});
Expand All @@ -81,14 +81,14 @@ void main() {
expect(present.isPresent, true);
});

test('on $ValueOrAbsent with value returns true', () {
final present = Value.valueOrAbsent(42);
test('on ${Value.ofNullable} with value returns true', () {
final present = Value.ofNullable(42);

expect(present.isPresent, true);
});

test('on $ValueOrAbsent with null returns false', () {
final present = Value<String>.valueOrAbsent(null);
test('on ${Value.ofNullable} with null returns false', () {
final present = Value<String>.ofNullable(null);

expect(present.isPresent, false);
});
Expand All @@ -103,18 +103,6 @@ void main() {

expect(present.toString(), 'PresentValue<int>(value: 42)');
});

test('on $ValueOrAbsent', () {
final present = Value.valueOrAbsent(42);

expect(present.toString(), 'ValueOrAbsent<int>(value: 42)');
});

test('on $ValueOrAbsent', () {
final present = Value<String>.valueOrAbsent(null);

expect(present.toString(), 'ValueOrAbsent<String>(value: null)');
});
},
);
}

0 comments on commit 9f32323

Please sign in to comment.