public static ZEnumValue From(this EnumValueDescriptorProto proto) { var name = proto.Name; var index = proto.Number; return(new ZEnumValue(name, index)); }
/// <summary> /// Write an enum value /// </summary> protected override void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto obj, ref object state) { // var name = ctx.NameNormalizer.GetName(obj); string name = obj.Name; WriteOptions(ctx, obj.Options); ctx.WriteLine($"{Escape(name)} = {obj.Number},"); }
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); }
/// <summary> /// Suggest a normalized identifier /// </summary> public virtual string GetName(EnumValueDescriptorProto definition) { var name = definition?.Options?.GetOptions()?.Name; if (!string.IsNullOrWhiteSpace(name)) { return(name); } return(AutoCapitalize(definition.Name)); }
/// <summary> /// Write an enum value /// </summary> protected override void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto obj, ref object state) { ctx.WriteLine($"{Escape(obj.Name)} = {obj.Number},"); foreach (var prefix in EnumPrefixes) { if (obj.Name.StartsWith(prefix)) { ctx.WriteLine($"{Escape(obj.Name.Substring(prefix.Length))} = {obj.Number},"); } } }
/// <summary> /// Write an enum value /// </summary> protected override void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto @enum, ref object state) { var name = ctx.NameNormalizer.GetName(@enum); if (name != @enum.Name) { var tw = ctx.Write("[global::ProtoBuf.ProtoEnum("); tw.Write($@"Name = @""{@enum.Name}"""); tw.WriteLine(")]"); } WriteOptions(ctx, @enum.Options); ctx.WriteLine($"{Escape(name)} = {@enum.Number},"); }
/// <summary> /// Write an enum value /// </summary> protected override void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto obj, ref object state) { var name = ctx.NameNormalizer.GetName(obj); if (name != obj.Name) { var tw = ctx.Write($@"<Global.ProtoBuf.ProtoEnum("); tw.Write($@"Name := ""{obj.Name}"""); tw.WriteLine(")> _"); } WriteOptions(ctx, obj.Options); ctx.WriteLine($"{Escape(name)} = {obj.Number}"); }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, EnumValueDescriptorProto instance) { if (instance.Name != null) { // Key for field: 1, LengthDelimited stream.WriteByte(10); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name)); } // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Number); if (instance.Options != null) { // Key for field: 3, LengthDelimited stream.WriteByte(26); using (var ms3 = new MemoryStream()) { Google.protobuf.EnumValueOptions.Serialize(ms3, instance.Options); // Length delimited byte array uint ms3Length = (uint)ms3.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms3Length); stream.Write(ms3.GetBuffer(), 0, (int)ms3Length); } } }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumValueDescriptorProto DeserializeLengthDelimited(Stream stream) { EnumValueDescriptorProto instance = new EnumValueDescriptorProto(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumValueDescriptorProto DeserializeLength(Stream stream, int length) { EnumValueDescriptorProto instance = new EnumValueDescriptorProto(); DeserializeLength(stream, length, instance); return instance; }
public EnumValueDescriptor(EnumDescriptor d, EnumValueDescriptorProto def) { Define = def; _d = d; }
/// <summary> /// Write a field /// </summary> protected override void WriteField(GeneratorContext ctx, FieldDescriptorProto obj, ref object state, OneOfStub[] oneOfs) { var name = ctx.NameNormalizer.GetName(obj); var tw = ctx.Write($@"[global::ProtoBuf.ProtoMember({obj.Number}"); if (name != obj.Name) { tw.Write($@", Name = @""{obj.Name}"""); } var options = obj.Options?.GetOptions(); if (options?.AsReference == true) { tw.Write($@", AsReference = true"); } if (options?.DynamicType == true) { tw.Write($@", DynamicType = true"); } bool isOptional = obj.label == FieldDescriptorProto.Label.LabelOptional; bool isRepeated = obj.label == FieldDescriptorProto.Label.LabelRepeated; OneOfStub oneOf = obj.ShouldSerializeOneofIndex() ? oneOfs?[obj.OneofIndex] : null; if (oneOf != null && oneOf.CountTotal == 1) { oneOf = null; // not really a one-of, then! } bool explicitValues = isOptional && oneOf == null && ctx.Syntax == FileDescriptorProto.SyntaxProto2 && obj.type != FieldDescriptorProto.Type.TypeMessage && obj.type != FieldDescriptorProto.Type.TypeGroup; string defaultValue = null; bool suppressDefaultAttribute = !isOptional; if (isOptional || obj.type == FieldDescriptorProto.Type.TypeEnum) { defaultValue = obj.DefaultValue; if (obj.type == FieldDescriptorProto.Type.TypeString) { defaultValue = string.IsNullOrEmpty(defaultValue) ? "\"\"" : ("@\"" + (defaultValue ?? "").Replace("\"", "\"\"") + "\""); } else if (obj.type == FieldDescriptorProto.Type.TypeDouble) { switch (defaultValue) { case "inf": defaultValue = "double.PositiveInfinity"; break; case "-inf": defaultValue = "double.NegativeInfinity"; break; case "nan": defaultValue = "double.NaN"; break; } } else if (obj.type == FieldDescriptorProto.Type.TypeFloat) { switch (defaultValue) { case "inf": defaultValue = "float.PositiveInfinity"; break; case "-inf": defaultValue = "float.NegativeInfinity"; break; case "nan": defaultValue = "float.NaN"; break; } } else if (obj.type == FieldDescriptorProto.Type.TypeEnum) { var enumType = ctx.TryFind <EnumDescriptorProto>(obj.TypeName); if (enumType != null) { EnumValueDescriptorProto found = null; if (!string.IsNullOrEmpty(defaultValue)) { found = enumType.Values.FirstOrDefault(x => x.Name == defaultValue); } else if (ctx.Syntax == FileDescriptorProto.SyntaxProto2) { // find the first one; if that is a zero, we don't need it after all found = enumType.Values.FirstOrDefault(); if (found != null && found.Number == 0) { if (!isOptional) { found = null; // we don't need it after all } } } // for proto3 the default is 0, so no need to do anything - GetValueOrDefault() will do it all if (found != null) { defaultValue = ctx.NameNormalizer.GetName(found); } if (!string.IsNullOrWhiteSpace(defaultValue)) { defaultValue = ctx.NameNormalizer.GetName(enumType) + "." + defaultValue; } } } } var typeName = GetTypeName(ctx, obj, out var dataFormat, out var isMap); if (!string.IsNullOrWhiteSpace(dataFormat)) { tw.Write($", DataFormat = global::ProtoBuf.DataFormat.{dataFormat}"); } if (obj.IsPacked(ctx.Syntax)) { tw.Write($", IsPacked = true"); } if (obj.label == FieldDescriptorProto.Label.LabelRequired) { tw.Write($", IsRequired = true"); } tw.WriteLine(")]"); if (!isRepeated && !string.IsNullOrWhiteSpace(defaultValue) && !suppressDefaultAttribute) { ctx.WriteLine($"[global::System.ComponentModel.DefaultValue({defaultValue})]"); } WriteOptions(ctx, obj.Options); if (isRepeated) { var mapMsgType = isMap ? ctx.TryFind <DescriptorProto>(obj.TypeName) : null; if (mapMsgType != null) { var keyTypeName = GetTypeName(ctx, mapMsgType.Fields.Single(x => x.Number == 1), out var keyDataFormat, out var _); var valueTypeName = GetTypeName(ctx, mapMsgType.Fields.Single(x => x.Number == 2), out var valueDataFormat, out var _); bool first = true; tw = ctx.Write($"[global::ProtoBuf.ProtoMap"); if (!string.IsNullOrWhiteSpace(keyDataFormat)) { tw.Write($"{(first ? "(" : ", ")}KeyFormat = global::ProtoBuf.DataFormat.{keyDataFormat}"); first = false; } if (!string.IsNullOrWhiteSpace(valueDataFormat)) { tw.Write($"{(first ? "(" : ", ")}ValueFormat = global::ProtoBuf.DataFormat.{valueDataFormat}"); first = false; } tw.WriteLine(first ? "]" : ")]"); ctx.WriteLine($"{GetAccess(GetAccess(obj))} global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}> {Escape(name)} {{ get; }} = new global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}>();"); } else if (UseArray(obj)) { ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName}[] {Escape(name)} {{ get; set; }}"); } else { ctx.WriteLine($"{GetAccess(GetAccess(obj))} global::System.Collections.Generic.List<{typeName}> {Escape(name)} {{ get; }} = new global::System.Collections.Generic.List<{typeName}>();"); } } else if (oneOf != null) { var defValue = string.IsNullOrWhiteSpace(defaultValue) ? $"default({typeName})" : defaultValue; var fieldName = FieldPrefix + oneOf.OneOf.Name; var storage = oneOf.GetStorage(obj.type, obj.TypeName); ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName} {Escape(name)}").WriteLine("{").Indent(); switch (obj.type) { case FieldDescriptorProto.Type.TypeMessage: case FieldDescriptorProto.Type.TypeGroup: case FieldDescriptorProto.Type.TypeEnum: case FieldDescriptorProto.Type.TypeBytes: case FieldDescriptorProto.Type.TypeString: ctx.WriteLine($"get {{ return {fieldName}.Is({obj.Number}) ? (({typeName}){fieldName}.{storage}) : {defValue}; }}"); break; default: ctx.WriteLine($"get {{ return {fieldName}.Is({obj.Number}) ? {fieldName}.{storage} : {defValue}; }}"); break; } var unionType = oneOf.GetUnionType(); ctx.WriteLine($"set {{ {fieldName} = new global::ProtoBuf.{unionType}({obj.Number}, value); }}") .Outdent().WriteLine("}") .WriteLine($"{GetAccess(GetAccess(obj))} bool ShouldSerialize{name}() => {fieldName}.Is({obj.Number});") .WriteLine($"{GetAccess(GetAccess(obj))} void Reset{name}() => global::ProtoBuf.{unionType}.Reset(ref {fieldName}, {obj.Number});"); if (oneOf.IsFirst()) { ctx.WriteLine().WriteLine($"private global::ProtoBuf.{unionType} {fieldName};"); } } else if (explicitValues) { string fieldName = FieldPrefix + name, fieldType; bool isRef = false; switch (obj.type) { case FieldDescriptorProto.Type.TypeString: case FieldDescriptorProto.Type.TypeBytes: fieldType = typeName; isRef = true; break; default: fieldType = typeName + "?"; break; } ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName} {Escape(name)}").WriteLine("{").Indent(); tw = ctx.Write($"get {{ return {fieldName}"); if (!string.IsNullOrWhiteSpace(defaultValue)) { tw.Write(" ?? "); tw.Write(defaultValue); } else if (!isRef) { tw.Write(".GetValueOrDefault()"); } tw.WriteLine("; }"); ctx.WriteLine($"set {{ {fieldName} = value; }}") .Outdent().WriteLine("}") .WriteLine($"{GetAccess(GetAccess(obj))} bool ShouldSerialize{name}() => {fieldName} != null;") .WriteLine($"{GetAccess(GetAccess(obj))} void Reset{name}() => {fieldName} = null;") .WriteLine($"private {fieldType} {fieldName};"); } else { tw = ctx.Write($"{GetAccess(GetAccess(obj))} {typeName} {Escape(name)} {{ get; set; }}"); if (!string.IsNullOrWhiteSpace(defaultValue)) { tw.Write($" = {defaultValue};"); } tw.WriteLine(); } ctx.WriteLine(); }
protected override void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto obj, ref object state) { throw new NotImplementedException(); }
/// <summary>Helper: Serialize with a varint length prefix</summary> public static void SerializeLengthDelimited(Stream stream, EnumValueDescriptorProto instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(EnumValueDescriptorProto instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary> /// Emit code representing an enum value /// </summary> protected abstract void WriteEnumValue(GeneratorContext ctx, EnumValueDescriptorProto obj, ref object state);
protected string GetDefaltValue(GeneratorContext ctx, FieldDescriptorProto obj, string _typename) { string defaultValue = obj.DefaultValue; if (obj.type == FieldDescriptorProto.Type.TypeString) { if (!string.IsNullOrEmpty(defaultValue)) { defaultValue = '"' + defaultValue + '"'; } } else if (obj.type == FieldDescriptorProto.Type.TypeDouble) { switch (defaultValue) { case "inf": defaultValue = "double.PositiveInfinity"; break; case "-inf": defaultValue = "double.NegativeInfinity"; break; case "nan": defaultValue = "double.NaN"; break; default: if (!string.IsNullOrEmpty(defaultValue) && !defaultValue.EndsWith("f")) { defaultValue += "f"; } break; } } else if (obj.type == FieldDescriptorProto.Type.TypeFloat) { switch (defaultValue) { case "inf": defaultValue = "float.PositiveInfinity"; break; case "-inf": defaultValue = "float.NegativeInfinity"; break; case "nan": defaultValue = "float.NaN"; break; default: if (!string.IsNullOrEmpty(defaultValue) && !defaultValue.EndsWith("f")) { defaultValue += "f"; } break; } } else if (obj.type == FieldDescriptorProto.Type.TypeMessage) { defaultValue = $"new {_typename}()"; } else if (obj.type == FieldDescriptorProto.Type.TypeEnum) { var enumType = ctx.TryFind <EnumDescriptorProto>(obj.TypeName); if (enumType != null) { EnumValueDescriptorProto found = null; if (!string.IsNullOrEmpty(defaultValue)) { found = enumType.Values.FirstOrDefault(x => x.Name == defaultValue); } else if (ctx.Syntax == FileDescriptorProto.SyntaxProto2) { found = enumType.Values.FirstOrDefault(); } if (found != null) { // defaultValue = ctx.NameNormalizer.GetName(found); defaultValue = found.Name; } if (!string.IsNullOrEmpty(defaultValue)) { var typeName = GetTypeName(ctx, obj, out var dataFormat, out var isMap); defaultValue = typeName + "." + defaultValue; } } } return(defaultValue); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumValueDescriptorProto Deserialize(Stream stream) { var instance = new EnumValueDescriptorProto(); Deserialize(stream, instance); return instance; }
private string GetDefaultValue(GeneratorContext ctx, FieldDescriptorProto obj, string typeName) { string defaultValue = null; bool isOptional = obj.label == FieldDescriptorProto.Label.LabelOptional; if (isOptional || ctx.EmitRequiredDefaults || obj.type == FieldDescriptorProto.Type.TypeEnum) { defaultValue = obj.DefaultValue; if (obj.type == FieldDescriptorProto.Type.TypeString) { defaultValue = string.IsNullOrEmpty(defaultValue) ? "\"\"" : ("@\"" + (defaultValue ?? "").Replace("\"", "\"\"") + "\""); } else if (obj.type == FieldDescriptorProto.Type.TypeDouble) { switch (defaultValue) { case "inf": defaultValue = "double.PositiveInfinity"; break; case "-inf": defaultValue = "double.NegativeInfinity"; break; case "nan": defaultValue = "double.NaN"; break; } } else if (obj.type == FieldDescriptorProto.Type.TypeFloat) { switch (defaultValue) { case "inf": defaultValue = "float.PositiveInfinity"; break; case "-inf": defaultValue = "float.NegativeInfinity"; break; case "nan": defaultValue = "float.NaN"; break; } } else if (obj.type == FieldDescriptorProto.Type.TypeEnum) { var enumType = ctx.TryFind <EnumDescriptorProto>(obj.TypeName); if (enumType != null) { EnumValueDescriptorProto found = null; if (!string.IsNullOrEmpty(defaultValue)) { found = enumType.Values.Find(x => x.Name == defaultValue); } else if (ctx.Syntax == FileDescriptorProto.SyntaxProto2) { // find the first one; if that is a zero, we don't need it after all found = enumType.Values.FirstOrDefault(); if (found != null && found.Number == 0) { if (!isOptional) { found = null; // we don't need it after all } } } // for proto3 the default is 0, so no need to do anything - GetValueOrDefault() will do it all if (found != null) { defaultValue = ctx.NameNormalizer.GetName(found); } if (!string.IsNullOrWhiteSpace(defaultValue)) { defaultValue = typeName + "." + defaultValue; } } } } return(defaultValue); }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, EnumValueDescriptorProto instance) { var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); if (instance.Name != null) { // Key for field: 1, LengthDelimited stream.WriteByte(10); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name)); } // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Number); if (instance.Options != null) { // Key for field: 3, LengthDelimited stream.WriteByte(26); msField.SetLength(0); Google.Protobuf.EnumValueOptions.Serialize(msField, instance.Options); // Length delimited byte array uint length3 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length3); msField.WriteTo(stream); } global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> public static EnumValueDescriptorProto Deserialize(byte[] buffer) { EnumValueDescriptorProto instance = new EnumValueDescriptorProto(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
/// <summary> /// Constructs Delphi source code representing a protobuf enum value. /// </summary> /// <param name="prefix">Optional prefix to the generated identifier</param> /// <param name="enumValue">Protobuf enum value to generate code for</param> public EnumValueSourceCode(string?prefix, EnumValueDescriptorProto enumValue) { this.prefix = prefix; this.enumValue = enumValue; }