void DumpEnumDescriptor(FileDescriptorProto source, EnumDescriptorProto field, StringBuilder sb, int level, ref bool marker) { var levelspace = new string( '\t', level ); AppendHeadingSpace(sb, ref marker); sb.AppendLine($"{levelspace}enum {field.name} {{"); foreach (var option in DumpOptions(source, field.options)) { sb.AppendLine($"{levelspace}\toption {option.Key} = {option.Value};"); } foreach (var enumValue in field.value) { var options = DumpOptions(source, enumValue.options); var parameters = string.Empty; if (options.Count > 0) { parameters = $" [{string.Join( ", ", options.Select( kvp => $"{kvp.Key} = {kvp.Value}" ) )}]"; } sb.AppendLine($"{levelspace}\t{enumValue.name} = {enumValue.number}{parameters};"); } sb.AppendLine($"{levelspace}}}"); marker = true; }
private void DumpEnumDescriptor(EnumDescriptorProto field, StringBuilder sb, int level) { AddEnumDescriptorLookup(field); var levelspace = new string('\t', level); sb.AppendLine($"{levelspace}enum {field.name} {{"); foreach (var option in DumpOptions(field.options)) { sb.AppendLine($"{levelspace}\toption {option.Key} = {option.Value};"); } foreach (var enumValue in field.value) { var options = DumpOptions(enumValue.options); var parameters = string.Empty; if (options.Count > 0) { parameters = $" [{string.Join(", ", options.Select(kvp => $"{kvp.Key} = {kvp.Value}"))}]"; } sb.AppendLine($"{levelspace}\t{enumValue.name} = {enumValue.number}{parameters};"); } sb.AppendLine($"{levelspace}}}"); sb.AppendLine(); }
private void DumpEnumDescriptor(EnumDescriptorProto field, StringBuilder sb, int level) { AddEnumDescriptorLookup(field); string levelspace = new String('\t', level); sb.AppendLine(levelspace + "enum " + field.name + " {"); foreach (var option in DumpOptions(field.options)) { sb.AppendLine(levelspace + "\toption " + option.Key + " = " + option.Value + ";"); } foreach (EnumValueDescriptorProto enumValue in field.value) { Dictionary <string, string> options = DumpOptions(enumValue.options); string parameters = String.Empty; if (options.Count > 0) { parameters = " [" + String.Join(", ", options.Select(kvp => String.Format("{0} = {1}", kvp.Key, kvp.Value))) + "]"; } sb.AppendLine(levelspace + "\t" + enumValue.name + " = " + enumValue.number + parameters + ";"); } sb.AppendLine(levelspace + "}"); sb.AppendLine(); }
/// <summary> /// Start an enum /// </summary> protected override void WriteEnumHeader(GeneratorContext ctx, EnumDescriptorProto obj, ref object state) { //var name = ctx.NameNormalizer.GetName(obj); string name = obj.Name; var tw = ctx.Output; WriteOptions(ctx, obj.Options); ctx.WriteLine($"{GetAccess(GetAccess(obj))} enum {Escape(name)}").WriteLine("{").Indent(); }
/// <summary> /// Suggest a normalized identifier /// </summary> public virtual string GetName(EnumDescriptorProto definition) { var name = definition?.Options?.GetOptions()?.Name; if (!string.IsNullOrWhiteSpace(name)) { return(name); } return(GetName(definition.Parent as DescriptorProto, GetName(definition.Name), definition.Name, false)); }
/// <summary> /// Emit code representing an enum type /// </summary> protected virtual void WriteEnum(GeneratorContext ctx, EnumDescriptorProto obj) { object state = null; WriteEnumHeader(ctx, obj, ref state); foreach (var inner in obj.Values) { WriteEnumValue(ctx, inner, ref state); } WriteEnumFooter(ctx, obj, ref state); }
/// <summary> /// Start an enum /// </summary> protected override void WriteEnumHeader(GeneratorContext ctx, EnumDescriptorProto obj, ref object state) { var name = ctx.NameNormalizer.GetName(obj); var tw = ctx.Write($@"<Global.ProtoBuf.ProtoContract("); if (name != obj.Name) { tw.Write($@"Name := ""{obj.Name}"""); } tw.WriteLine(")> _"); WriteOptions(ctx, obj.Options); ctx.WriteLine($"{GetAccess(GetAccess(obj))} Enum {Escape(name)}").Indent(); }
/// <summary> /// Start an enum /// </summary> protected override void WriteEnumHeader(GeneratorContext ctx, EnumDescriptorProto @enum, ref object state) { var name = ctx.NameNormalizer.GetName(@enum); var tw = ctx.Write("[global::ProtoBuf.ProtoContract("); if (name != @enum.Name) { tw.Write($@"Name = @""{@enum.Name}"""); } tw.WriteLine(")]"); WriteOptions(ctx, @enum.Options); ctx.WriteLine($"{GetAccess(GetAccess(@enum))} enum {Escape(name)}").WriteLine("{").Indent(); }
public EnumDescriptor(DescriptorPool pool, ProtoBase parent, EnumDescriptorProto def) { Define = def; base.Init(pool, parent); for (int i = 0; i < def.value.Count; i++) { var fieldDef = def.value[i]; var fieldD = new EnumValueDescriptor(this, fieldDef); _fieldByName.Add(fieldDef.name, fieldD); _fieldByNumber.Add(fieldDef.number, fieldD); } }
public static ZEnum From(this EnumDescriptorProto proto, string scope) { // can't use Pascalize because enums are SCREAMING_CASE, and when they're formatted // as "SUCH", Pascalize will return "SUCH". so, we tolower it so Pascalize will // return "Such". var name = proto.Name; var values = new List <ZEnumValue>(); foreach (var value in proto.Values) { values.Add(value.From()); } return(new ZEnum(name, values, scope + name)); }
internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; this.clrType = clrType; 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 EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type generatedType) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; this.generatedType = generatedType; 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); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(EnumDescriptorProto instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumDescriptorProto Deserialize(Stream stream) { var instance = new EnumDescriptorProto(); Deserialize(stream, instance); return instance; }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, EnumDescriptorProto instance) { if (instance.Name != null) { // Key for field: 1, LengthDelimited stream.WriteByte(10); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name)); } if (instance.Value != null) { foreach (var i2 in instance.Value) { // Key for field: 2, LengthDelimited stream.WriteByte(18); using (var ms2 = new MemoryStream()) { Google.protobuf.EnumValueDescriptorProto.Serialize(ms2, i2); // Length delimited byte array uint ms2Length = (uint)ms2.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms2Length); stream.Write(ms2.GetBuffer(), 0, (int)ms2Length); } } } if (instance.Options != null) { // Key for field: 3, LengthDelimited stream.WriteByte(26); using (var ms3 = new MemoryStream()) { Google.protobuf.EnumOptions.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: Serialize with a varint length prefix</summary> public static void SerializeLengthDelimited(Stream stream, EnumDescriptorProto instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumDescriptorProto DeserializeLength(Stream stream, int length) { EnumDescriptorProto instance = new EnumDescriptorProto(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static EnumDescriptorProto DeserializeLengthDelimited(Stream stream) { EnumDescriptorProto instance = new EnumDescriptorProto(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> public static EnumDescriptorProto Deserialize(byte[] buffer) { EnumDescriptorProto instance = new EnumDescriptorProto(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
/// <summary> /// Obtain the access of an item, accounting for the model's hierarchy /// </summary> protected Access GetAccess(EnumDescriptorProto obj) => NullIfInherit(obj?.Options?.GetOptions()?.Access) ?? GetAccess(obj?.Parent) ?? Access.Public;
/// <summary> /// Emit code following a set of enum values /// </summary> protected abstract void WriteEnumFooter(GeneratorContext ctx, EnumDescriptorProto obj, ref object state);
/// <summary> /// Constructs Delphi source code representing a protobuf enum. /// </summary> /// <param name="enum">Protobuf enum to generate code for</param> /// <param name="schema">Protobuf schema definition that this enum is part of</param> /// <param name="containerType">Representation of the message type that this enum is nested in, absent if this is not a nested enum</param> public EnumSourceCode(EnumDescriptorProto @enum, SchemaSourceCode schema, MessageTypeSourceCode?containerType) : base(schema, containerType) => Enum = @enum;
/// <summary> /// End an enum /// </summary> protected override void WriteEnumFooter(GeneratorContext ctx, EnumDescriptorProto @enum, ref object state) { ctx.Outdent().WriteLine("}").WriteLine(); }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, EnumDescriptorProto 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)); } if (instance.Value != null) { foreach (var i2 in instance.Value) { // Key for field: 2, LengthDelimited stream.WriteByte(18); msField.SetLength(0); Google.Protobuf.EnumValueDescriptorProto.Serialize(msField, i2); // Length delimited byte array uint length2 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length2); msField.WriteTo(stream); } } if (instance.Options != null) { // Key for field: 3, LengthDelimited stream.WriteByte(26); msField.SetLength(0); Google.Protobuf.EnumOptions.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); }
protected override void WriteEnumFooter(GeneratorContext ctx, EnumDescriptorProto obj, ref object state) { throw new NotImplementedException(); }
private void AddEnumDescriptorLookup(EnumDescriptorProto enumdesc) { enumLookup[GetDescriptorName(enumdesc.name)] = enumdesc; enumLookupCount[GetDescriptorName(enumdesc.name)] = enumdesc.value.Count; }
private void AddEnum(EnumDescriptorProto enumDescriptor) { _filesBySymbol[enumDescriptor.FullyQualifiedName] = enumDescriptor.GetFile(); }