diff options
Diffstat (limited to 'csharp/src/Google.Protobuf')
9 files changed, 235 insertions, 133 deletions
diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index ef524baf..5557612a 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -117,6 +117,7 @@ <Compile Include="Reflection\MethodDescriptor.cs" />
<Compile Include="Reflection\OneofAccessor.cs" />
<Compile Include="Reflection\OneofDescriptor.cs" />
+ <Compile Include="Reflection\OriginalNameAttribute.cs" />
<Compile Include="Reflection\PackageDescriptor.cs" />
<Compile Include="Reflection\PartialClasses.cs" />
<Compile Include="Reflection\ReflectionUtil.cs" />
diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.nuspec b/csharp/src/Google.Protobuf/Google.Protobuf.nuspec index f51bc89a..2892b8bf 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.nuspec +++ b/csharp/src/Google.Protobuf/Google.Protobuf.nuspec @@ -33,10 +33,12 @@ <dependency id="System.Linq.Expressions" version="4.0.0" /> <dependency id="System.ObjectModel" version="4.0.0" /> <dependency id="System.Reflection" version="4.0.0" /> + <dependency id="System.Reflection.Extensions" version="4.0.0" /> <dependency id="System.Runtime" version="4.0.0" /> <dependency id="System.Runtime.Extensions" version="4.0.0" /> <dependency id="System.Text.Encoding" version="4.0.0" /> <dependency id="System.Text.RegularExpressions" version="4.0.0" /> + <dependency id="System.Threading" version="4.0.0" /> </group> </dependencies> </metadata> diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index cbd9366c..73a4f64b 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -39,6 +39,7 @@ using Google.Protobuf.WellKnownTypes; using System.IO; using System.Linq; using System.Collections.Generic; +using System.Reflection; namespace Google.Protobuf { @@ -420,9 +421,10 @@ namespace Google.Protobuf } else if (value is System.Enum) { - if (System.Enum.IsDefined(value.GetType(), value)) + string name = OriginalEnumValueHelper.GetOriginalName(value); + if (name != null) { - WriteString(writer, value.ToString()); + WriteString(writer, name); } else { @@ -877,5 +879,44 @@ namespace Google.Protobuf TypeRegistry = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry)); } } + + // Effectively a cache of mapping from enum values to the original name as specified in the proto file, + // fetched by reflection. + // The need for this is unfortunate, as is its unbounded size, but realistically it shouldn't cause issues. + private static class OriginalEnumValueHelper + { + // TODO: In the future we might want to use ConcurrentDictionary, at the point where all + // the platforms we target have it. + private static readonly Dictionary<System.Type, Dictionary<object, string>> dictionaries + = new Dictionary<System.Type, Dictionary<object, string>>(); + + internal static string GetOriginalName(object value) + { + var enumType = value.GetType(); + Dictionary<object, string> nameMapping; + lock (dictionaries) + { + if (!dictionaries.TryGetValue(enumType, out nameMapping)) + { + nameMapping = GetNameMapping(enumType); + dictionaries[enumType] = nameMapping; + } + } + + string originalName; + // If this returns false, originalName will be null, which is what we want. + nameMapping.TryGetValue(value, out originalName); + return originalName; + } + + private static Dictionary<object, string> GetNameMapping(System.Type enumType) => + enumType.GetTypeInfo().DeclaredFields + .Where(f => f.IsStatic) + .ToDictionary(f => f.GetValue(null), + f => f.GetCustomAttributes<OriginalNameAttribute>() + .FirstOrDefault() + // If the attribute hasn't been applied, fall back to the name of the field. + ?.Name ?? f.Name); + } } } diff --git a/csharp/src/Google.Protobuf/Reflection/Descriptor.cs b/csharp/src/Google.Protobuf/Reflection/Descriptor.cs index 7de7b5fc..c003c0ff 100644 --- a/csharp/src/Google.Protobuf/Reflection/Descriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/Descriptor.cs @@ -1291,7 +1291,7 @@ namespace Google.Protobuf.Reflection { /// <summary>Field number for the "label" field.</summary> public const int LabelFieldNumber = 4; - private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label label_ = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL; + private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label label_ = 0; public global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label Label { get { return label_; } set { @@ -1301,7 +1301,7 @@ namespace Google.Protobuf.Reflection { /// <summary>Field number for the "type" field.</summary> public const int TypeFieldNumber = 5; - private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type type_ = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE; + private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type type_ = 0; /// <summary> /// If type_name is set, this need not be set. If both this and type_name /// are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP. @@ -1429,8 +1429,8 @@ namespace Google.Protobuf.Reflection { int hash = 1; if (Name.Length != 0) hash ^= Name.GetHashCode(); if (Number != 0) hash ^= Number.GetHashCode(); - if (Label != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL) hash ^= Label.GetHashCode(); - if (Type != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE) hash ^= Type.GetHashCode(); + if (Label != 0) hash ^= Label.GetHashCode(); + if (Type != 0) hash ^= Type.GetHashCode(); if (TypeName.Length != 0) hash ^= TypeName.GetHashCode(); if (Extendee.Length != 0) hash ^= Extendee.GetHashCode(); if (DefaultValue.Length != 0) hash ^= DefaultValue.GetHashCode(); @@ -1457,11 +1457,11 @@ namespace Google.Protobuf.Reflection { output.WriteRawTag(24); output.WriteInt32(Number); } - if (Label != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL) { + if (Label != 0) { output.WriteRawTag(32); output.WriteEnum((int) Label); } - if (Type != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE) { + if (Type != 0) { output.WriteRawTag(40); output.WriteEnum((int) Type); } @@ -1495,10 +1495,10 @@ namespace Google.Protobuf.Reflection { if (Number != 0) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Number); } - if (Label != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL) { + if (Label != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Label); } - if (Type != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE) { + if (Type != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Type); } if (TypeName.Length != 0) { @@ -1532,10 +1532,10 @@ namespace Google.Protobuf.Reflection { if (other.Number != 0) { Number = other.Number; } - if (other.Label != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL) { + if (other.Label != 0) { Label = other.Label; } - if (other.Type != global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE) { + if (other.Type != 0) { Type = other.Type; } if (other.TypeName.Length != 0) { @@ -1624,59 +1624,59 @@ namespace Google.Protobuf.Reflection { /// 0 is reserved for errors. /// Order is weird for historical reasons. /// </summary> - TYPE_DOUBLE = 1, - TYPE_FLOAT = 2, + [pbr::OriginalName("TYPE_DOUBLE")] Double = 1, + [pbr::OriginalName("TYPE_FLOAT")] Float = 2, /// <summary> /// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if /// negative values are likely. /// </summary> - TYPE_INT64 = 3, - TYPE_UINT64 = 4, + [pbr::OriginalName("TYPE_INT64")] Int64 = 3, + [pbr::OriginalName("TYPE_UINT64")] Uint64 = 4, /// <summary> /// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if /// negative values are likely. /// </summary> - TYPE_INT32 = 5, - TYPE_FIXED64 = 6, - TYPE_FIXED32 = 7, - TYPE_BOOL = 8, - TYPE_STRING = 9, + [pbr::OriginalName("TYPE_INT32")] Int32 = 5, + [pbr::OriginalName("TYPE_FIXED64")] Fixed64 = 6, + [pbr::OriginalName("TYPE_FIXED32")] Fixed32 = 7, + [pbr::OriginalName("TYPE_BOOL")] Bool = 8, + [pbr::OriginalName("TYPE_STRING")] String = 9, /// <summary> /// Tag-delimited aggregate. /// </summary> - TYPE_GROUP = 10, + [pbr::OriginalName("TYPE_GROUP")] Group = 10, /// <summary> /// Length-delimited aggregate. /// </summary> - TYPE_MESSAGE = 11, + [pbr::OriginalName("TYPE_MESSAGE")] Message = 11, /// <summary> /// New in version 2. /// </summary> - TYPE_BYTES = 12, - TYPE_UINT32 = 13, - TYPE_ENUM = 14, - TYPE_SFIXED32 = 15, - TYPE_SFIXED64 = 16, + [pbr::OriginalName("TYPE_BYTES")] Bytes = 12, + [pbr::OriginalName("TYPE_UINT32")] Uint32 = 13, + [pbr::OriginalName("TYPE_ENUM")] Enum = 14, + [pbr::OriginalName("TYPE_SFIXED32")] Sfixed32 = 15, + [pbr::OriginalName("TYPE_SFIXED64")] Sfixed64 = 16, /// <summary> /// Uses ZigZag encoding. /// </summary> - TYPE_SINT32 = 17, + [pbr::OriginalName("TYPE_SINT32")] Sint32 = 17, /// <summary> /// Uses ZigZag encoding. /// </summary> - TYPE_SINT64 = 18, + [pbr::OriginalName("TYPE_SINT64")] Sint64 = 18, } internal enum Label { /// <summary> /// 0 is reserved for errors /// </summary> - LABEL_OPTIONAL = 1, - LABEL_REQUIRED = 2, + [pbr::OriginalName("LABEL_OPTIONAL")] Optional = 1, + [pbr::OriginalName("LABEL_REQUIRED")] Required = 2, /// <summary> /// TODO(sanjay): Should we add LABEL_MAP? /// </summary> - LABEL_REPEATED = 3, + [pbr::OriginalName("LABEL_REPEATED")] Repeated = 3, } } @@ -2666,7 +2666,7 @@ namespace Google.Protobuf.Reflection { /// <summary>Field number for the "optimize_for" field.</summary> public const int OptimizeForFieldNumber = 9; - private global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode optimizeFor_ = global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED; + private global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode optimizeFor_ = 0; public global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode OptimizeFor { get { return optimizeFor_; } set { @@ -2854,7 +2854,7 @@ namespace Google.Protobuf.Reflection { if (JavaMultipleFiles != false) hash ^= JavaMultipleFiles.GetHashCode(); if (JavaGenerateEqualsAndHash != false) hash ^= JavaGenerateEqualsAndHash.GetHashCode(); if (JavaStringCheckUtf8 != false) hash ^= JavaStringCheckUtf8.GetHashCode(); - if (OptimizeFor != global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED) hash ^= OptimizeFor.GetHashCode(); + if (OptimizeFor != 0) hash ^= OptimizeFor.GetHashCode(); if (GoPackage.Length != 0) hash ^= GoPackage.GetHashCode(); if (CcGenericServices != false) hash ^= CcGenericServices.GetHashCode(); if (JavaGenericServices != false) hash ^= JavaGenericServices.GetHashCode(); @@ -2881,7 +2881,7 @@ namespace Google.Protobuf.Reflection { output.WriteRawTag(66); output.WriteString(JavaOuterClassname); } - if (OptimizeFor != global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED) { + if (OptimizeFor != 0) { output.WriteRawTag(72); output.WriteEnum((int) OptimizeFor); } @@ -2953,7 +2953,7 @@ namespace Google.Protobuf.Reflection { if (JavaStringCheckUtf8 != false) { size += 2 + 1; } - if (OptimizeFor != global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED) { + if (OptimizeFor != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) OptimizeFor); } if (GoPackage.Length != 0) { @@ -3006,7 +3006,7 @@ namespace Google.Protobuf.Reflection { if (other.JavaStringCheckUtf8 != false) { JavaStringCheckUtf8 = other.JavaStringCheckUtf8; } - if (other.OptimizeFor != global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED) { + if (other.OptimizeFor != 0) { OptimizeFor = other.OptimizeFor; } if (other.GoPackage.Length != 0) { @@ -3125,15 +3125,15 @@ namespace Google.Protobuf.Reflection { /// <summary> /// Generate complete code for parsing, serialization, /// </summary> - SPEED = 1, + [pbr::OriginalName("SPEED")] Speed = 1, /// <summary> /// etc. /// </summary> - CODE_SIZE = 2, + [pbr::OriginalName("CODE_SIZE")] CodeSize = 2, /// <summary> /// Generate code using MessageLite and the lite runtime. /// </summary> - LITE_RUNTIME = 3, + [pbr::OriginalName("LITE_RUNTIME")] LiteRuntime = 3, } } @@ -3436,7 +3436,7 @@ namespace Google.Protobuf.Reflection { /// <summary>Field number for the "ctype" field.</summary> public const int CtypeFieldNumber = 1; - private global::Google.Protobuf.Reflection.FieldOptions.Types.CType ctype_ = global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING; + private global::Google.Protobuf.Reflection.FieldOptions.Types.CType ctype_ = 0; /// <summary> /// The ctype option instructs the C++ code generator to use a different /// representation of the field than it normally would. See the specific @@ -3469,7 +3469,7 @@ namespace Google.Protobuf.Reflection { /// <summary>Field number for the "jstype" field.</summary> public const int JstypeFieldNumber = 6; - private global::Google.Protobuf.Reflection.FieldOptions.Types.JSType jstype_ = global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL; + private global::Google.Protobuf.Reflection.FieldOptions.Types.JSType jstype_ = 0; /// <summary> /// The jstype option determines the JavaScript type used for values of the /// field. The option is permitted only for 64 bit integral and fixed types @@ -3591,9 +3591,9 @@ namespace Google.Protobuf.Reflection { public override int GetHashCode() { int hash = 1; - if (Ctype != global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING) hash ^= Ctype.GetHashCode(); + if (Ctype != 0) hash ^= Ctype.GetHashCode(); if (Packed != false) hash ^= Packed.GetHashCode(); - if (Jstype != global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL) hash ^= Jstype.GetHashCode(); + if (Jstype != 0) hash ^= Jstype.GetHashCode(); if (Lazy != false) hash ^= Lazy.GetHashCode(); if (Deprecated != false) hash ^= Deprecated.GetHashCode(); if (Weak != false) hash ^= Weak.GetHashCode(); @@ -3606,7 +3606,7 @@ namespace Google.Protobuf.Reflection { } public void WriteTo(pb::CodedOutputStream output) { - if (Ctype != global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING) { + if (Ctype != 0) { output.WriteRawTag(8); output.WriteEnum((int) Ctype); } @@ -3622,7 +3622,7 @@ namespace Google.Protobuf.Reflection { output.WriteRawTag(40); output.WriteBool(Lazy); } - if (Jstype != global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL) { + if (Jstype != 0) { output.WriteRawTag(48); output.WriteEnum((int) Jstype); } @@ -3635,13 +3635,13 @@ namespace Google.Protobuf.Reflection { public int CalculateSize() { int size = 0; - if (Ctype != global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING) { + if (Ctype != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Ctype); } if (Packed != false) { size += 1 + 1; } - if (Jstype != global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL) { + if (Jstype != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Jstype); } if (Lazy != false) { @@ -3661,13 +3661,13 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Ctype != global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING) { + if (other.Ctype != 0) { Ctype = other.Ctype; } if (other.Packed != false) { Packed = other.Packed; } - if (other.Jstype != global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL) { + if (other.Jstype != 0) { Jstype = other.Jstype; } if (other.Lazy != false) { @@ -3729,24 +3729,24 @@ namespace Google.Protobuf.Reflection { /// <summary> /// Default mode. /// </summary> - STRING = 0, - CORD = 1, - STRING_PIECE = 2, + [pbr::OriginalName("STRING")] String = 0, + [pbr::OriginalName("CORD")] Cord = 1, + [pbr::OriginalName("STRING_PIECE")] StringPiece = 2, } internal enum JSType { /// <summary> /// Use the default type. /// </summary> - JS_NORMAL = 0, + [pbr::OriginalName("JS_NORMAL")] JsNormal = 0, /// <summary> /// Use JavaScript strings. /// </summary> - JS_STRING = 1, + [pbr::OriginalName("JS_STRING")] JsString = 1, /// <summary> /// Use JavaScript numbers. /// </summary> - JS_NUMBER = 2, + [pbr::OriginalName("JS_NUMBER")] JsNumber = 2, } } diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index de6e5717..6c6f6ee0 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -133,41 +133,41 @@ namespace Google.Protobuf.Reflection { switch (type) { - case FieldDescriptorProto.Types.Type.TYPE_DOUBLE: + case FieldDescriptorProto.Types.Type.Double: return FieldType.Double; - case FieldDescriptorProto.Types.Type.TYPE_FLOAT: + case FieldDescriptorProto.Types.Type.Float: return FieldType.Float; - case FieldDescriptorProto.Types.Type.TYPE_INT64: + case FieldDescriptorProto.Types.Type.Int64: return FieldType.Int64; - case FieldDescriptorProto.Types.Type.TYPE_UINT64: + case FieldDescriptorProto.Types.Type.Uint64: return FieldType.UInt64; - case FieldDescriptorProto.Types.Type.TYPE_INT32: + case FieldDescriptorProto.Types.Type.Int32: return FieldType.Int32; - case FieldDescriptorProto.Types.Type.TYPE_FIXED64: + case FieldDescriptorProto.Types.Type.Fixed64: return FieldType.Fixed64; - case FieldDescriptorProto.Types.Type.TYPE_FIXED32: + case FieldDescriptorProto.Types.Type.Fixed32: return FieldType.Fixed32; - case FieldDescriptorProto.Types.Type.TYPE_BOOL: + case FieldDescriptorProto.Types.Type.Bool: return FieldType.Bool; - case FieldDescriptorProto.Types.Type.TYPE_STRING: + case FieldDescriptorProto.Types.Type.String: return FieldType.String; - case FieldDescriptorProto.Types.Type.TYPE_GROUP: + case FieldDescriptorProto.Types.Type.Group: return FieldType.Group; - case FieldDescriptorProto.Types.Type.TYPE_MESSAGE: + case FieldDescriptorProto.Types.Type.Message: return FieldType.Message; - case FieldDescriptorProto.Types.Type.TYPE_BYTES: + case FieldDescriptorProto.Types.Type.Bytes: return FieldType.Bytes; - case FieldDescriptorProto.Types.Type.TYPE_UINT32: + case FieldDescriptorProto.Types.Type.Uint32: return FieldType.UInt32; - case FieldDescriptorProto.Types.Type.TYPE_ENUM: + case FieldDescriptorProto.Types.Type.Enum: return FieldType.Enum; - case FieldDescriptorProto.Types.Type.TYPE_SFIXED32: + case FieldDescriptorProto.Types.Type.Sfixed32: return FieldType.SFixed32; - case FieldDescriptorProto.Types.Type.TYPE_SFIXED64: + case FieldDescriptorProto.Types.Type.Sfixed64: return FieldType.SFixed64; - case FieldDescriptorProto.Types.Type.TYPE_SINT32: + case FieldDescriptorProto.Types.Type.Sint32: return FieldType.SInt32; - case FieldDescriptorProto.Types.Type.TYPE_SINT64: + case FieldDescriptorProto.Types.Type.Sint64: return FieldType.SInt64; default: throw new ArgumentException("Invalid type specified"); @@ -177,7 +177,7 @@ namespace Google.Protobuf.Reflection /// <summary> /// Returns <c>true</c> if this field is a repeated field; <c>false</c> otherwise. /// </summary> - public bool IsRepeated => Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; + public bool IsRepeated => Proto.Label == FieldDescriptorProto.Types.Label.Repeated; /// <summary> /// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise. diff --git a/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs new file mode 100644 index 00000000..27f9ab98 --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs @@ -0,0 +1,58 @@ +#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.Reflection +{ + /// <summary> + /// Specifies the original name (in the .proto file) of a named element, + /// such as an enum value. + /// </summary> + [AttributeUsage(AttributeTargets.Field)] + public class OriginalNameAttribute : Attribute + { + /// <summary> + /// The name of the element in the .proto file. + /// </summary> + public string Name { get; set; } + + /// <summary> + /// Constructs a new attribute instance for the given name. + /// </summary> + /// <param name="name">The name of the element in the .proto file.</param> + public OriginalNameAttribute(string name) + { + Name = ProtoPreconditions.CheckNotNull(name, nameof(name)); + } + } +}
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs index de7aea3a..e568a2c9 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs @@ -185,7 +185,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "syntax" field.</summary> public const int SyntaxFieldNumber = 7; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; /// <summary> /// The source syntax of the service. /// </summary> @@ -225,7 +225,7 @@ namespace Google.Protobuf.WellKnownTypes { if (Version.Length != 0) hash ^= Version.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); hash ^= mixins_.GetHashCode(); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) hash ^= Syntax.GetHashCode(); + if (Syntax != 0) hash ^= Syntax.GetHashCode(); return hash; } @@ -249,7 +249,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteMessage(SourceContext); } mixins_.WriteTo(output, _repeated_mixins_codec); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { output.WriteRawTag(56); output.WriteEnum((int) Syntax); } @@ -269,7 +269,7 @@ namespace Google.Protobuf.WellKnownTypes { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } size += mixins_.CalculateSize(_repeated_mixins_codec); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } return size; @@ -294,7 +294,7 @@ namespace Google.Protobuf.WellKnownTypes { SourceContext.MergeFrom(other.SourceContext); } mixins_.Add(other.mixins_); - if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (other.Syntax != 0) { Syntax = other.Syntax; } } @@ -458,7 +458,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "syntax" field.</summary> public const int SyntaxFieldNumber = 7; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; /// <summary> /// The source syntax of this method. /// </summary> @@ -498,7 +498,7 @@ namespace Google.Protobuf.WellKnownTypes { if (ResponseTypeUrl.Length != 0) hash ^= ResponseTypeUrl.GetHashCode(); if (ResponseStreaming != false) hash ^= ResponseStreaming.GetHashCode(); hash ^= options_.GetHashCode(); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) hash ^= Syntax.GetHashCode(); + if (Syntax != 0) hash ^= Syntax.GetHashCode(); return hash; } @@ -528,7 +528,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteBool(ResponseStreaming); } options_.WriteTo(output, _repeated_options_codec); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { output.WriteRawTag(56); output.WriteEnum((int) Syntax); } @@ -552,7 +552,7 @@ namespace Google.Protobuf.WellKnownTypes { size += 1 + 1; } size += options_.CalculateSize(_repeated_options_codec); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } return size; @@ -578,7 +578,7 @@ namespace Google.Protobuf.WellKnownTypes { ResponseStreaming = other.ResponseStreaming; } options_.Add(other.options_); - if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (other.Syntax != 0) { Syntax = other.Syntax; } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs index 8e2ce8cf..c9da30d5 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs @@ -59,7 +59,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary> /// Null value. /// </summary> - NULL_VALUE = 0, + [pbr::OriginalName("NULL_VALUE")] NullValue = 0, } #endregion @@ -234,7 +234,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Represents a null value. /// </summary> public global::Google.Protobuf.WellKnownTypes.NullValue NullValue { - get { return kindCase_ == KindOneofCase.NullValue ? (global::Google.Protobuf.WellKnownTypes.NullValue) kind_ : global::Google.Protobuf.WellKnownTypes.NullValue.NULL_VALUE; } + get { return kindCase_ == KindOneofCase.NullValue ? (global::Google.Protobuf.WellKnownTypes.NullValue) kind_ : 0; } set { kind_ = value; kindCase_ = KindOneofCase.NullValue; diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs index 8faa1d1c..657c2464 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs @@ -79,11 +79,11 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary> /// Syntax `proto2`. /// </summary> - SYNTAX_PROTO2 = 0, + [pbr::OriginalName("SYNTAX_PROTO2")] Proto2 = 0, /// <summary> /// Syntax `proto3`. /// </summary> - SYNTAX_PROTO3 = 1, + [pbr::OriginalName("SYNTAX_PROTO3")] Proto3 = 1, } #endregion @@ -188,7 +188,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "syntax" field.</summary> public const int SyntaxFieldNumber = 6; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; /// <summary> /// The source syntax. /// </summary> @@ -226,7 +226,7 @@ namespace Google.Protobuf.WellKnownTypes { hash ^= oneofs_.GetHashCode(); hash ^= options_.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) hash ^= Syntax.GetHashCode(); + if (Syntax != 0) hash ^= Syntax.GetHashCode(); return hash; } @@ -246,7 +246,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteRawTag(42); output.WriteMessage(SourceContext); } - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { output.WriteRawTag(48); output.WriteEnum((int) Syntax); } @@ -263,7 +263,7 @@ namespace Google.Protobuf.WellKnownTypes { if (sourceContext_ != null) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } return size; @@ -285,7 +285,7 @@ namespace Google.Protobuf.WellKnownTypes { } SourceContext.MergeFrom(other.SourceContext); } - if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (other.Syntax != 0) { Syntax = other.Syntax; } } @@ -371,7 +371,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "kind" field.</summary> public const int KindFieldNumber = 1; - private global::Google.Protobuf.WellKnownTypes.Field.Types.Kind kind_ = global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TYPE_UNKNOWN; + private global::Google.Protobuf.WellKnownTypes.Field.Types.Kind kind_ = 0; /// <summary> /// The field type. /// </summary> @@ -384,7 +384,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "cardinality" field.</summary> public const int CardinalityFieldNumber = 2; - private global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality cardinality_ = global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.CARDINALITY_UNKNOWN; + private global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality cardinality_ = 0; /// <summary> /// The field cardinality. /// </summary> @@ -526,8 +526,8 @@ namespace Google.Protobuf.WellKnownTypes { public override int GetHashCode() { int hash = 1; - if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TYPE_UNKNOWN) hash ^= Kind.GetHashCode(); - if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.CARDINALITY_UNKNOWN) hash ^= Cardinality.GetHashCode(); + if (Kind != 0) hash ^= Kind.GetHashCode(); + if (Cardinality != 0) hash ^= Cardinality.GetHashCode(); if (Number != 0) hash ^= Number.GetHashCode(); if (Name.Length != 0) hash ^= Name.GetHashCode(); if (TypeUrl.Length != 0) hash ^= TypeUrl.GetHashCode(); @@ -544,11 +544,11 @@ namespace Google.Protobuf.WellKnownTypes { } public void WriteTo(pb::CodedOutputStream output) { - if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TYPE_UNKNOWN) { + if (Kind != 0) { output.WriteRawTag(8); output.WriteEnum((int) Kind); } - if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.CARDINALITY_UNKNOWN) { + if (Cardinality != 0) { output.WriteRawTag(16); output.WriteEnum((int) Cardinality); } @@ -585,10 +585,10 @@ namespace Google.Protobuf.WellKnownTypes { public int CalculateSize() { int size = 0; - if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TYPE_UNKNOWN) { + if (Kind != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Kind); } - if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.CARDINALITY_UNKNOWN) { + if (Cardinality != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Cardinality); } if (Number != 0) { @@ -620,10 +620,10 @@ namespace Google.Protobuf.WellKnownTypes { if (other == null) { return; } - if (other.Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TYPE_UNKNOWN) { + if (other.Kind != 0) { Kind = other.Kind; } - if (other.Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.CARDINALITY_UNKNOWN) { + if (other.Cardinality != 0) { Cardinality = other.Cardinality; } if (other.Number != 0) { @@ -712,79 +712,79 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary> /// Field type unknown. /// </summary> - TYPE_UNKNOWN = 0, + [pbr::OriginalName("TYPE_UNKNOWN")] TypeUnknown = 0, /// <summary> /// Field type double. /// </summary> - TYPE_DOUBLE = 1, + [pbr::OriginalName("TYPE_DOUBLE")] TypeDouble = 1, /// <summary> /// Field type float. /// </summary> - TYPE_FLOAT = 2, + [pbr::OriginalName("TYPE_FLOAT")] TypeFloat = 2, /// <summary> /// Field type int64. /// </summary> - TYPE_INT64 = 3, + [pbr::OriginalName("TYPE_INT64")] TypeInt64 = 3, /// <summary> /// Field type uint64. /// </summary> - TYPE_UINT64 = 4, + [pbr::OriginalName("TYPE_UINT64")] TypeUint64 = 4, /// <summary> /// Field type int32. /// </summary> - TYPE_INT32 = 5, + [pbr::OriginalName("TYPE_INT32")] TypeInt32 = 5, /// <summary> /// Field type fixed64. /// </summary> - TYPE_FIXED64 = 6, + [pbr::OriginalName("TYPE_FIXED64")] TypeFixed64 = 6, /// <summary> /// Field type fixed32. /// </summary> - TYPE_FIXED32 = 7, + [pbr::OriginalName("TYPE_FIXED32")] TypeFixed32 = 7, /// <summary> /// Field type bool. /// </summary> - TYPE_BOOL = 8, + [pbr::OriginalName("TYPE_BOOL")] TypeBool = 8, /// <summary> /// Field type string. /// </summary> - TYPE_STRING = 9, + [pbr::OriginalName("TYPE_STRING")] TypeString = 9, /// <summary> /// Field type group. Proto2 syntax only, and deprecated. /// </summary> - TYPE_GROUP = 10, + [pbr::OriginalName("TYPE_GROUP")] TypeGroup = 10, /// <summary> /// Field type message. /// </summary> - TYPE_MESSAGE = 11, + [pbr::OriginalName("TYPE_MESSAGE")] TypeMessage = 11, /// <summary> /// Field type bytes. /// </summary> - TYPE_BYTES = 12, + [pbr::OriginalName("TYPE_BYTES")] TypeBytes = 12, /// <summary> /// Field type uint32. /// </summary> - TYPE_UINT32 = 13, + [pbr::OriginalName("TYPE_UINT32")] TypeUint32 = 13, /// <summary> /// Field type enum. /// </summary> - TYPE_ENUM = 14, + [pbr::OriginalName("TYPE_ENUM")] TypeEnum = 14, /// <summary> /// Field type sfixed32. /// </summary> - TYPE_SFIXED32 = 15, + [pbr::OriginalName("TYPE_SFIXED32")] TypeSfixed32 = 15, /// <summary> /// Field type sfixed64. /// </summary> - TYPE_SFIXED64 = 16, + [pbr::OriginalName("TYPE_SFIXED64")] TypeSfixed64 = 16, /// <summary> /// Field type sint32. /// </summary> - TYPE_SINT32 = 17, + [pbr::OriginalName("TYPE_SINT32")] TypeSint32 = 17, /// <summary> /// Field type sint64. /// </summary> - TYPE_SINT64 = 18, + [pbr::OriginalName("TYPE_SINT64")] TypeSint64 = 18, } /// <summary> @@ -794,19 +794,19 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary> /// For fields with unknown cardinality. /// </summary> - CARDINALITY_UNKNOWN = 0, + [pbr::OriginalName("CARDINALITY_UNKNOWN")] Unknown = 0, /// <summary> /// For optional fields. /// </summary> - CARDINALITY_OPTIONAL = 1, + [pbr::OriginalName("CARDINALITY_OPTIONAL")] Optional = 1, /// <summary> /// For required fields. Proto2 syntax only. /// </summary> - CARDINALITY_REQUIRED = 2, + [pbr::OriginalName("CARDINALITY_REQUIRED")] Required = 2, /// <summary> /// For repeated fields. /// </summary> - CARDINALITY_REPEATED = 3, + [pbr::OriginalName("CARDINALITY_REPEATED")] Repeated = 3, } } @@ -900,7 +900,7 @@ namespace Google.Protobuf.WellKnownTypes { /// <summary>Field number for the "syntax" field.</summary> public const int SyntaxFieldNumber = 5; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; /// <summary> /// The source syntax. /// </summary> @@ -936,7 +936,7 @@ namespace Google.Protobuf.WellKnownTypes { hash ^= enumvalue_.GetHashCode(); hash ^= options_.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) hash ^= Syntax.GetHashCode(); + if (Syntax != 0) hash ^= Syntax.GetHashCode(); return hash; } @@ -955,7 +955,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteRawTag(34); output.WriteMessage(SourceContext); } - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { output.WriteRawTag(40); output.WriteEnum((int) Syntax); } @@ -971,7 +971,7 @@ namespace Google.Protobuf.WellKnownTypes { if (sourceContext_ != null) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } - if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (Syntax != 0) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } return size; @@ -992,7 +992,7 @@ namespace Google.Protobuf.WellKnownTypes { } SourceContext.MergeFrom(other.SourceContext); } - if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.SYNTAX_PROTO2) { + if (other.Syntax != 0) { Syntax = other.Syntax; } } |