|
| 1 | +// Copyright (c) 2024, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:built_collection/built_collection.dart'; |
| 6 | +import 'package:built_value/built_value.dart'; |
| 7 | +import 'package:built_value/json_object.dart'; |
| 8 | +import 'package:built_value/serializer.dart'; |
| 9 | + |
| 10 | +part 'built_value.g.dart'; |
| 11 | + |
| 12 | +abstract class SimpleValue implements Built<SimpleValue, SimpleValueBuilder> { |
| 13 | + static Serializer<SimpleValue> get serializer => _$simpleValueSerializer; |
| 14 | + |
| 15 | + int get anInt; |
| 16 | + |
| 17 | + String? get aString; |
| 18 | + |
| 19 | + factory SimpleValue([void Function(SimpleValueBuilder) updates]) = |
| 20 | + _$SimpleValue; |
| 21 | + SimpleValue._(); |
| 22 | +} |
| 23 | + |
| 24 | +abstract class VerySimpleValue |
| 25 | + implements Built<VerySimpleValue, VerySimpleValueBuilder> { |
| 26 | + static Serializer<VerySimpleValue> get serializer => |
| 27 | + _$verySimpleValueSerializer; |
| 28 | + |
| 29 | + int get value; |
| 30 | + |
| 31 | + factory VerySimpleValue(int value) => _$VerySimpleValue._(value: value); |
| 32 | + VerySimpleValue._(); |
| 33 | +} |
| 34 | + |
| 35 | +abstract class CompoundValue |
| 36 | + implements Built<CompoundValue, CompoundValueBuilder> { |
| 37 | + static Serializer<CompoundValue> get serializer => _$compoundValueSerializer; |
| 38 | + |
| 39 | + SimpleValue get simpleValue; |
| 40 | + ValidatedValue? get validatedValue; |
| 41 | + |
| 42 | + factory CompoundValue([void Function(CompoundValueBuilder) updates]) = |
| 43 | + _$CompoundValue; |
| 44 | + CompoundValue._(); |
| 45 | +} |
| 46 | + |
| 47 | +abstract class ValidatedValue |
| 48 | + implements Built<ValidatedValue, ValidatedValueBuilder> { |
| 49 | + static Serializer<ValidatedValue> get serializer => |
| 50 | + _$validatedValueSerializer; |
| 51 | + |
| 52 | + int get anInt; |
| 53 | + String? get aString; |
| 54 | + |
| 55 | + factory ValidatedValue([void Function(ValidatedValueBuilder) updates]) = |
| 56 | + _$ValidatedValue; |
| 57 | + |
| 58 | + ValidatedValue._() { |
| 59 | + if (anInt == 7) throw StateError('anInt may not be 7'); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +abstract class ValueWithCode |
| 64 | + implements Built<ValueWithCode, ValueWithCodeBuilder> { |
| 65 | + static final int youCanHaveStaticFields = 3; |
| 66 | + |
| 67 | + int get anInt; |
| 68 | + String? get aString; |
| 69 | + |
| 70 | + String get youCanWriteDerivedGetters => anInt.toString() + aString!; |
| 71 | + |
| 72 | + factory ValueWithCode([void Function(ValueWithCodeBuilder) updates]) = |
| 73 | + _$ValueWithCode; |
| 74 | + ValueWithCode._(); |
| 75 | + |
| 76 | + factory ValueWithCode.fromCustomFactory(int anInt) => ValueWithCode( |
| 77 | + (b) => |
| 78 | + b |
| 79 | + ..anInt = anInt |
| 80 | + ..aString = 'two', |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +abstract class ValueWithDefaults |
| 85 | + implements Built<ValueWithDefaults, ValueWithDefaultsBuilder> { |
| 86 | + int get anInt; |
| 87 | + String? get aString; |
| 88 | + |
| 89 | + factory ValueWithDefaults([void Function(ValueWithDefaultsBuilder) updates]) = |
| 90 | + _$ValueWithDefaults; |
| 91 | + ValueWithDefaults._(); |
| 92 | +} |
| 93 | + |
| 94 | +abstract class ValueWithDefaultsBuilder |
| 95 | + implements Builder<ValueWithDefaults, ValueWithDefaultsBuilder> { |
| 96 | + int anInt = 7; |
| 97 | + |
| 98 | + String? aString; |
| 99 | + |
| 100 | + factory ValueWithDefaultsBuilder() = _$ValueWithDefaultsBuilder; |
| 101 | + ValueWithDefaultsBuilder._(); |
| 102 | +} |
| 103 | + |
| 104 | +abstract class DerivedValue |
| 105 | + implements Built<DerivedValue, DerivedValueBuilder> { |
| 106 | + int get anInt; |
| 107 | + |
| 108 | + @memoized |
| 109 | + int get derivedValue => anInt + 10; |
| 110 | + |
| 111 | + @memoized |
| 112 | + Iterable<String> get derivedString => [toString()]; |
| 113 | + |
| 114 | + factory DerivedValue([void Function(DerivedValueBuilder) updates]) = |
| 115 | + _$DerivedValue; |
| 116 | + DerivedValue._(); |
| 117 | +} |
| 118 | + |
| 119 | +abstract class Account implements Built<Account, AccountBuilder> { |
| 120 | + static Serializer<Account> get serializer => _$accountSerializer; |
| 121 | + int get id; |
| 122 | + String get name; |
| 123 | + BuiltMap<String, JsonObject> get keyValues; |
| 124 | + |
| 125 | + factory Account([void Function(AccountBuilder) updates]) = _$Account; |
| 126 | + Account._(); |
| 127 | +} |
| 128 | + |
| 129 | +@BuiltValue(wireName: 'V') |
| 130 | +abstract class WireNameValue |
| 131 | + implements Built<WireNameValue, WireNameValueBuilder> { |
| 132 | + static Serializer<WireNameValue> get serializer => _$wireNameValueSerializer; |
| 133 | + |
| 134 | + @BuiltValueField(wireName: 'v') |
| 135 | + int get value; |
| 136 | + |
| 137 | + factory WireNameValue([void Function(WireNameValueBuilder) updates]) = |
| 138 | + _$WireNameValue; |
| 139 | + |
| 140 | + WireNameValue._(); |
| 141 | +} |
0 commit comments