forked from google/protobuf.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReadonlyMessageMixin and the hooks needed to support it.
The protoc plugin will use this mixin to implement a getDefault() method for each message. BUG=google#41 Review URL: https://chromiumcodereview.appspot.com//1228213004.
- Loading branch information
Brian Slesinsky
committed
Jul 15, 2015
1 parent
16eb0d6
commit aa21cf7
Showing
6 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of protobuf; | ||
|
||
/// Modifies a GeneratedMessage so that it's read-only. | ||
abstract class ReadonlyMessageMixin { | ||
static final _emptyUnknownFields = new _ReadonlyUnknownFieldSet(); | ||
static final _emptyList = new List.unmodifiable([]); | ||
|
||
BuilderInfo get info_; | ||
|
||
get unknownFields => _emptyUnknownFields; | ||
|
||
List _getDefaultRepeatedField(int tagNumber, List value) => _emptyList; | ||
|
||
void addExtension(Extension extension, var value) => | ||
_readonly("addExtension"); | ||
|
||
void clear() => _readonly("clear"); | ||
|
||
void clearExtension(Extension extension) => _readonly("clearExtension"); | ||
|
||
void clearField(int tagNumber) => _readonly("clearField"); | ||
|
||
void mergeFromBuffer(List<int> input, | ||
[ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) => | ||
_readonly("mergeFromBuffer"); | ||
|
||
void mergeFromCodedBufferReader(CodedBufferReader input, | ||
[ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) => | ||
_readonly("mergeFromCodedBufferReader"); | ||
|
||
void mergeFromJson(String data, | ||
[ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) => | ||
_readonly("mergeFromJson"); | ||
|
||
void mergeFromJsonMap(Map<String, dynamic> json, | ||
[ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) => | ||
_readonly("mergeFromJsonMap"); | ||
|
||
void mergeFromMessage(GeneratedMessage other) => | ||
_readonly("mergeFromMessage"); | ||
|
||
void mergeUnknownFields(UnknownFieldSet unknownFieldSet) => | ||
_readonly("mergeUnknownFields"); | ||
|
||
void setExtension(Extension extension, var value) => | ||
_readonly("setExtension"); | ||
|
||
void setField(int tagNumber, var value, [int fieldType = null]) => | ||
_readonly("setField"); | ||
|
||
void _readonly(String methodName) { | ||
String messageType = info_.messageName; | ||
throw new UnsupportedError( | ||
"attempted to call $methodName on a read-only message ($messageType)"); | ||
} | ||
} | ||
|
||
class _ReadonlyUnknownFieldSet extends UnknownFieldSet { | ||
|
||
@override | ||
void clear() => _readonly("clear"); | ||
|
||
@override | ||
void addField(int number, UnknownFieldSetField field) => | ||
_readonly("addField"); | ||
|
||
@override | ||
void mergeField(int number, UnknownFieldSetField field) => | ||
_readonly("mergeField"); | ||
|
||
@override | ||
bool mergeFieldFromBuffer(int tag, CodedBufferReader input) { | ||
_readonly("mergeFieldFromBuffer"); | ||
return false; // not reached | ||
} | ||
|
||
@override | ||
void mergeFromCodedBufferReader(CodedBufferReader input) => | ||
_readonly("mergeFromCodedBufferReader"); | ||
|
||
@override | ||
void mergeFromUnknownFieldSet(UnknownFieldSet other) => | ||
_readonly("mergeFromUnknownFieldSet"); | ||
|
||
@override | ||
UnknownFieldSetField _getField(int number) { | ||
_readonly("a merge method"); | ||
return null; // not reached | ||
} | ||
|
||
void _readonly(String methodName) { | ||
throw new UnsupportedError( | ||
"attempted to call $methodName on a read-only UnknownFieldSet"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env dart | ||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
library readonly_message_test; | ||
|
||
import 'package:test/test.dart' show test, expect, throwsA, predicate; | ||
|
||
import 'package:protobuf/protobuf.dart' | ||
show GeneratedMessage, ReadonlyMessageMixin, BuilderInfo; | ||
|
||
throwsError(Type expectedType, String expectedMessage) => throwsA( | ||
predicate((x) { | ||
expect(x.runtimeType, expectedType); | ||
expect(x.message, expectedMessage); | ||
return true; | ||
})); | ||
|
||
class Rec extends GeneratedMessage with ReadonlyMessageMixin { | ||
@override | ||
BuilderInfo info_ = new BuilderInfo("rec"); | ||
} | ||
|
||
main() { | ||
test("can write a read-only message", () { | ||
expect(new Rec().writeToBuffer(), []); | ||
expect(new Rec().writeToJson(), "{}"); | ||
}); | ||
|
||
test("can't merge to a read-only message", () { | ||
expect(() => new Rec().mergeFromJson("{}"), throwsError(UnsupportedError, | ||
"attempted to call mergeFromJson on a read-only message (rec)")); | ||
}); | ||
|
||
test("can't set a field on a read-only message", () { | ||
expect(() => new Rec().setField(123, 456), throwsError(UnsupportedError, | ||
"attempted to call setField on a read-only message (rec)")); | ||
}); | ||
|
||
test("can't clear a read-only message", () { | ||
expect(() => new Rec().clear(), throwsError(UnsupportedError, | ||
"attempted to call clear on a read-only message (rec)")); | ||
}); | ||
|
||
test("can't modify unknown fields on a read-only message", () { | ||
expect(() => new Rec().unknownFields.clear(), throwsError(UnsupportedError, | ||
"attempted to call clear on a read-only UnknownFieldSet")); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters