From 9f37de960fec523c42b47d982f3fdab5b2d4d4d9 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 14 Jul 2015 10:24:52 +0100 Subject: Changing reflection namespace (part 1) - Move types into Google.Protobuf.Reflection - Change codegen to reflect that in generated types Generated code changes coming in part 2 --- csharp/src/ProtocolBuffers.Test/DescriptorsTest.cs | 225 ------------- csharp/src/ProtocolBuffers.Test/IssuesTest.cs | 2 +- .../ProtocolBuffers.Test.csproj | 2 +- .../Reflection/DescriptorsTest.cs | 223 +++++++++++++ .../ProtocolBuffers/Descriptors/DescriptorBase.cs | 84 ----- .../ProtocolBuffers/Descriptors/DescriptorPool.cs | 365 --------------------- .../ProtocolBuffers/Descriptors/DescriptorUtil.cs | 65 ---- .../Descriptors/DescriptorValidationException.cs | 80 ----- .../ProtocolBuffers/Descriptors/EnumDescriptor.cs | 109 ------ .../Descriptors/EnumValueDescriptor.cs | 63 ---- .../ProtocolBuffers/Descriptors/FieldDescriptor.cs | 293 ----------------- .../src/ProtocolBuffers/Descriptors/FieldType.cs | 60 ---- .../ProtocolBuffers/Descriptors/FileDescriptor.cs | 354 -------------------- .../src/ProtocolBuffers/Descriptors/IDescriptor.cs | 44 --- .../Descriptors/MessageDescriptor.cs | 173 ---------- .../Descriptors/MethodDescriptor.cs | 95 ------ .../ProtocolBuffers/Descriptors/OneofDescriptor.cs | 79 ----- .../Descriptors/PackageDescriptor.cs | 68 ---- .../Descriptors/ServiceDescriptor.cs | 90 ----- .../FieldAccess/FieldAccessorBase.cs | 68 ---- .../FieldAccess/FieldAccessorTable.cs | 98 ------ .../ProtocolBuffers/FieldAccess/IFieldAccessor.cs | 72 ---- .../FieldAccess/MapFieldAccessor.cs | 59 ---- .../ProtocolBuffers/FieldAccess/OneofAccessor.cs | 86 ----- .../ProtocolBuffers/FieldAccess/ReflectionUtil.cs | 106 ------ .../FieldAccess/RepeatedFieldAccessor.cs | 60 ---- .../FieldAccess/SingleFieldAccessor.cs | 86 ----- csharp/src/ProtocolBuffers/IMessage.cs | 2 +- csharp/src/ProtocolBuffers/JsonFormatter.cs | 3 +- csharp/src/ProtocolBuffers/ProtocolBuffers.csproj | 47 +-- .../ProtocolBuffers/Reflection/DescriptorBase.cs | 82 +++++ .../ProtocolBuffers/Reflection/DescriptorPool.cs | 365 +++++++++++++++++++++ .../ProtocolBuffers/Reflection/DescriptorUtil.cs | 65 ++++ .../Reflection/DescriptorValidationException.cs | 80 +++++ .../ProtocolBuffers/Reflection/EnumDescriptor.cs | 109 ++++++ .../Reflection/EnumValueDescriptor.cs | 63 ++++ .../Reflection/FieldAccessorBase.cs | 67 ++++ .../Reflection/FieldAccessorTable.cs | 97 ++++++ .../ProtocolBuffers/Reflection/FieldDescriptor.cs | 293 +++++++++++++++++ csharp/src/ProtocolBuffers/Reflection/FieldType.cs | 60 ++++ .../ProtocolBuffers/Reflection/FileDescriptor.cs | 354 ++++++++++++++++++++ .../src/ProtocolBuffers/Reflection/IDescriptor.cs | 44 +++ .../ProtocolBuffers/Reflection/IFieldAccessor.cs | 70 ++++ .../ProtocolBuffers/Reflection/MapFieldAccessor.cs | 58 ++++ .../Reflection/MessageDescriptor.cs | 173 ++++++++++ .../ProtocolBuffers/Reflection/MethodDescriptor.cs | 95 ++++++ .../ProtocolBuffers/Reflection/OneofAccessor.cs | 85 +++++ .../ProtocolBuffers/Reflection/OneofDescriptor.cs | 79 +++++ .../Reflection/PackageDescriptor.cs | 68 ++++ .../ProtocolBuffers/Reflection/ReflectionUtil.cs | 106 ++++++ .../Reflection/RepeatedFieldAccessor.cs | 59 ++++ .../Reflection/ServiceDescriptor.cs | 90 +++++ .../Reflection/SingleFieldAccessor.cs | 85 +++++ csharp/src/ProtocolBuffers/WireFormat.cs | 2 +- 54 files changed, 2899 insertions(+), 2911 deletions(-) delete mode 100644 csharp/src/ProtocolBuffers.Test/DescriptorsTest.cs create mode 100644 csharp/src/ProtocolBuffers.Test/Reflection/DescriptorsTest.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/DescriptorBase.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/DescriptorPool.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/DescriptorUtil.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/DescriptorValidationException.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/FieldType.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/FileDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/IDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/MessageDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/MethodDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/OneofDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/PackageDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/Descriptors/ServiceDescriptor.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs delete mode 100644 csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/DescriptorBase.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/DescriptorPool.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/DescriptorUtil.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/DescriptorValidationException.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/EnumDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/EnumValueDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/FieldAccessorBase.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/FieldAccessorTable.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/FieldDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/FieldType.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/FileDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/IDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/IFieldAccessor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/MapFieldAccessor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/MessageDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/MethodDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/OneofAccessor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/OneofDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/PackageDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/ReflectionUtil.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/RepeatedFieldAccessor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/ServiceDescriptor.cs create mode 100644 csharp/src/ProtocolBuffers/Reflection/SingleFieldAccessor.cs (limited to 'csharp') diff --git a/csharp/src/ProtocolBuffers.Test/DescriptorsTest.cs b/csharp/src/ProtocolBuffers.Test/DescriptorsTest.cs deleted file mode 100644 index d5c622bc..00000000 --- a/csharp/src/ProtocolBuffers.Test/DescriptorsTest.cs +++ /dev/null @@ -1,225 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System.Linq; -using Google.Protobuf.DescriptorProtos; -using Google.Protobuf.Descriptors; -using Google.Protobuf.TestProtos; -using NUnit.Framework; - -namespace Google.Protobuf -{ - /// - /// Tests for descriptors. (Not in its own namespace or broken up into individual classes as the - /// size doesn't warrant it. On the other hand, this makes me feel a bit dirty...) - /// - public class DescriptorsTest - { - [Test] - public void FileDescriptor() - { - FileDescriptor file = UnittestProto3.Descriptor; - - Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Name); - Assert.AreEqual("protobuf_unittest", file.Package); - - Assert.AreEqual("UnittestProto", file.Proto.Options.JavaOuterClassname); - Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Proto.Name); - - // unittest.proto doesn't have any public imports, but unittest_import.proto does. - Assert.AreEqual(0, file.PublicDependencies.Count); - Assert.AreEqual(1, UnittestImportProto3.Descriptor.PublicDependencies.Count); - Assert.AreEqual(UnittestImportPublicProto3.Descriptor, UnittestImportProto3.Descriptor.PublicDependencies[0]); - - Assert.AreEqual(1, file.Dependencies.Count); - Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0]); - - MessageDescriptor messageType = TestAllTypes.Descriptor; - Assert.AreEqual(messageType, file.MessageTypes[0]); - Assert.AreEqual(messageType, file.FindTypeByName("TestAllTypes")); - Assert.Null(file.FindTypeByName("NoSuchType")); - Assert.Null(file.FindTypeByName("protobuf_unittest.TestAllTypes")); - for (int i = 0; i < file.MessageTypes.Count; i++) - { - Assert.AreEqual(i, file.MessageTypes[i].Index); - } - - Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName("ForeignEnum")); - Assert.Null(file.FindTypeByName("NoSuchType")); - Assert.Null(file.FindTypeByName("protobuf_unittest.ForeignEnum")); - Assert.AreEqual(1, UnittestImportProto3.Descriptor.EnumTypes.Count); - Assert.AreEqual("ImportEnum", UnittestImportProto3.Descriptor.EnumTypes[0].Name); - for (int i = 0; i < file.EnumTypes.Count; i++) - { - Assert.AreEqual(i, file.EnumTypes[i].Index); - } - } - - [Test] - public void MessageDescriptor() - { - MessageDescriptor messageType = TestAllTypes.Descriptor; - MessageDescriptor nestedType = TestAllTypes.Types.NestedMessage.Descriptor; - - Assert.AreEqual("TestAllTypes", messageType.Name); - Assert.AreEqual("protobuf_unittest.TestAllTypes", messageType.FullName); - Assert.AreEqual(UnittestProto3.Descriptor, messageType.File); - Assert.IsNull(messageType.ContainingType); - Assert.IsNull(messageType.Proto.Options); - - Assert.AreEqual("TestAllTypes", messageType.Name); - - Assert.AreEqual("NestedMessage", nestedType.Name); - Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedMessage", nestedType.FullName); - Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); - Assert.AreEqual(messageType, nestedType.ContainingType); - - FieldDescriptor field = messageType.Fields[0]; - Assert.AreEqual("single_int32", field.Name); - Assert.AreEqual(field, messageType.FindDescriptor("single_int32")); - Assert.Null(messageType.FindDescriptor("no_such_field")); - Assert.AreEqual(field, messageType.FindFieldByNumber(1)); - Assert.Null(messageType.FindFieldByNumber(571283)); - for (int i = 0; i < messageType.Fields.Count; i++) - { - Assert.AreEqual(i, messageType.Fields[i].Index); - } - - Assert.AreEqual(nestedType, messageType.NestedTypes[0]); - Assert.AreEqual(nestedType, messageType.FindDescriptor("NestedMessage")); - Assert.Null(messageType.FindDescriptor("NoSuchType")); - for (int i = 0; i < messageType.NestedTypes.Count; i++) - { - Assert.AreEqual(i, messageType.NestedTypes[i].Index); - } - - Assert.AreEqual(messageType.EnumTypes[0], messageType.FindDescriptor("NestedEnum")); - Assert.Null(messageType.FindDescriptor("NoSuchType")); - for (int i = 0; i < messageType.EnumTypes.Count; i++) - { - Assert.AreEqual(i, messageType.EnumTypes[i].Index); - } - } - - [Test] - public void FieldDescriptor() - { - MessageDescriptor messageType = TestAllTypes.Descriptor; - FieldDescriptor primitiveField = messageType.FindDescriptor("single_int32"); - FieldDescriptor enumField = messageType.FindDescriptor("single_nested_enum"); - FieldDescriptor messageField = messageType.FindDescriptor("single_foreign_message"); - - Assert.AreEqual("single_int32", primitiveField.Name); - Assert.AreEqual("protobuf_unittest.TestAllTypes.single_int32", - primitiveField.FullName); - Assert.AreEqual(1, primitiveField.FieldNumber); - Assert.AreEqual(messageType, primitiveField.ContainingType); - Assert.AreEqual(UnittestProto3.Descriptor, primitiveField.File); - Assert.AreEqual(FieldType.Int32, primitiveField.FieldType); - Assert.IsNull(primitiveField.Proto.Options); - - Assert.AreEqual("single_nested_enum", enumField.Name); - Assert.AreEqual(FieldType.Enum, enumField.FieldType); - // Assert.AreEqual(TestAllTypes.Types.NestedEnum.DescriptorProtoFile, enumField.EnumType); - - Assert.AreEqual("single_foreign_message", messageField.Name); - Assert.AreEqual(FieldType.Message, messageField.FieldType); - Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType); - } - - [Test] - public void FieldDescriptorLabel() - { - FieldDescriptor singleField = - TestAllTypes.Descriptor.FindDescriptor("single_int32"); - FieldDescriptor repeatedField = - TestAllTypes.Descriptor.FindDescriptor("repeated_int32"); - - Assert.IsFalse(singleField.IsRepeated); - Assert.IsTrue(repeatedField.IsRepeated); - } - - [Test] - public void EnumDescriptor() - { - // Note: this test is a bit different to the Java version because there's no static way of getting to the descriptor - EnumDescriptor enumType = UnittestProto3.Descriptor.FindTypeByName("ForeignEnum"); - EnumDescriptor nestedType = TestAllTypes.Descriptor.FindDescriptor("NestedEnum"); - - Assert.AreEqual("ForeignEnum", enumType.Name); - Assert.AreEqual("protobuf_unittest.ForeignEnum", enumType.FullName); - Assert.AreEqual(UnittestProto3.Descriptor, enumType.File); - Assert.Null(enumType.ContainingType); - Assert.Null(enumType.Proto.Options); - - Assert.AreEqual("NestedEnum", nestedType.Name); - Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedEnum", - nestedType.FullName); - Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); - Assert.AreEqual(TestAllTypes.Descriptor, nestedType.ContainingType); - - EnumValueDescriptor value = enumType.FindValueByName("FOREIGN_FOO"); - Assert.AreEqual(value, enumType.Values[1]); - Assert.AreEqual("FOREIGN_FOO", value.Name); - Assert.AreEqual(4, value.Number); - Assert.AreEqual((int) ForeignEnum.FOREIGN_FOO, value.Number); - Assert.AreEqual(value, enumType.FindValueByNumber(4)); - Assert.Null(enumType.FindValueByName("NO_SUCH_VALUE")); - for (int i = 0; i < enumType.Values.Count; i++) - { - Assert.AreEqual(i, enumType.Values[i].Index); - } - } - - [Test] - public void OneofDescriptor() - { - OneofDescriptor descriptor = TestAllTypes.Descriptor.FindDescriptor("oneof_field"); - Assert.AreEqual("oneof_field", descriptor.Name); - Assert.AreEqual("protobuf_unittest.TestAllTypes.oneof_field", descriptor.FullName); - - var expectedFields = new[] { - TestAllTypes.OneofBytesFieldNumber, - TestAllTypes.OneofNestedMessageFieldNumber, - TestAllTypes.OneofStringFieldNumber, - TestAllTypes.OneofUint32FieldNumber } - .Select(fieldNumber => TestAllTypes.Descriptor.FindFieldByNumber(fieldNumber)) - .ToList(); - foreach (var field in expectedFields) - { - Assert.AreSame(descriptor, field.ContainingOneof); - } - - CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields); - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers.Test/IssuesTest.cs b/csharp/src/ProtocolBuffers.Test/IssuesTest.cs index d2248158..b5ad34ae 100644 --- a/csharp/src/ProtocolBuffers.Test/IssuesTest.cs +++ b/csharp/src/ProtocolBuffers.Test/IssuesTest.cs @@ -30,7 +30,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion -using Google.Protobuf.Descriptors; +using Google.Protobuf.Reflection; using UnitTest.Issues.TestProtos; using NUnit.Framework; diff --git a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index 269961c7..45c75bae 100644 --- a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -81,6 +81,7 @@ + @@ -89,7 +90,6 @@ - diff --git a/csharp/src/ProtocolBuffers.Test/Reflection/DescriptorsTest.cs b/csharp/src/ProtocolBuffers.Test/Reflection/DescriptorsTest.cs new file mode 100644 index 00000000..0db01a5e --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/Reflection/DescriptorsTest.cs @@ -0,0 +1,223 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System.Linq; +using Google.Protobuf.TestProtos; +using NUnit.Framework; + +namespace Google.Protobuf.Reflection +{ + /// + /// Tests for descriptors. (Not in its own namespace or broken up into individual classes as the + /// size doesn't warrant it. On the other hand, this makes me feel a bit dirty...) + /// + public class DescriptorsTest + { + [Test] + public void FileDescriptor() + { + FileDescriptor file = UnittestProto3.Descriptor; + + Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Name); + Assert.AreEqual("protobuf_unittest", file.Package); + + Assert.AreEqual("UnittestProto", file.Proto.Options.JavaOuterClassname); + Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Proto.Name); + + // unittest.proto doesn't have any public imports, but unittest_import.proto does. + Assert.AreEqual(0, file.PublicDependencies.Count); + Assert.AreEqual(1, UnittestImportProto3.Descriptor.PublicDependencies.Count); + Assert.AreEqual(UnittestImportPublicProto3.Descriptor, UnittestImportProto3.Descriptor.PublicDependencies[0]); + + Assert.AreEqual(1, file.Dependencies.Count); + Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0]); + + MessageDescriptor messageType = TestAllTypes.Descriptor; + Assert.AreEqual(messageType, file.MessageTypes[0]); + Assert.AreEqual(messageType, file.FindTypeByName("TestAllTypes")); + Assert.Null(file.FindTypeByName("NoSuchType")); + Assert.Null(file.FindTypeByName("protobuf_unittest.TestAllTypes")); + for (int i = 0; i < file.MessageTypes.Count; i++) + { + Assert.AreEqual(i, file.MessageTypes[i].Index); + } + + Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName("ForeignEnum")); + Assert.Null(file.FindTypeByName("NoSuchType")); + Assert.Null(file.FindTypeByName("protobuf_unittest.ForeignEnum")); + Assert.AreEqual(1, UnittestImportProto3.Descriptor.EnumTypes.Count); + Assert.AreEqual("ImportEnum", UnittestImportProto3.Descriptor.EnumTypes[0].Name); + for (int i = 0; i < file.EnumTypes.Count; i++) + { + Assert.AreEqual(i, file.EnumTypes[i].Index); + } + } + + [Test] + public void MessageDescriptor() + { + MessageDescriptor messageType = TestAllTypes.Descriptor; + MessageDescriptor nestedType = TestAllTypes.Types.NestedMessage.Descriptor; + + Assert.AreEqual("TestAllTypes", messageType.Name); + Assert.AreEqual("protobuf_unittest.TestAllTypes", messageType.FullName); + Assert.AreEqual(UnittestProto3.Descriptor, messageType.File); + Assert.IsNull(messageType.ContainingType); + Assert.IsNull(messageType.Proto.Options); + + Assert.AreEqual("TestAllTypes", messageType.Name); + + Assert.AreEqual("NestedMessage", nestedType.Name); + Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedMessage", nestedType.FullName); + Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); + Assert.AreEqual(messageType, nestedType.ContainingType); + + FieldDescriptor field = messageType.Fields[0]; + Assert.AreEqual("single_int32", field.Name); + Assert.AreEqual(field, messageType.FindDescriptor("single_int32")); + Assert.Null(messageType.FindDescriptor("no_such_field")); + Assert.AreEqual(field, messageType.FindFieldByNumber(1)); + Assert.Null(messageType.FindFieldByNumber(571283)); + for (int i = 0; i < messageType.Fields.Count; i++) + { + Assert.AreEqual(i, messageType.Fields[i].Index); + } + + Assert.AreEqual(nestedType, messageType.NestedTypes[0]); + Assert.AreEqual(nestedType, messageType.FindDescriptor("NestedMessage")); + Assert.Null(messageType.FindDescriptor("NoSuchType")); + for (int i = 0; i < messageType.NestedTypes.Count; i++) + { + Assert.AreEqual(i, messageType.NestedTypes[i].Index); + } + + Assert.AreEqual(messageType.EnumTypes[0], messageType.FindDescriptor("NestedEnum")); + Assert.Null(messageType.FindDescriptor("NoSuchType")); + for (int i = 0; i < messageType.EnumTypes.Count; i++) + { + Assert.AreEqual(i, messageType.EnumTypes[i].Index); + } + } + + [Test] + public void FieldDescriptor() + { + MessageDescriptor messageType = TestAllTypes.Descriptor; + FieldDescriptor primitiveField = messageType.FindDescriptor("single_int32"); + FieldDescriptor enumField = messageType.FindDescriptor("single_nested_enum"); + FieldDescriptor messageField = messageType.FindDescriptor("single_foreign_message"); + + Assert.AreEqual("single_int32", primitiveField.Name); + Assert.AreEqual("protobuf_unittest.TestAllTypes.single_int32", + primitiveField.FullName); + Assert.AreEqual(1, primitiveField.FieldNumber); + Assert.AreEqual(messageType, primitiveField.ContainingType); + Assert.AreEqual(UnittestProto3.Descriptor, primitiveField.File); + Assert.AreEqual(FieldType.Int32, primitiveField.FieldType); + Assert.IsNull(primitiveField.Proto.Options); + + Assert.AreEqual("single_nested_enum", enumField.Name); + Assert.AreEqual(FieldType.Enum, enumField.FieldType); + // Assert.AreEqual(TestAllTypes.Types.NestedEnum.DescriptorProtoFile, enumField.EnumType); + + Assert.AreEqual("single_foreign_message", messageField.Name); + Assert.AreEqual(FieldType.Message, messageField.FieldType); + Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType); + } + + [Test] + public void FieldDescriptorLabel() + { + FieldDescriptor singleField = + TestAllTypes.Descriptor.FindDescriptor("single_int32"); + FieldDescriptor repeatedField = + TestAllTypes.Descriptor.FindDescriptor("repeated_int32"); + + Assert.IsFalse(singleField.IsRepeated); + Assert.IsTrue(repeatedField.IsRepeated); + } + + [Test] + public void EnumDescriptor() + { + // Note: this test is a bit different to the Java version because there's no static way of getting to the descriptor + EnumDescriptor enumType = UnittestProto3.Descriptor.FindTypeByName("ForeignEnum"); + EnumDescriptor nestedType = TestAllTypes.Descriptor.FindDescriptor("NestedEnum"); + + Assert.AreEqual("ForeignEnum", enumType.Name); + Assert.AreEqual("protobuf_unittest.ForeignEnum", enumType.FullName); + Assert.AreEqual(UnittestProto3.Descriptor, enumType.File); + Assert.Null(enumType.ContainingType); + Assert.Null(enumType.Proto.Options); + + Assert.AreEqual("NestedEnum", nestedType.Name); + Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedEnum", + nestedType.FullName); + Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); + Assert.AreEqual(TestAllTypes.Descriptor, nestedType.ContainingType); + + EnumValueDescriptor value = enumType.FindValueByName("FOREIGN_FOO"); + Assert.AreEqual(value, enumType.Values[1]); + Assert.AreEqual("FOREIGN_FOO", value.Name); + Assert.AreEqual(4, value.Number); + Assert.AreEqual((int) ForeignEnum.FOREIGN_FOO, value.Number); + Assert.AreEqual(value, enumType.FindValueByNumber(4)); + Assert.Null(enumType.FindValueByName("NO_SUCH_VALUE")); + for (int i = 0; i < enumType.Values.Count; i++) + { + Assert.AreEqual(i, enumType.Values[i].Index); + } + } + + [Test] + public void OneofDescriptor() + { + OneofDescriptor descriptor = TestAllTypes.Descriptor.FindDescriptor("oneof_field"); + Assert.AreEqual("oneof_field", descriptor.Name); + Assert.AreEqual("protobuf_unittest.TestAllTypes.oneof_field", descriptor.FullName); + + var expectedFields = new[] { + TestAllTypes.OneofBytesFieldNumber, + TestAllTypes.OneofNestedMessageFieldNumber, + TestAllTypes.OneofStringFieldNumber, + TestAllTypes.OneofUint32FieldNumber } + .Select(fieldNumber => TestAllTypes.Descriptor.FindFieldByNumber(fieldNumber)) + .ToList(); + foreach (var field in expectedFields) + { + Assert.AreSame(descriptor, field.ContainingOneof); + } + + CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields); + } + } +} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/DescriptorBase.cs b/csharp/src/ProtocolBuffers/Descriptors/DescriptorBase.cs deleted file mode 100644 index 0eb71215..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/DescriptorBase.cs +++ /dev/null @@ -1,84 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Base class for nearly all descriptors, providing common functionality. - /// - public abstract class DescriptorBase : IDescriptor - { - private readonly FileDescriptor file; - private readonly string fullName; - private readonly int index; - - internal DescriptorBase(FileDescriptor file, string fullName, int index) - { - this.file = file; - this.fullName = fullName; - this.index = index; - } - - /// - /// The index of this descriptor within its parent descriptor. - /// - /// - /// This returns the index of this descriptor within its parent, for - /// this descriptor's type. (There can be duplicate values for different - /// types, e.g. one enum type with index 0 and one message type with index 0.) - /// - public int Index - { - get { return index; } - } - - public abstract string Name { get; } - - /// - /// The fully qualified name of the descriptor's target. - /// - public string FullName - { - get { return fullName; } - } - - /// - /// The file this descriptor was declared in. - /// - public FileDescriptor File - { - get { return file; } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/DescriptorPool.cs b/csharp/src/ProtocolBuffers/Descriptors/DescriptorPool.cs deleted file mode 100644 index b07af060..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/DescriptorPool.cs +++ /dev/null @@ -1,365 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Contains lookup tables containing all the descriptors defined in a particular file. - /// - internal sealed class DescriptorPool - { - private readonly IDictionary descriptorsByName = - new Dictionary(); - - private readonly IDictionary fieldsByNumber = - new Dictionary(); - - private readonly IDictionary enumValuesByNumber = - new Dictionary(); - - private readonly HashSet dependencies; - - internal DescriptorPool(FileDescriptor[] dependencyFiles) - { - dependencies = new HashSet(); - for (int i = 0; i < dependencyFiles.Length; i++) - { - dependencies.Add(dependencyFiles[i]); - ImportPublicDependencies(dependencyFiles[i]); - } - - foreach (FileDescriptor dependency in dependencyFiles) - { - AddPackage(dependency.Package, dependency); - } - } - - private void ImportPublicDependencies(FileDescriptor file) - { - foreach (FileDescriptor dependency in file.PublicDependencies) - { - if (dependencies.Add(dependency)) - { - ImportPublicDependencies(dependency); - } - } - } - - /// - /// Finds a symbol of the given name within the pool. - /// - /// The type of symbol to look for - /// Fully-qualified name to look up - /// The symbol with the given name and type, - /// or null if the symbol doesn't exist or has the wrong type - internal T FindSymbol(string fullName) where T : class - { - IDescriptor result; - descriptorsByName.TryGetValue(fullName, out result); - T descriptor = result as T; - if (descriptor != null) - { - return descriptor; - } - - foreach (FileDescriptor dependency in dependencies) - { - dependency.DescriptorPool.descriptorsByName.TryGetValue(fullName, out result); - descriptor = result as T; - if (descriptor != null) - { - return descriptor; - } - } - - return null; - } - - /// - /// Adds a package to the symbol tables. If a package by the same name - /// already exists, that is fine, but if some other kind of symbol - /// exists under the same name, an exception is thrown. If the package - /// has multiple components, this also adds the parent package(s). - /// - internal void AddPackage(string fullName, FileDescriptor file) - { - int dotpos = fullName.LastIndexOf('.'); - String name; - if (dotpos != -1) - { - AddPackage(fullName.Substring(0, dotpos), file); - name = fullName.Substring(dotpos + 1); - } - else - { - name = fullName; - } - - IDescriptor old; - if (descriptorsByName.TryGetValue(fullName, out old)) - { - if (!(old is PackageDescriptor)) - { - throw new DescriptorValidationException(file, - "\"" + name + - "\" is already defined (as something other than a " + - "package) in file \"" + old.File.Name + "\"."); - } - } - descriptorsByName[fullName] = new PackageDescriptor(name, fullName, file); - } - - /// - /// Adds a symbol to the symbol table. - /// - /// The symbol already existed - /// in the symbol table. - internal void AddSymbol(IDescriptor descriptor) - { - ValidateSymbolName(descriptor); - String fullName = descriptor.FullName; - - IDescriptor old; - if (descriptorsByName.TryGetValue(fullName, out old)) - { - int dotPos = fullName.LastIndexOf('.'); - string message; - if (descriptor.File == old.File) - { - if (dotPos == -1) - { - message = "\"" + fullName + "\" is already defined."; - } - else - { - message = "\"" + fullName.Substring(dotPos + 1) + "\" is already defined in \"" + - fullName.Substring(0, dotPos) + "\"."; - } - } - else - { - message = "\"" + fullName + "\" is already defined in file \"" + old.File.Name + "\"."; - } - throw new DescriptorValidationException(descriptor, message); - } - descriptorsByName[fullName] = descriptor; - } - - private static readonly Regex ValidationRegex = new Regex("^[_A-Za-z][_A-Za-z0-9]*$", - FrameworkPortability.CompiledRegexWhereAvailable); - - /// - /// Verifies that the descriptor's name is valid (i.e. it contains - /// only letters, digits and underscores, and does not start with a digit). - /// - /// - private static void ValidateSymbolName(IDescriptor descriptor) - { - if (descriptor.Name == "") - { - throw new DescriptorValidationException(descriptor, "Missing name."); - } - if (!ValidationRegex.IsMatch(descriptor.Name)) - { - throw new DescriptorValidationException(descriptor, - "\"" + descriptor.Name + "\" is not a valid identifier."); - } - } - - /// - /// Returns the field with the given number in the given descriptor, - /// or null if it can't be found. - /// - internal FieldDescriptor FindFieldByNumber(MessageDescriptor messageDescriptor, int number) - { - FieldDescriptor ret; - fieldsByNumber.TryGetValue(new DescriptorIntPair(messageDescriptor, number), out ret); - return ret; - } - - internal EnumValueDescriptor FindEnumValueByNumber(EnumDescriptor enumDescriptor, int number) - { - EnumValueDescriptor ret; - enumValuesByNumber.TryGetValue(new DescriptorIntPair(enumDescriptor, number), out ret); - return ret; - } - - /// - /// Adds a field to the fieldsByNumber table. - /// - /// A field with the same - /// containing type and number already exists. - internal void AddFieldByNumber(FieldDescriptor field) - { - DescriptorIntPair key = new DescriptorIntPair(field.ContainingType, field.FieldNumber); - FieldDescriptor old; - if (fieldsByNumber.TryGetValue(key, out old)) - { - throw new DescriptorValidationException(field, "Field number " + field.FieldNumber + - "has already been used in \"" + - field.ContainingType.FullName + - "\" by field \"" + old.Name + "\"."); - } - fieldsByNumber[key] = field; - } - - /// - /// Adds an enum value to the enumValuesByNumber table. If an enum value - /// with the same type and number already exists, this method does nothing. - /// (This is allowed; the first value defined with the number takes precedence.) - /// - internal void AddEnumValueByNumber(EnumValueDescriptor enumValue) - { - DescriptorIntPair key = new DescriptorIntPair(enumValue.EnumDescriptor, enumValue.Number); - if (!enumValuesByNumber.ContainsKey(key)) - { - enumValuesByNumber[key] = enumValue; - } - } - - /// - /// Looks up a descriptor by name, relative to some other descriptor. - /// The name may be fully-qualified (with a leading '.'), partially-qualified, - /// or unqualified. C++-like name lookup semantics are used to search for the - /// matching descriptor. - /// - internal IDescriptor LookupSymbol(string name, IDescriptor relativeTo) - { - // TODO(jonskeet): This could be optimized in a number of ways. - - IDescriptor result; - if (name.StartsWith(".")) - { - // Fully-qualified name. - result = FindSymbol(name.Substring(1)); - } - else - { - // If "name" is a compound identifier, we want to search for the - // first component of it, then search within it for the rest. - int firstPartLength = name.IndexOf('.'); - string firstPart = firstPartLength == -1 ? name : name.Substring(0, firstPartLength); - - // We will search each parent scope of "relativeTo" looking for the - // symbol. - StringBuilder scopeToTry = new StringBuilder(relativeTo.FullName); - - while (true) - { - // Chop off the last component of the scope. - - // TODO(jonskeet): Make this more efficient. May not be worth using StringBuilder at all - int dotpos = scopeToTry.ToString().LastIndexOf("."); - if (dotpos == -1) - { - result = FindSymbol(name); - break; - } - else - { - scopeToTry.Length = dotpos + 1; - - // Append firstPart and try to find. - scopeToTry.Append(firstPart); - result = FindSymbol(scopeToTry.ToString()); - - if (result != null) - { - if (firstPartLength != -1) - { - // We only found the first part of the symbol. Now look for - // the whole thing. If this fails, we *don't* want to keep - // searching parent scopes. - scopeToTry.Length = dotpos + 1; - scopeToTry.Append(name); - result = FindSymbol(scopeToTry.ToString()); - } - break; - } - - // Not found. Remove the name so we can try again. - scopeToTry.Length = dotpos; - } - } - } - - if (result == null) - { - throw new DescriptorValidationException(relativeTo, "\"" + name + "\" is not defined."); - } - else - { - return result; - } - } - - /// - /// Struct used to hold the keys for the fieldByNumber table. - /// - private struct DescriptorIntPair : IEquatable - { - private readonly int number; - private readonly IDescriptor descriptor; - - internal DescriptorIntPair(IDescriptor descriptor, int number) - { - this.number = number; - this.descriptor = descriptor; - } - - public bool Equals(DescriptorIntPair other) - { - return descriptor == other.descriptor - && number == other.number; - } - - public override bool Equals(object obj) - { - if (obj is DescriptorIntPair) - { - return Equals((DescriptorIntPair) obj); - } - return false; - } - - public override int GetHashCode() - { - return descriptor.GetHashCode()*((1 << 16) - 1) + number; - } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/DescriptorUtil.cs b/csharp/src/ProtocolBuffers/Descriptors/DescriptorUtil.cs deleted file mode 100644 index eb4ced60..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/DescriptorUtil.cs +++ /dev/null @@ -1,65 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System.Collections.Generic; -using System.Collections.ObjectModel; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Internal class containing utility methods when working with descriptors. - /// - internal static class DescriptorUtil - { - /// - /// Equivalent to Func[TInput, int, TOutput] but usable in .NET 2.0. Only used to convert - /// arrays. - /// - internal delegate TOutput IndexedConverter(TInput element, int index); - - /// - /// Converts the given array into a read-only list, applying the specified conversion to - /// each input element. - /// - internal static IList ConvertAndMakeReadOnly(IList input, - IndexedConverter - converter) - { - TOutput[] array = new TOutput[input.Count]; - for (int i = 0; i < array.Length; i++) - { - array[i] = converter(input[i], i); - } - return new ReadOnlyCollection(array); - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/DescriptorValidationException.cs b/csharp/src/ProtocolBuffers/Descriptors/DescriptorValidationException.cs deleted file mode 100644 index 855eda61..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/DescriptorValidationException.cs +++ /dev/null @@ -1,80 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Thrown when building descriptors fails because the source DescriptorProtos - /// are not valid. - /// - public sealed class DescriptorValidationException : Exception - { - private readonly String name; - private readonly string description; - - /// - /// The full name of the descriptor where the error occurred. - /// - public String ProblemSymbolName - { - get { return name; } - } - - /// - /// A human-readable description of the error. (The Message property - /// is made up of the descriptor's name and this description.) - /// - public string Description - { - get { return description; } - } - - internal DescriptorValidationException(IDescriptor problemDescriptor, string description) : - base(problemDescriptor.FullName + ": " + description) - { - // Note that problemDescriptor may be partially uninitialized, so we - // don't want to expose it directly to the user. So, we only provide - // the name and the original proto. - name = problemDescriptor.FullName; - this.description = description; - } - - internal DescriptorValidationException(IDescriptor problemDescriptor, string description, Exception cause) : - base(problemDescriptor.FullName + ": " + description, cause) - { - name = problemDescriptor.FullName; - this.description = description; - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs deleted file mode 100644 index 57860e61..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs +++ /dev/null @@ -1,109 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System.Collections.Generic; -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Descriptor for an enum type in a .proto file. - /// - public sealed class EnumDescriptor : DescriptorBase - { - private readonly EnumDescriptorProto proto; - private readonly MessageDescriptor containingType; - private readonly IList values; - - internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) - : base(file, file.ComputeFullName(parent, proto.Name), index) - { - this.proto = proto; - containingType = parent; - - if (proto.Value.Count == 0) - { - // We cannot allow enums with no values because this would mean there - // would be no valid default value for fields of this type. - throw new DescriptorValidationException(this, "Enums must contain at least one value."); - } - - values = DescriptorUtil.ConvertAndMakeReadOnly(proto.Value, - (value, i) => new EnumValueDescriptor(value, file, this, i)); - - File.DescriptorPool.AddSymbol(this); - } - - internal EnumDescriptorProto Proto { get { return proto; } } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - /// - /// If this is a nested type, get the outer descriptor, otherwise null. - /// - public MessageDescriptor ContainingType - { - get { return containingType; } - } - - /// - /// An unmodifiable list of defined value descriptors for this enum. - /// - public IList Values - { - get { return values; } - } - - /// - /// Finds an enum value by number. If multiple enum values have the - /// same number, this returns the first defined value with that number. - /// If there is no value for the given number, this returns null. - /// - public EnumValueDescriptor FindValueByNumber(int number) - { - return File.DescriptorPool.FindEnumValueByNumber(this, number); - } - - /// - /// Finds an enum value by name. - /// - /// The unqualified name of the value (e.g. "FOO"). - /// The value's descriptor, or null if not found. - public EnumValueDescriptor FindValueByName(string name) - { - return File.DescriptorPool.FindSymbol(FullName + "." + name); - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs deleted file mode 100644 index e609b1f8..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs +++ /dev/null @@ -1,63 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Descriptor for a single enum value within an enum in a .proto file. - /// - public sealed class EnumValueDescriptor : DescriptorBase - { - private readonly EnumDescriptor enumDescriptor; - private readonly EnumValueDescriptorProto proto; - - internal EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file, - EnumDescriptor parent, int index) - : base(file, parent.FullName + "." + proto.Name, index) - { - this.proto = proto; - enumDescriptor = parent; - file.DescriptorPool.AddSymbol(this); - file.DescriptorPool.AddEnumValueByNumber(this); - } - - internal EnumValueDescriptorProto Proto { get { return proto; } } - - public override string Name { get { return proto.Name; } } - - public int Number { get { return Proto.Number; } } - - public EnumDescriptor EnumDescriptor { get { return enumDescriptor; } } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs deleted file mode 100644 index 7af69bbb..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs +++ /dev/null @@ -1,293 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Descriptor for a field or extension within a message in a .proto file. - /// - public sealed class FieldDescriptor : DescriptorBase, IComparable - { - private readonly FieldDescriptorProto proto; - private EnumDescriptor enumType; - private MessageDescriptor messageType; - private readonly MessageDescriptor containingType; - private readonly OneofDescriptor containingOneof; - private FieldType fieldType; - - internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, - MessageDescriptor parent, int index) - : base(file, file.ComputeFullName(parent, proto.Name), index) - { - this.proto = proto; - if (proto.Type != 0) - { - fieldType = GetFieldTypeFromProtoType(proto.Type); - } - - if (FieldNumber <= 0) - { - throw new DescriptorValidationException(this, - "Field numbers must be positive integers."); - } - containingType = parent; - // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction. - if (proto.OneofIndex != -1) - { - if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count) - { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.oneof_index is out of range for type " + parent.Name); - } - containingOneof = parent.Oneofs[proto.OneofIndex]; - } - - file.DescriptorPool.AddSymbol(this); - } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - internal FieldDescriptorProto Proto { get { return proto; } } - - /// - /// Maps a field type as included in the .proto file to a FieldType. - /// - private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Type type) - { - switch (type) - { - case FieldDescriptorProto.Types.Type.TYPE_DOUBLE: - return FieldType.Double; - case FieldDescriptorProto.Types.Type.TYPE_FLOAT: - return FieldType.Float; - case FieldDescriptorProto.Types.Type.TYPE_INT64: - return FieldType.Int64; - case FieldDescriptorProto.Types.Type.TYPE_UINT64: - return FieldType.UInt64; - case FieldDescriptorProto.Types.Type.TYPE_INT32: - return FieldType.Int32; - case FieldDescriptorProto.Types.Type.TYPE_FIXED64: - return FieldType.Fixed64; - case FieldDescriptorProto.Types.Type.TYPE_FIXED32: - return FieldType.Fixed32; - case FieldDescriptorProto.Types.Type.TYPE_BOOL: - return FieldType.Bool; - case FieldDescriptorProto.Types.Type.TYPE_STRING: - return FieldType.String; - case FieldDescriptorProto.Types.Type.TYPE_GROUP: - return FieldType.Group; - case FieldDescriptorProto.Types.Type.TYPE_MESSAGE: - return FieldType.Message; - case FieldDescriptorProto.Types.Type.TYPE_BYTES: - return FieldType.Bytes; - case FieldDescriptorProto.Types.Type.TYPE_UINT32: - return FieldType.UInt32; - case FieldDescriptorProto.Types.Type.TYPE_ENUM: - return FieldType.Enum; - case FieldDescriptorProto.Types.Type.TYPE_SFIXED32: - return FieldType.SFixed32; - case FieldDescriptorProto.Types.Type.TYPE_SFIXED64: - return FieldType.SFixed64; - case FieldDescriptorProto.Types.Type.TYPE_SINT32: - return FieldType.SInt32; - case FieldDescriptorProto.Types.Type.TYPE_SINT64: - return FieldType.SInt64; - default: - throw new ArgumentException("Invalid type specified"); - } - } - - public bool IsRepeated - { - get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; } - } - - public bool IsMap - { - get { return fieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; } - } - - public bool IsPacked - { - get { return Proto.Options != null && Proto.Options.Packed; } - } - - /// - /// Get the field's containing type. For extensions, this is the type being - /// extended, not the location where the extension was defined. See - /// . - /// - public MessageDescriptor ContainingType - { - get { return containingType; } - } - - public OneofDescriptor ContainingOneof - { - get { return containingOneof; } - } - - public FieldType FieldType - { - get { return fieldType; } - } - - public int FieldNumber - { - get { return Proto.Number; } - } - - /// - /// Compares this descriptor with another one, ordering in "canonical" order - /// which simply means ascending order by field number. - /// must be a field of the same type, i.e. the of - /// both fields must be the same. - /// - public int CompareTo(FieldDescriptor other) - { - if (other.containingType != containingType) - { - throw new ArgumentException("FieldDescriptors can only be compared to other FieldDescriptors " + - "for fields of the same message type."); - } - return FieldNumber - other.FieldNumber; - } - - /// - /// For enum fields, returns the field's type. - /// - public EnumDescriptor EnumType - { - get - { - if (fieldType != FieldType.Enum) - { - throw new InvalidOperationException("EnumType is only valid for enum fields."); - } - return enumType; - } - } - - /// - /// For embedded message and group fields, returns the field's type. - /// - public MessageDescriptor MessageType - { - get - { - if (fieldType != FieldType.Message) - { - throw new InvalidOperationException("MessageType is only valid for enum fields."); - } - return messageType; - } - } - - /// - /// Look up and cross-link all field types etc. - /// - internal void CrossLink() - { - if (Proto.TypeName != "") - { - IDescriptor typeDescriptor = - File.DescriptorPool.LookupSymbol(Proto.TypeName, this); - - if (Proto.Type != 0) - { - // Choose field type based on symbol. - if (typeDescriptor is MessageDescriptor) - { - fieldType = FieldType.Message; - } - else if (typeDescriptor is EnumDescriptor) - { - fieldType = FieldType.Enum; - } - else - { - throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not a type."); - } - } - - if (fieldType == FieldType.Message) - { - if (!(typeDescriptor is MessageDescriptor)) - { - throw new DescriptorValidationException(this, - "\"" + Proto.TypeName + "\" is not a message type."); - } - messageType = (MessageDescriptor) typeDescriptor; - - if (Proto.DefaultValue != "") - { - throw new DescriptorValidationException(this, "Messages can't have default values."); - } - } - else if (fieldType == FieldType.Enum) - { - if (!(typeDescriptor is EnumDescriptor)) - { - throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not an enum type."); - } - enumType = (EnumDescriptor) typeDescriptor; - } - else - { - throw new DescriptorValidationException(this, "Field with primitive type has type_name."); - } - } - else - { - if (fieldType == FieldType.Message || fieldType == FieldType.Enum) - { - throw new DescriptorValidationException(this, "Field with message or enum type missing type_name."); - } - } - - // Note: no attempt to perform any default value parsing - - File.DescriptorPool.AddFieldByNumber(this); - - if (containingType != null && containingType.Proto.Options != null && containingType.Proto.Options.MessageSetWireFormat) - { - throw new DescriptorValidationException(this, "MessageSet format is not supported."); - } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/FieldType.cs b/csharp/src/ProtocolBuffers/Descriptors/FieldType.cs deleted file mode 100644 index 69851464..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/FieldType.cs +++ /dev/null @@ -1,60 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -namespace Google.Protobuf.Descriptors -{ - /// - /// Enumeration of all the possible field types. The odd formatting is to make it very clear - /// which attribute applies to which value, while maintaining a compact format. - /// - public enum FieldType - { - Double, - Float, - Int64, - UInt64, - Int32, - Fixed64, - Fixed32, - Bool, - String, - Group, - Message, - Bytes, - UInt32, - SFixed32, - SFixed64, - SInt32, - SInt64, - Enum - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/FileDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/FileDescriptor.cs deleted file mode 100644 index 9d0bdfd3..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/FileDescriptor.cs +++ /dev/null @@ -1,354 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Google.Protobuf.DescriptorProtos; -using FileOptions = Google.Protobuf.DescriptorProtos.FileOptions; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Describes a .proto file, including everything defined within. - /// IDescriptor is implemented such that the File property returns this descriptor, - /// and the FullName is the same as the Name. - /// - public sealed class FileDescriptor : IDescriptor - { - private readonly FileDescriptorProto proto; - private readonly IList messageTypes; - private readonly IList enumTypes; - private readonly IList services; - private readonly IList dependencies; - private readonly IList publicDependencies; - private readonly DescriptorPool pool; - - public enum ProtoSyntax - { - Proto2, - Proto3 - } - - public ProtoSyntax Syntax - { - get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; } - } - - private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies) - { - this.pool = pool; - this.proto = proto; - this.dependencies = new ReadOnlyCollection((FileDescriptor[]) dependencies.Clone()); - - publicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies); - - pool.AddPackage(Package, this); - - messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType, - (message, index) => - new MessageDescriptor(message, this, null, index)); - - enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, - (enumType, index) => - new EnumDescriptor(enumType, this, null, index)); - - services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service, - (service, index) => - new ServiceDescriptor(service, this, index)); - } - - /// - /// Computes the full name of a descriptor within this file, with an optional parent message. - /// - internal string ComputeFullName(MessageDescriptor parent, string name) - { - if (parent != null) - { - return parent.FullName + "." + name; - } - if (Package.Length > 0) - { - return Package + "." + name; - } - return name; - } - - /// - /// Extracts public dependencies from direct dependencies. This is a static method despite its - /// first parameter, as the value we're in the middle of constructing is only used for exceptions. - /// - private static IList DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies) - { - var nameToFileMap = new Dictionary(); - foreach (var file in dependencies) - { - nameToFileMap[file.Name] = file; - } - var publicDependencies = new List(); - for (int i = 0; i < proto.PublicDependency.Count; i++) - { - int index = proto.PublicDependency[i]; - if (index < 0 || index >= proto.Dependency.Count) - { - throw new DescriptorValidationException(@this, "Invalid public dependency index."); - } - string name = proto.Dependency[index]; - FileDescriptor file = nameToFileMap[name]; - if (file == null) - { - if (!allowUnknownDependencies) - { - throw new DescriptorValidationException(@this, "Invalid public dependency: " + name); - } - // Ignore unknown dependencies. - } - else - { - publicDependencies.Add(file); - } - } - return new ReadOnlyCollection(publicDependencies); - } - - /// - /// The descriptor in its protocol message representation. - /// - internal FileDescriptorProto Proto - { - get { return proto; } - } - - /// - /// The file name. - /// - public string Name - { - get { return proto.Name; } - } - - /// - /// The package as declared in the .proto file. This may or may not - /// be equivalent to the .NET namespace of the generated classes. - /// - public string Package - { - get { return proto.Package; } - } - - /// - /// Unmodifiable list of top-level message types declared in this file. - /// - public IList MessageTypes - { - get { return messageTypes; } - } - - /// - /// Unmodifiable list of top-level enum types declared in this file. - /// - public IList EnumTypes - { - get { return enumTypes; } - } - - /// - /// Unmodifiable list of top-level services declared in this file. - /// - public IList Services - { - get { return services; } - } - - /// - /// Unmodifiable list of this file's dependencies (imports). - /// - public IList Dependencies - { - get { return dependencies; } - } - - /// - /// Unmodifiable list of this file's public dependencies (public imports). - /// - public IList PublicDependencies - { - get { return publicDependencies; } - } - - /// - /// Implementation of IDescriptor.FullName - just returns the same as Name. - /// - string IDescriptor.FullName - { - get { return Name; } - } - - /// - /// Implementation of IDescriptor.File - just returns this descriptor. - /// - FileDescriptor IDescriptor.File - { - get { return this; } - } - - /// - /// Pool containing symbol descriptors. - /// - internal DescriptorPool DescriptorPool - { - get { return pool; } - } - - /// - /// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types. - /// - /// The unqualified type name to look for. - /// The type of descriptor to look for (or ITypeDescriptor for any) - /// The type's descriptor, or null if not found. - public T FindTypeByName(String name) - where T : class, IDescriptor - { - // Don't allow looking up nested types. This will make optimization - // easier later. - if (name.IndexOf('.') != -1) - { - return null; - } - if (Package.Length > 0) - { - name = Package + "." + name; - } - T result = pool.FindSymbol(name); - if (result != null && result.File == this) - { - return result; - } - return null; - } - - /// - /// Builds a FileDescriptor from its protocol buffer representation. - /// - /// The protocol message form of the FileDescriptor. - /// FileDescriptors corresponding to all of the - /// file's dependencies, in the exact order listed in the .proto file. May be null, - /// in which case it is treated as an empty array. - /// Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false). - /// If is not - /// a valid descriptor. This can occur for a number of reasons, such as a field - /// having an undefined type or because two messages were defined with the same name. - private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies) - { - // Building descriptors involves two steps: translating and linking. - // In the translation step (implemented by FileDescriptor's - // constructor), we build an object tree mirroring the - // FileDescriptorProto's tree and put all of the descriptors into the - // DescriptorPool's lookup tables. In the linking step, we look up all - // type references in the DescriptorPool, so that, for example, a - // FieldDescriptor for an embedded message contains a pointer directly - // to the Descriptor for that message's type. We also detect undefined - // types in the linking step. - if (dependencies == null) - { - dependencies = new FileDescriptor[0]; - } - - DescriptorPool pool = new DescriptorPool(dependencies); - FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies); - - // TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code, - // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".) - //if (dependencies.Length != proto.DependencyCount) - //{ - // throw new DescriptorValidationException(result, - // "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - // "those listed in the FileDescriptorProto."); - //} - //for (int i = 0; i < proto.DependencyCount; i++) - //{ - // if (dependencies[i].Name != proto.DependencyList[i]) - // { - // throw new DescriptorValidationException(result, - // "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - // "those listed in the FileDescriptorProto."); - // } - //} - - result.CrossLink(); - return result; - } - - private void CrossLink() - { - foreach (MessageDescriptor message in messageTypes) - { - message.CrossLink(); - } - - foreach (ServiceDescriptor service in services) - { - service.CrossLink(); - } - } - - public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData, - FileDescriptor[] dependencies) - { - FileDescriptorProto proto; - try - { - proto = FileDescriptorProto.Parser.ParseFrom(descriptorData); - } - catch (InvalidProtocolBufferException e) - { - throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e); - } - - try - { - // When building descriptors for generated code, we allow unknown - // dependencies by default. - return BuildFrom(proto, dependencies, true); - } - catch (DescriptorValidationException e) - { - throw new ArgumentException("Invalid embedded descriptor for \"" + proto.Name + "\".", e); - } - } - - public override string ToString() - { - return "FileDescriptor for " + proto.Name; - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/IDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/IDescriptor.cs deleted file mode 100644 index 92c6d463..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/IDescriptor.cs +++ /dev/null @@ -1,44 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -namespace Google.Protobuf.Descriptors -{ - /// - /// Interface implemented by all descriptor types. - /// - public interface IDescriptor - { - string Name { get; } - string FullName { get; } - FileDescriptor File { get; } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/MessageDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/MessageDescriptor.cs deleted file mode 100644 index e65e8bb0..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/MessageDescriptor.cs +++ /dev/null @@ -1,173 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.Generic; -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Describes a message type. - /// - public sealed class MessageDescriptor : DescriptorBase - { - private readonly DescriptorProto proto; - private readonly MessageDescriptor containingType; - private readonly IList nestedTypes; - private readonly IList enumTypes; - private readonly IList fields; - private readonly IList oneofs; - - internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex) - : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) - { - this.proto = proto; - containingType = parent; - - oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDecl, - (oneof, index) => - new OneofDescriptor(oneof, file, this, index)); - - nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedType, - (type, index) => - new MessageDescriptor(type, file, this, index)); - - enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, - (type, index) => - new EnumDescriptor(type, file, this, index)); - - // TODO(jonskeet): Sort fields first? - fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.Field, - (field, index) => - new FieldDescriptor(field, file, this, index)); - file.DescriptorPool.AddSymbol(this); - } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - internal DescriptorProto Proto { get { return proto; } } - - /// - /// If this is a nested type, get the outer descriptor, otherwise null. - /// - public MessageDescriptor ContainingType - { - get { return containingType; } - } - - /// - /// An unmodifiable list of this message type's fields. - /// - public IList Fields - { - get { return fields; } - } - - /// - /// An unmodifiable list of this message type's nested types. - /// - public IList NestedTypes - { - get { return nestedTypes; } - } - - /// - /// An unmodifiable list of this message type's enum types. - /// - public IList EnumTypes - { - get { return enumTypes; } - } - - public IList Oneofs - { - get { return oneofs; } - } - - /// - /// Finds a field by field name. - /// - /// The unqualified name of the field (e.g. "foo"). - /// The field's descriptor, or null if not found. - public FieldDescriptor FindFieldByName(String name) - { - return File.DescriptorPool.FindSymbol(FullName + "." + name); - } - - /// - /// Finds a field by field number. - /// - /// The field number within this message type. - /// The field's descriptor, or null if not found. - public FieldDescriptor FindFieldByNumber(int number) - { - return File.DescriptorPool.FindFieldByNumber(this, number); - } - - /// - /// Finds a nested descriptor by name. The is valid for fields, nested - /// message types, oneofs and enums. - /// - /// The unqualified name of the descriptor, e.g. "Foo" - /// The descriptor, or null if not found. - public T FindDescriptor(string name) - where T : class, IDescriptor - { - return File.DescriptorPool.FindSymbol(FullName + "." + name); - } - - /// - /// Looks up and cross-links all fields and nested types. - /// - internal void CrossLink() - { - foreach (MessageDescriptor message in nestedTypes) - { - message.CrossLink(); - } - - foreach (FieldDescriptor field in fields) - { - field.CrossLink(); - } - - foreach (OneofDescriptor oneof in oneofs) - { - oneof.CrossLink(); - } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/MethodDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/MethodDescriptor.cs deleted file mode 100644 index 7d4a6f4f..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/MethodDescriptor.cs +++ /dev/null @@ -1,95 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Describes a single method in a service. - /// - public sealed class MethodDescriptor : DescriptorBase - { - private readonly MethodDescriptorProto proto; - private readonly ServiceDescriptor service; - private MessageDescriptor inputType; - private MessageDescriptor outputType; - - /// - /// The service this method belongs to. - /// - public ServiceDescriptor Service { get { return service; } } - - /// - /// The method's input type. - /// - public MessageDescriptor InputType { get { return inputType; } } - - /// - /// The method's input type. - /// - public MessageDescriptor OutputType { get { return outputType; } } - - internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file, - ServiceDescriptor parent, int index) - : base(file, parent.FullName + "." + proto.Name, index) - { - this.proto = proto; - service = parent; - file.DescriptorPool.AddSymbol(this); - } - - internal MethodDescriptorProto Proto { get { return proto; } } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - internal void CrossLink() - { - IDescriptor lookup = File.DescriptorPool.LookupSymbol(Proto.InputType, this); - if (!(lookup is MessageDescriptor)) - { - throw new DescriptorValidationException(this, "\"" + Proto.InputType + "\" is not a message type."); - } - inputType = (MessageDescriptor) lookup; - - lookup = File.DescriptorPool.LookupSymbol(Proto.OutputType, this); - if (!(lookup is MessageDescriptor)) - { - throw new DescriptorValidationException(this, "\"" + Proto.OutputType + "\" is not a message type."); - } - outputType = (MessageDescriptor) lookup; - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/OneofDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/OneofDescriptor.cs deleted file mode 100644 index 8418948f..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/OneofDescriptor.cs +++ /dev/null @@ -1,79 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - public sealed class OneofDescriptor : DescriptorBase - { - private readonly OneofDescriptorProto proto; - private MessageDescriptor containingType; - private IList fields; - - internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) - : base(file, file.ComputeFullName(parent, proto.Name), index) - { - this.proto = proto; - containingType = parent; - - file.DescriptorPool.AddSymbol(this); - } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - public MessageDescriptor ContainingType - { - get { return containingType; } - } - - public IList Fields { get { return fields; } } - - internal void CrossLink() - { - List fieldCollection = new List(); - foreach (var field in ContainingType.Fields) - { - if (field.ContainingOneof == this) - { - fieldCollection.Add(field); - } - } - fields = new ReadOnlyCollection(fieldCollection); - } - } -} diff --git a/csharp/src/ProtocolBuffers/Descriptors/PackageDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/PackageDescriptor.cs deleted file mode 100644 index 18adc9e3..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/PackageDescriptor.cs +++ /dev/null @@ -1,68 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -namespace Google.Protobuf.Descriptors -{ - /// - /// Represents a package in the symbol table. We use PackageDescriptors - /// just as placeholders so that someone cannot define, say, a message type - /// that has the same name as an existing package. - /// - internal sealed class PackageDescriptor : IDescriptor - { - private readonly string name; - private readonly string fullName; - private readonly FileDescriptor file; - - internal PackageDescriptor(string name, string fullName, FileDescriptor file) - { - this.file = file; - this.fullName = fullName; - this.name = name; - } - - public string Name - { - get { return name; } - } - - public string FullName - { - get { return fullName; } - } - - public FileDescriptor File - { - get { return file; } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Descriptors/ServiceDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/ServiceDescriptor.cs deleted file mode 100644 index 2556e272..00000000 --- a/csharp/src/ProtocolBuffers/Descriptors/ServiceDescriptor.cs +++ /dev/null @@ -1,90 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.Generic; -using Google.Protobuf.DescriptorProtos; - -namespace Google.Protobuf.Descriptors -{ - /// - /// Describes a service type. - /// - public sealed class ServiceDescriptor : DescriptorBase - { - private readonly ServiceDescriptorProto proto; - private readonly IList methods; - - internal ServiceDescriptor(ServiceDescriptorProto proto, FileDescriptor file, int index) - : base(file, file.ComputeFullName(null, proto.Name), index) - { - this.proto = proto; - methods = DescriptorUtil.ConvertAndMakeReadOnly(proto.Method, - (method, i) => new MethodDescriptor(method, file, this, i)); - - file.DescriptorPool.AddSymbol(this); - } - - /// - /// The brief name of the descriptor's target. - /// - public override string Name { get { return proto.Name; } } - - internal ServiceDescriptorProto Proto { get { return proto; } } - - /// - /// An unmodifiable list of methods in this service. - /// - public IList Methods - { - get { return methods; } - } - - /// - /// Finds a method by name. - /// - /// The unqualified name of the method (e.g. "Foo"). - /// The method's decsriptor, or null if not found. - public MethodDescriptor FindMethodByName(String name) - { - return File.DescriptorPool.FindSymbol(FullName + "." + name); - } - - internal void CrossLink() - { - foreach (MethodDescriptor method in methods) - { - method.CrossLink(); - } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs deleted file mode 100644 index 2a3e5b8b..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs +++ /dev/null @@ -1,68 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Reflection; -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Base class for field accessors. - /// - internal abstract class FieldAccessorBase : IFieldAccessor - { - private readonly Func getValueDelegate; - private readonly FieldDescriptor descriptor; - - internal FieldAccessorBase(Type type, string propertyName, FieldDescriptor descriptor) - { - PropertyInfo property = type.GetProperty(propertyName); - if (property == null || !property.CanRead) - { - throw new ArgumentException("Not all required properties/methods available"); - } - this.descriptor = descriptor; - getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod()); - } - - public FieldDescriptor Descriptor { get { return descriptor; } } - - public object GetValue(object message) - { - return getValueDelegate(message); - } - - public abstract void Clear(object message); - public abstract void SetValue(object message, object value); - } -} diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs deleted file mode 100644 index 80be93f5..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs +++ /dev/null @@ -1,98 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.ObjectModel; -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Provides access to fields in generated messages via reflection. - /// - public sealed class FieldAccessorTable - { - private readonly ReadOnlyCollection accessors; - private readonly ReadOnlyCollection oneofs; - private readonly MessageDescriptor descriptor; - - /// - /// Constructs a FieldAccessorTable for a particular message class. - /// Only one FieldAccessorTable should be constructed per class. - /// - /// The CLR type for the message. - /// The type's descriptor - /// The Pascal-case names of all the field-based properties in the message. - public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames) - { - this.descriptor = descriptor; - var accessorsArray = new IFieldAccessor[descriptor.Fields.Count]; - for (int i = 0; i < accessorsArray.Length; i++) - { - var field = descriptor.Fields[i]; - var name = propertyNames[i]; - accessorsArray[i] = - field.IsMap ? new MapFieldAccessor(type, name, field) - : field.IsRepeated ? new RepeatedFieldAccessor(type, name, field) - : (IFieldAccessor) new SingleFieldAccessor(type, name, field); - } - accessors = new ReadOnlyCollection(accessorsArray); - var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count]; - for (int i = 0; i < oneofsArray.Length; i++) - { - var oneof = descriptor.Oneofs[i]; - oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof); - } - oneofs = new ReadOnlyCollection(oneofsArray); - } - - // TODO: Validate the name here... should possibly make this type a more "general reflection access" type, - // bearing in mind the oneof parts to come as well. - /// - /// Returns all of the field accessors for the message type. - /// - public ReadOnlyCollection Accessors { get { return accessors; } } - - public ReadOnlyCollection Oneofs { get { return oneofs; } } - - // TODO: Review this, as it's easy to get confused between FieldNumber and Index. - // Currently only used to get an accessor related to a oneof... maybe just make that simpler? - public IFieldAccessor this[int fieldNumber] - { - get - { - FieldDescriptor field = descriptor.FindFieldByNumber(fieldNumber); - return accessors[field.Index]; - } - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs deleted file mode 100644 index d1727cb4..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs +++ /dev/null @@ -1,72 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Allows fields to be reflectively accessed. - /// - public interface IFieldAccessor - { - /// - /// Returns the descriptor associated with this field. - /// - FieldDescriptor Descriptor { get; } - - // TODO: Should the argument type for these messages by IReflectedMessage? - - /// - /// Clears the field in the specified message. (For repeated fields, - /// this clears the list.) - /// - void Clear(object message); - - /// - /// Fetches the field value. For repeated values, this will be an - /// implementation. For map values, this will be an - /// implementation. - /// - object GetValue(object message); - - /// - /// Mutator for single "simple" fields only. - /// - /// - /// Repeated fields are mutated by fetching the value and manipulating it as a list. - /// Map fields are mutated by fetching the value and manipulating it as a dictionary. - /// - /// The field is not a "simple" field, or the message is frozen. - void SetValue(object message, object value); - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs deleted file mode 100644 index 100dbb37..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs +++ /dev/null @@ -1,59 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections; -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Accessor for map fields. - /// - internal sealed class MapFieldAccessor : FieldAccessorBase - { - internal MapFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) - { - } - - public override void Clear(object message) - { - IDictionary list = (IDictionary) GetValue(message); - list.Clear(); - } - - public override void SetValue(object message, object value) - { - throw new InvalidOperationException("SetValue is not implemented for map fields"); - } - } -} diff --git a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs deleted file mode 100644 index 590b6309..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs +++ /dev/null @@ -1,86 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using Google.Protobuf.Descriptors; -using System; -using System.Reflection; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Reflection access for a oneof, allowing clear and "get case" actions. - /// - public sealed class OneofAccessor - { - private readonly Func caseDelegate; - private readonly Action clearDelegate; - private OneofDescriptor descriptor; - - internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor) - { - PropertyInfo property = type.GetProperty(propertyName + "Case"); - if (property == null || !property.CanRead) - { - throw new ArgumentException("Not all required properties/methods available"); - } - this.descriptor = descriptor; - caseDelegate = ReflectionUtil.CreateFuncObjectT(property.GetGetMethod()); - - this.descriptor = descriptor; - MethodInfo clearMethod = type.GetMethod("Clear" + propertyName); - clearDelegate = ReflectionUtil.CreateActionObject(clearMethod); - } - - public OneofDescriptor Descriptor { get { return descriptor; } } - - /// - /// Clears the oneof in the specified message. - /// - public void Clear(object message) - { - clearDelegate(message); - } - - /// - /// Indicates which field in the oneof is set for specified message - /// - public FieldDescriptor GetCaseFieldDescriptor(object message) - { - int fieldNumber = caseDelegate(message); - if (fieldNumber > 0) - { - return descriptor.ContainingType.FindFieldByNumber(fieldNumber); - } - return null; - } - } -} diff --git a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs deleted file mode 100644 index 08ef6c0c..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ /dev/null @@ -1,106 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Linq.Expressions; -using System.Reflection; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// The methods in this class are somewhat evil, and should not be tampered with lightly. - /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos - /// which are more strongly typed. They do this by creating an appropriate strongly typed - /// delegate from the MethodInfo, and then calling that within an anonymous method. - /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are - /// very fast compared with calling Invoke later on. - /// - internal static class ReflectionUtil - { - /// - /// Empty Type[] used when calling GetProperty to force property instead of indexer fetching. - /// - internal static readonly Type[] EmptyTypes = new Type[0]; - - /// - /// Creates a delegate which will cast the argument to the appropriate method target type, - /// call the method on it, then convert the result to object. - /// - internal static Func CreateFuncObjectObject(MethodInfo method) - { - ParameterExpression parameter = Expression.Parameter(typeof(object), "p"); - Expression downcast = Expression.Convert(parameter, method.DeclaringType); - Expression call = Expression.Call(downcast, method); - Expression upcast = Expression.Convert(call, typeof(object)); - return Expression.Lambda>(upcast, parameter).Compile(); - } - - /// - /// Creates a delegate which will cast the argument to the appropriate method target type, - /// call the method on it, then convert the result to the specified type. - /// - internal static Func CreateFuncObjectT(MethodInfo method) - { - ParameterExpression parameter = Expression.Parameter(typeof(object), "p"); - Expression downcast = Expression.Convert(parameter, method.DeclaringType); - Expression call = Expression.Call(downcast, method); - Expression upcast = Expression.Convert(call, typeof(T)); - return Expression.Lambda>(upcast, parameter).Compile(); - } - - /// - /// Creates a delegate which will execute the given method after casting the first argument to - /// the target type of the method, and the second argument to the first parameter type of the method. - /// - internal static Action CreateActionObjectObject(MethodInfo method) - { - ParameterExpression targetParameter = Expression.Parameter(typeof(object), "target"); - ParameterExpression argParameter = Expression.Parameter(typeof(object), "arg"); - Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType); - Expression castArgument = Expression.Convert(argParameter, method.GetParameters()[0].ParameterType); - Expression call = Expression.Call(castTarget, method, castArgument); - return Expression.Lambda>(call, targetParameter, argParameter).Compile(); - } - - /// - /// Creates a delegate which will execute the given method after casting the first argument to - /// the target type of the method. - /// - internal static Action CreateActionObject(MethodInfo method) - { - ParameterExpression targetParameter = Expression.Parameter(typeof(object), "target"); - Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType); - Expression call = Expression.Call(castTarget, method); - return Expression.Lambda>(call, targetParameter).Compile(); - } - } -} \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs deleted file mode 100644 index 8d7ecbaf..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs +++ /dev/null @@ -1,60 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections; -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Accessor for repeated fields. - /// - internal sealed class RepeatedFieldAccessor : FieldAccessorBase - { - internal RepeatedFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) - { - } - - public override void Clear(object message) - { - IList list = (IList) GetValue(message); - list.Clear(); - } - - public override void SetValue(object message, object value) - { - throw new InvalidOperationException("SetValue is not implemented for repeated fields"); - } - - } -} diff --git a/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs deleted file mode 100644 index cdc89e8d..00000000 --- a/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs +++ /dev/null @@ -1,86 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Reflection; -using Google.Protobuf.Descriptors; - -namespace Google.Protobuf.FieldAccess -{ - /// - /// Accessor for single fields. - /// - internal sealed class SingleFieldAccessor : FieldAccessorBase - { - // All the work here is actually done in the constructor - it creates the appropriate delegates. - // There are various cases to consider, based on the property type (message, string/bytes, or "genuine" primitive) - // and proto2 vs proto3 for non-message types, as proto3 doesn't support "full" presence detection or default - // values. - - private readonly Action setValueDelegate; - private readonly Action clearDelegate; - - internal SingleFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) - { - PropertyInfo property = type.GetProperty(propertyName); - // We know there *is* such a property, or the base class constructor would have thrown. We should be able to write - // to it though. - if (!property.CanWrite) - { - throw new ArgumentException("Not all required properties/methods available"); - } - setValueDelegate = ReflectionUtil.CreateActionObjectObject(property.GetSetMethod()); - - var clrType = property.PropertyType; - - // TODO: What should clear on a oneof member do? Clear the oneof? - - // TODO: Validate that this is a reasonable single field? (Should be a value type, a message type, or string/ByteString.) - object defaultValue = - typeof(IMessage).IsAssignableFrom(clrType) ? null - : clrType == typeof(string) ? "" - : clrType == typeof(ByteString) ? ByteString.Empty - : Activator.CreateInstance(clrType); - clearDelegate = message => SetValue(message, defaultValue); - } - - public override void Clear(object message) - { - clearDelegate(message); - } - - public override void SetValue(object message, object value) - { - setValueDelegate(message, value); - } - } -} diff --git a/csharp/src/ProtocolBuffers/IMessage.cs b/csharp/src/ProtocolBuffers/IMessage.cs index 3324e9ae..8c4a4aaf 100644 --- a/csharp/src/ProtocolBuffers/IMessage.cs +++ b/csharp/src/ProtocolBuffers/IMessage.cs @@ -31,7 +31,7 @@ #endregion using System; -using Google.Protobuf.FieldAccess; +using Google.Protobuf.Reflection; namespace Google.Protobuf { diff --git a/csharp/src/ProtocolBuffers/JsonFormatter.cs b/csharp/src/ProtocolBuffers/JsonFormatter.cs index 9bd628eb..dacc7221 100644 --- a/csharp/src/ProtocolBuffers/JsonFormatter.cs +++ b/csharp/src/ProtocolBuffers/JsonFormatter.cs @@ -34,8 +34,7 @@ using System; using System.Collections; using System.Globalization; using System.Text; -using Google.Protobuf.Descriptors; -using Google.Protobuf.FieldAccess; +using Google.Protobuf.Reflection; namespace Google.Protobuf { diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj index f5dd6d5a..8f1d87ab 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -62,45 +62,46 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + +