/// <summary> /// Start a message /// </summary> protected override void WriteMessageHeader(GeneratorContext ctx, DescriptorProto 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($"Partial {GetAccess(GetAccess(obj))} Class {Escape(name)}"); ctx.Indent().WriteLine("Implements Global.ProtoBuf.IExtensible").Outdent(); ctx.Indent(); if (obj.Options?.MessageSetWireFormat == true) { ctx.WriteLine("REM #error message_set_wire_format is not currently implemented").WriteLine(); } ctx.WriteLine($"Private {FieldPrefix}extensionData As Global.ProtoBuf.IExtension") .WriteLine($"Private Function GetExtensionObject(ByVal createIfMissing As Boolean) As IExtension Implements IExtensible.GetExtensionObject") .Indent().WriteLine($"Return Extensible.GetExtensionObject({FieldPrefix}extensionData, createIfMissing)") .Outdent().WriteLine("End Function"); }
/// <summary> /// Start a message /// </summary> protected override void WriteMessageHeader(GeneratorContext ctx, DescriptorProto message, ref object state) { var name = ctx.NameNormalizer.GetName(message); var tw = ctx.Write("[global::ProtoBuf.ProtoContract("); if (name != message.Name) { tw.Write($@"Name = @""{message.Name}"""); } tw.WriteLine(")]"); WriteOptions(ctx, message.Options); tw = ctx.Write($"{GetAccess(GetAccess(message))} partial class {Escape(name)}"); tw.Write(" : global::ProtoBuf.IExtensible"); tw.WriteLine(); ctx.WriteLine("{").Indent(); if (message.Options?.MessageSetWireFormat == true) { ctx.WriteLine("#error message_set_wire_format is not currently implemented").WriteLine(); } ctx.WriteLine($"private global::ProtoBuf.IExtension {FieldPrefix}extensionData;") .WriteLine($"global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)"); if (ctx.Supports(CSharp6)) { ctx.Indent().WriteLine($"=> global::ProtoBuf.Extensible.GetExtensionObject(ref {FieldPrefix}extensionData, createIfMissing);").Outdent().WriteLine(); } else { ctx.WriteLine("{").Indent().WriteLine($"return global::ProtoBuf.Extensible.GetExtensionObject(ref {FieldPrefix}extensionData, createIfMissing);").Outdent().WriteLine("}"); } }
/// <summary> /// Write an extension /// </summary> protected override void WriteExtension(GeneratorContext ctx, FieldDescriptorProto field) { string dataFormat; bool isMap; var type = GetTypeName(ctx, field, out dataFormat, out isMap); if (isMap) { ctx.WriteLine("#error map extensions not yet implemented"); } else if (field.label == FieldDescriptorProto.Label.LabelRepeated) { ctx.WriteLine("#error repeated extensions not yet implemented"); } else { var msg = ctx.TryFind <DescriptorProto>(field.Extendee); var extendee = MakeRelativeName(field, msg, ctx.NameNormalizer); var @this = field.Parent is FileDescriptorProto ? "this " : ""; string name = ctx.NameNormalizer.GetName(field); ctx.WriteLine($"{GetAccess(GetAccess(field))} static {type} Get{name}({@this}{extendee} obj)"); TextWriter tw; if (ctx.Supports(CSharp6)) { tw = ctx.Indent().Write($"=> "); } else { ctx.WriteLine("{").Indent(); tw = ctx.Write("return "); } tw.Write($"obj == null ? default({type}) : global::ProtoBuf.Extensible.GetValue<{type}>(obj, {field.Number}"); if (!string.IsNullOrEmpty(dataFormat)) { tw.Write($", global::ProtoBuf.DataFormat.{dataFormat}"); } tw.WriteLine(");"); if (ctx.Supports(CSharp6)) { ctx.Outdent().WriteLine(); } else { ctx.Outdent().WriteLine("}").WriteLine(); } // GetValue<TValue>(IExtensible instance, int tag, DataFormat format) } }
/// <summary> /// Write an extension /// </summary> protected override void WriteExtension(GeneratorContext ctx, FieldDescriptorProto field) { var type = GetTypeName(ctx, field, out string dataFormat, out bool isMap); if (isMap) { ctx.WriteLine("REM #error map extensions not yet implemented; please file an issue"); } else if (field.label == FieldDescriptorProto.Label.LabelRepeated) { ctx.WriteLine("REM #error repeated extensions not yet implemented; please file an issue"); } else { var msg = ctx.TryFind <DescriptorProto>(field.Extendee); var extendee = MakeRelativeName(field, msg, ctx.NameNormalizer); string name = ctx.NameNormalizer.GetName(field); string shared = ""; if (field.Parent is FileDescriptorProto) { ctx.WriteLine("<Global.System.Runtime.CompilerServices.Extension> _"); } else { shared = "Shared "; } ctx.WriteLine($"{GetAccess(GetAccess(field))} {shared}Function Get{name}(ByVal obj As {extendee}) As {type}"); var tw = ctx.Indent().Write($"Return If(obj Is Nothing, CType(Nothing, {type}), Global.ProtoBuf.Extensible.GetValue(Of {type})(obj, {field.Number}"); if (!string.IsNullOrEmpty(dataFormat)) { tw.Write($", Global.ProtoBuf.DataFormat.{dataFormat}"); } tw.WriteLine("))"); ctx.Outdent().WriteLine("End Function"); } }
/// <summary> /// Write an extension /// </summary> protected override void WriteExtension(GeneratorContext ctx, FieldDescriptorProto field) { var type = GetTypeName(ctx, field, out string dataFormat, out bool isMap, false); var nonNullableType = GetTypeName(ctx, field, out _, out _, true); var msg = ctx.TryFind <DescriptorProto>(field.Extendee); var extendee = MakeRelativeName(field, msg, ctx.NameNormalizer); var @this = field.Parent is FileDescriptorProto ? "this " : ""; string name = ctx.NameNormalizer.GetName(field); TextWriter tw; if (isMap) { ctx.WriteLine("#error map extensions not yet implemented; please file an issue"); } else { bool isRepeated = field.label == FieldDescriptorProto.Label.LabelRepeated; var getMethodName = isRepeated ? nameof(Extensible.GetValues) : nameof(Extensible.GetValue); if (isRepeated) { ctx.WriteLine($"{GetAccess(GetAccess(field))} static global::System.Collections.Generic.IEnumerable<{nonNullableType}> Get{name}({@this}{extendee} obj)"); } else { ctx.WriteLine($"{GetAccess(GetAccess(field))} static {type} Get{name}({@this}{extendee} obj)"); } if (ctx.Supports(CSharp6)) { tw = ctx.Indent().Write($"=> "); } else { ctx.WriteLine("{").Indent(); tw = ctx.Write("return "); } var defaultValue = isRepeated ? "null" : ctx.Supports(CSharp7_1) ? "default" : $"default({type})"; tw.Write($"obj == null ? {defaultValue} : global::ProtoBuf.Extensible.{getMethodName}<{(isRepeated ? nonNullableType : type)}>(obj, {field.Number}"); if (!string.IsNullOrEmpty(dataFormat)) { tw.Write($", global::ProtoBuf.DataFormat.{dataFormat}"); } tw.WriteLine(");"); if (ctx.Supports(CSharp6)) { ctx.Outdent().WriteLine(); } else { ctx.Outdent().WriteLine("}").WriteLine(); } var setAccessorName = isRepeated ? "Add" : "Set"; ctx.WriteLine($"{GetAccess(GetAccess(field))} static void {setAccessorName}{name}({@this}{extendee} obj, {nonNullableType} value)"); if (ctx.Supports(CSharp6)) { tw = ctx.Indent().Write($"=> "); } else { ctx.WriteLine("{").Indent(); tw = ctx.Write(""); } tw.Write($"global::ProtoBuf.Extensible.AppendValue<{nonNullableType}>(obj, {field.Number}"); if (!string.IsNullOrEmpty(dataFormat)) { tw.Write($", global::ProtoBuf.DataFormat.{dataFormat}"); } tw.WriteLine(", value);"); if (ctx.Supports(CSharp6)) { ctx.Outdent().WriteLine(); } else { ctx.Outdent().WriteLine("}").WriteLine(); } } }