| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Provides metadata about mixins to dart-protoc-plugin. | 5 /// Provides metadata about mixins to dart-protoc-plugin. |
| 6 /// (Experimental API; subject to change.) | 6 /// (Experimental API; subject to change.) |
| 7 library protobuf.mixins.meta; | 7 library protobuf.mixins.meta; |
| 8 | 8 |
| 9 /// Entry point called by dart-protoc-plugin. | 9 /// Entry point called by dart-protoc-plugin. |
| 10 PbMixin findMixin(String name) { | 10 PbMixin findMixin(String name) { |
| 11 for (var m in _exportedMixins) { | 11 for (var m in _exportedMixins) { |
| 12 if (m.name == name) { | 12 if (m.name == name) { |
| 13 return m; | 13 return m; |
| 14 } | 14 } |
| 15 } | 15 } |
| 16 return null; // not found | 16 return null; // not found |
| 17 } | 17 } |
| 18 | 18 |
| 19 /// PbMixin contains metadata needed by dart-protoc-plugin to apply a mixin. | 19 /// PbMixin contains metadata needed by dart-protoc-plugin to apply a mixin. |
| 20 /// | 20 /// |
| 21 /// The mixin can be applied to a message using options in dart_options.proto. | 21 /// The mixin can be applied to a message using options in dart_options.proto. |
| 22 /// Only one mixin can be applied to each message, but that mixin can depend | 22 /// Only one mixin can be applied to each message, but that mixin can depend |
| 23 /// on another mixin, recursively, similar to single inheritance. | 23 /// on another mixin, recursively, similar to single inheritance. |
| 24 class PbMixin { | 24 class PbMixin { |
| 25 | |
| 26 /// The name of the mixin class to import into the .pb.dart file. | 25 /// The name of the mixin class to import into the .pb.dart file. |
| 27 final String name; | 26 final String name; |
| 28 | 27 |
| 29 /// The file that the .pb.dart file should import the symbol from. | 28 /// The file that the .pb.dart file should import the symbol from. |
| 30 final String importFrom; | 29 final String importFrom; |
| 31 | 30 |
| 32 /// Another mixin to apply ahead of this one, or null for none. | 31 /// Another mixin to apply ahead of this one, or null for none. |
| 33 final PbMixin parent; | 32 final PbMixin parent; |
| 34 | 33 |
| 35 /// Names that shouldn't be used by properties in the generated child class. | 34 /// Names that shouldn't be used by properties in the generated child class. |
| 36 /// May be null if the mixin doesn't reserve any new names. | 35 /// May be null if the mixin doesn't reserve any new names. |
| 37 final List<String> reservedNames; | 36 final List<String> reservedNames; |
| 38 | 37 |
| 39 const PbMixin._raw(this.name, | 38 const PbMixin(this.name, {this.importFrom, this.parent, this.reservedNames}); |
| 40 {this.importFrom, this.parent, this.reservedNames}); | |
| 41 | 39 |
| 42 /// Returns the mixin and its ancestors, in the order they should be applied. | 40 /// Returns the mixin and its ancestors, in the order they should be applied. |
| 43 Iterable<PbMixin> findMixinsToApply() { | 41 Iterable<PbMixin> findMixinsToApply() { |
| 44 var result = [this]; | 42 var result = [this]; |
| 45 for (var p = parent; p != null; p = p.parent) { | 43 for (var p = parent; p != null; p = p.parent) { |
| 46 result.add(p); | 44 result.add(p); |
| 47 } | 45 } |
| 48 return result.reversed; | 46 return result.reversed; |
| 49 } | 47 } |
| 50 | 48 |
| 51 /// Returns all the reserved names, including from ancestor mixins. | 49 /// Returns all the reserved names, including from ancestor mixins. |
| 52 Iterable<String> findReservedNames() { | 50 Iterable<String> findReservedNames() { |
| 53 var names = new Set<String>(); | 51 var names = new Set<String>(); |
| 54 for (var m = this; m != null; m = m.parent) { | 52 for (var m = this; m != null; m = m.parent) { |
| 55 names.add(m.name); | 53 names.add(m.name); |
| 56 if (m.reservedNames != null) { | 54 if (m.reservedNames != null) { |
| 57 names.addAll(m.reservedNames); | 55 names.addAll(m.reservedNames); |
| 58 } | 56 } |
| 59 } | 57 } |
| 60 return names; | 58 return names; |
| 61 } | 59 } |
| 62 } | 60 } |
| 63 | 61 |
| 64 /// The mixins that findMixin() can return. | 62 /// The mixins that findMixin() can return. |
| 65 const _exportedMixins = const [_pbMapMixin, _pbEventMixin]; | 63 final _exportedMixins = [_pbMapMixin, _pbEventMixin]; |
| 66 | 64 |
| 67 const _pbMapMixin = const PbMixin._raw("PbMapMixin", | 65 const _pbMapMixin = const PbMixin("PbMapMixin", |
| 68 importFrom: "package:protobuf/src/protobuf/mixins/map_mixin.dart", | 66 importFrom: "package:protobuf/src/protobuf/mixins/map_mixin.dart", |
| 69 parent: _mapMixin); | 67 parent: _mapMixin); |
| 70 | 68 |
| 71 const _pbEventMixin = const PbMixin._raw("PbEventMixin", | 69 const _pbEventMixin = const PbMixin("PbEventMixin", |
| 72 importFrom: "package:protobuf/src/protobuf/mixins/event_mixin.dart", | 70 importFrom: "package:protobuf/src/protobuf/mixins/event_mixin.dart", |
| 73 reservedNames: const ["changes", "deliverChanges"]); | 71 reservedNames: const ["changes", "deliverChanges"]); |
| 74 | 72 |
| 75 const List<String> _reservedNamesForMap = const [ | 73 const List<String> _reservedNamesForMap = const [ |
| 76 '[]', | 74 '[]', |
| 77 '[]=', | 75 '[]=', |
| 78 'addAll', | 76 'addAll', |
| 79 'containsKey', | 77 'containsKey', |
| 80 'containsValue', | 78 'containsValue', |
| 81 'forEach', | 79 'forEach', |
| 82 'putIfAbsent', | 80 'putIfAbsent', |
| 83 'remove', | 81 'remove', |
| 84 'isEmpty', | 82 'isEmpty', |
| 85 'isNotEmpty', | 83 'isNotEmpty', |
| 86 'keys', | 84 'keys', |
| 87 'length', | 85 'length', |
| 88 'values', | 86 'values', |
| 89 ]; | 87 ]; |
| 90 | 88 |
| 91 const _mapMixin = const PbMixin._raw("MapMixin", | 89 const _mapMixin = const PbMixin("MapMixin", |
| 92 importFrom: "dart:collection", | 90 importFrom: "dart:collection", reservedNames: _reservedNamesForMap); |
| 93 reservedNames: _reservedNamesForMap); | |
| OLD | NEW |