diff --git a/codegen/gql_tristate_value/lib/src/value.dart b/codegen/gql_tristate_value/lib/src/value.dart index 2e4bcdd8..3e3ffde5 100644 --- a/codegen/gql_tristate_value/lib/src/value.dart +++ b/codegen/gql_tristate_value/lib/src/value.dart @@ -9,21 +9,24 @@ sealed class Value { /// 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; + /// 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. @@ -31,13 +34,11 @@ sealed class Value { 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, }; } @@ -45,25 +46,6 @@ class AbsentValue extends Value { const AbsentValue() : super._(); } -class ValueOrAbsent extends Value { - final T? value; - - const ValueOrAbsent(this.value) : super._(); - - @override - bool operator ==(Object other) => - identical(this, other) || - other is ValueOrAbsent && - runtimeType == other.runtimeType && - value == other.value; - - @override - int get hashCode => value.hashCode; - - @override - String toString() => '$runtimeType(value: $value)'; -} - class PresentValue extends Value { final T? value; diff --git a/codegen/gql_tristate_value/test/value_test.dart b/codegen/gql_tristate_value/test/value_test.dart index 8fff424e..5a12777f 100644 --- a/codegen/gql_tristate_value/test/value_test.dart +++ b/codegen/gql_tristate_value/test/value_test.dart @@ -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.valueOrAbsent(null); + test('on ${Value.ofNullable} with null throws', () { + final nullValue = Value.ofNullable(null); expect(() => nullValue.requireValue, throwsA(isA())); }); @@ -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.valueOrAbsent(null); + test('on ${Value.ofNullable} with null returns null', () { + final present = Value.ofNullable(null); expect(present.valueOrNull, isNull); }); @@ -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.valueOrAbsent(null); + test('on ${Value.ofNullable} with null returns false', () { + final present = Value.ofNullable(null); expect(present.isPresent, false); }); @@ -103,18 +103,6 @@ void main() { expect(present.toString(), 'PresentValue(value: 42)'); }); - - test('on $ValueOrAbsent', () { - final present = Value.valueOrAbsent(42); - - expect(present.toString(), 'ValueOrAbsent(value: 42)'); - }); - - test('on $ValueOrAbsent', () { - final present = Value.valueOrAbsent(null); - - expect(present.toString(), 'ValueOrAbsent(value: null)'); - }); }, ); }