|
| 1 | +// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details. |
| 2 | + |
| 3 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +library has_int; |
| 7 | + |
| 8 | +import 'package:built_json/built_json.dart'; |
| 9 | +import 'package:built_value/built_value.dart'; |
| 10 | +import 'package:enum_class/enum_class.dart'; |
| 11 | + |
| 12 | +part 'has_int.g.dart'; |
| 13 | + |
| 14 | +/// Example "serializable" interface. |
| 15 | +/// |
| 16 | +/// In fact it is the implementations of the interface that must be |
| 17 | +/// serializable. See examples below. |
| 18 | +abstract class HasInt { |
| 19 | + int get anInt; |
| 20 | +} |
| 21 | + |
| 22 | +/// Example [HasInt] implementation that is not serializable. |
| 23 | +/// |
| 24 | +/// To be serializable it must use built_value or enum_class. |
| 25 | +abstract class WrongHasInt implements HasInt { |
| 26 | + int anInt = 4; |
| 27 | +} |
| 28 | + |
| 29 | +/// Example [HasInt] that is serializable because it uses built_value. |
| 30 | +abstract class ValueWithInt |
| 31 | + implements Built<ValueWithInt, ValueWithIntBuilder>, HasInt { |
| 32 | + /// Serializer field makes the built_value serializable. |
| 33 | + static final Serializer<ValueWithInt> serializer = _$valueWithIntSerializer; |
| 34 | + static final int youCanHaveStaticFields = 3; |
| 35 | + |
| 36 | + @override |
| 37 | + int get anInt; |
| 38 | + |
| 39 | + String get note; |
| 40 | + |
| 41 | + ValueWithInt._(); |
| 42 | + |
| 43 | + factory ValueWithInt([updates(ValueWithIntBuilder b)]) = _$ValueWithInt; |
| 44 | +} |
| 45 | + |
| 46 | +/// Builder class for [ValueWithInt]. |
| 47 | +abstract class ValueWithIntBuilder |
| 48 | + implements Builder<ValueWithInt, ValueWithIntBuilder> { |
| 49 | + int anInt; |
| 50 | + String note; |
| 51 | + |
| 52 | + ValueWithIntBuilder._(); |
| 53 | + |
| 54 | + factory ValueWithIntBuilder() = _$ValueWithIntBuilder; |
| 55 | +} |
| 56 | + |
| 57 | +/// Example [HasInt] that is serializable because it uses enum_class. |
| 58 | +class EnumWithInt extends EnumClass implements HasInt { |
| 59 | + /// Serializer field makes the enum_class serializable. |
| 60 | + static final Serializer<EnumWithInt> serializer = _$enumWithIntSerializer; |
| 61 | + |
| 62 | + static const EnumWithInt one = _$one; |
| 63 | + static const EnumWithInt two = _$two; |
| 64 | + static const EnumWithInt three = _$three; |
| 65 | + |
| 66 | + const EnumWithInt._(String name) : super(name); |
| 67 | + |
| 68 | + static BuiltSet<EnumWithInt> get values => _$values; |
| 69 | + |
| 70 | + static EnumWithInt valueOf(String name) => _$valueOf(name); |
| 71 | + |
| 72 | + @override |
| 73 | + int get anInt { |
| 74 | + switch (this) { |
| 75 | + case one: |
| 76 | + return 1; |
| 77 | + case two: |
| 78 | + return 2; |
| 79 | + case three: |
| 80 | + return 3; |
| 81 | + default: |
| 82 | + throw new StateError(this.toString()); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments