Skip to content

Commit

Permalink
feat: add ValueOrAbsent option
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLatee committed Feb 20, 2024
1 parent dee7eed commit d6dc397
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 20 deletions.
24 changes: 24 additions & 0 deletions codegen/gql_tristate_value/lib/src/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ 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.
Expand All @@ -18,25 +22,45 @@ sealed class Value<T extends Object> {
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;
}

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

Expand Down
103 changes: 83 additions & 20 deletions codegen/gql_tristate_value/test/value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,96 @@ import 'package:gql_tristate_value/gql_tristate_value.dart';
import 'package:test/test.dart';

void main() {
test("requireValue on absent throws", () {
final absent = const AbsentValue();
group(
'requireValue',
() {
test("on $AbsentValue throws", () {
final absent = const AbsentValue();

expect(() => absent.requireValue, throwsA(isA<StateError>()));
});
expect(() => absent.requireValue, throwsA(isA<StateError>()));
});

test("requireValue on present returns value", () {
final present = Value.present(42);
test("on $PresentValue with valuereturns value", () {
final present = Value.present(42);

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

test("requireValue on null returns null", () {
final nullValue = Value<String>.present(null);
test("on $PresentValue with null returns null", () {
final nullValue = Value<String>.present(null);

expect(nullValue.requireValue, isNull);
});
expect(nullValue.requireValue, isNull);
});

test("valueOrNull on absent returns null", () {
final absent = const AbsentValue();
test("on $ValueOrAbsent with value returns value", () {
final present = Value.valueOrAbsent(42);

expect(absent.valueOrNull, isNull);
});
expect(present.requireValue, equals(42));
});

test("valueOrNull on present returns value", () {
final present = Value.present(42);
test("on $ValueOrAbsent with null throws", () {
final nullValue = Value<String>.valueOrAbsent(null);

expect(present.valueOrNull, equals(42));
});
expect(() => nullValue.requireValue, throwsA(isA<StateError>()));
});
},
);

group(
'valueOrNull ',
() {
test("on $AbsentValue returns null", () {
final absent = const AbsentValue();

expect(absent.valueOrNull, isNull);
});

test("on $PresentValue with value returns value", () {
final present = Value.present(42);

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

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

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

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

expect(present.valueOrNull, isNull);
});
},
);

group(
'isPresent ',
() {
test("on $AbsentValue returns false", () {
final absent = const AbsentValue();

expect(absent.isPresent, false);
});

test("on $PresentValue with value returns true", () {
final present = Value.present(42);

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

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

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

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

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

0 comments on commit d6dc397

Please sign in to comment.