/// <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.Output; 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; var typeName = GetTypeName(ctx, obj, out var dataFormat, out var isMap); defaultValue = GetDefaltValue(ctx, obj, typeName); 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 _); ctx.WriteLine($"{GetAccess(GetAccess(obj))} System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}> {Escape(obj.Name)} = new System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}>();"); } // else if (UseArray(obj)) // { // ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName}[] {Escape(name)} ;"); // } else { ctx.WriteLine($"{GetAccess(GetAccess(obj))} System.Collections.Generic.List<{typeName}> {Escape(obj.Name)} = new System.Collections.Generic.List<{typeName}>();"); } } else { if (!string.IsNullOrEmpty(defaultValue)) { ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName} {obj.Name} = {defaultValue};"); } else { ctx.WriteLine($"{GetAccess(GetAccess(obj))} {typeName} {obj.Name} ;"); } } }
=> obj.Options?.MapEntry ?? false; // don't write this type - use a dictionary instead /// <summary> /// Emit code representing a message type /// </summary> protected virtual void WriteMessage(GeneratorContext ctx, DescriptorProto obj) { object state = null; if (ShouldOmitMessage(ctx, obj, ref state)) { return; } WriteMessageHeader(ctx, obj, ref state); var oneOfs = OneOfStub.Build(ctx, obj); if (WriteContructorHeader(ctx, obj, ref state)) { foreach (var inner in obj.Fields) { WriteInitField(ctx, inner, ref state, oneOfs); } WriteConstructorFooter(ctx, obj, ref state); } foreach (var inner in obj.Fields) { WriteField(ctx, inner, ref state, oneOfs); } if (oneOfs != null) { foreach (var stub in oneOfs) { WriteOneOf(ctx, stub); } } foreach (var inner in obj.NestedTypes) { WriteMessage(ctx, inner); } foreach (var inner in obj.EnumTypes) { WriteEnum(ctx, inner); } if (obj.Extensions.Count != 0) { object extState = null; WriteExtensionsHeader(ctx, obj, ref extState); foreach (var ext in obj.Extensions) { WriteExtension(ctx, ext); } WriteExtensionsFooter(ctx, obj, ref extState); } WriteMessageFooter(ctx, obj, ref state); }
/// <summary> /// Emit code initializing field values inside a constructor, if one is required /// </summary> protected override void WriteInitField(GeneratorContext ctx, FieldDescriptorProto obj, ref object state, OneOfStub[] oneOfs) { var name = ctx.NameNormalizer.GetName(obj); bool isOptional = obj.label == FieldDescriptorProto.Label.LabelOptional; bool isRepeated = obj.label == FieldDescriptorProto.Label.LabelRepeated; string dataFormat; bool isMap; var typeName = GetTypeName(ctx, obj, out dataFormat, out isMap); OneOfStub oneOf = obj.ShouldSerializeOneofIndex() ? oneOfs?[obj.OneofIndex] : null; bool explicitValues = isOptional && oneOf == null && ctx.Syntax == FileDescriptorProto.SyntaxProto2 && obj.type != FieldDescriptorProto.Type.TypeMessage && obj.type != FieldDescriptorProto.Type.TypeGroup; string defaultValue = GetDefaultValue(ctx, obj, typeName); if (isRepeated) { var mapMsgType = isMap ? ctx.TryFind <DescriptorProto>(obj.TypeName) : null; if (mapMsgType != null) { string keyDataFormat; string valueDataFormat; bool _; var keyTypeName = GetTypeName(ctx, mapMsgType.Fields.Single(x => x.Number == 1), out keyDataFormat, out _); var valueTypeName = GetTypeName(ctx, mapMsgType.Fields.Single(x => x.Number == 2), out valueDataFormat, out _); ctx.WriteLine($"{Escape(name)} = new global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}>();"); } else if (UseArray(obj)) { } // nothing needed else { ctx.WriteLine($"{Escape(name)} = new global::System.Collections.Generic.List<{typeName}>();"); } } else if (oneOf != null) { } // nothing to do else if (explicitValues) { } // nothing to do else { if (!string.IsNullOrWhiteSpace(defaultValue)) { ctx.WriteLine($"{Escape(name)} = {defaultValue};"); } } }
=> obj.Options?.MapEntry ?? false; // don't write this type - use a dictionary instead /// <summary> /// Emit code representing a message type /// </summary> protected virtual void WriteMessage(GeneratorContext ctx, DescriptorProto obj) { object state = null; if (ShouldOmitMessage(ctx, obj, ref state)) { return; } WriteMessageHeader(ctx, obj, ref state); var oneOfs = OneOfStub.Build(ctx, obj); ctx.WriteLine("#region 属性"); foreach (var inner in obj.Fields) { WriteField(ctx, inner, ref state, oneOfs); } ctx.WriteLine("#endregion"); ctx.WriteLine("#region 定义"); foreach (var inner in obj.NestedTypes) { WriteMessage(ctx, inner); } foreach (var inner in obj.EnumTypes) { WriteEnum(ctx, inner); } if (obj.Extensions.Count != 0) { object extState = null; WriteExtensionsHeader(ctx, obj, ref extState); foreach (var ext in obj.Extensions) { WriteExtension(ctx, ext); } WriteExtensionsFooter(ctx, obj, ref extState); } ctx.WriteLine("#endregion"); ctx.WriteLine("#region 读取写入"); WriteReadFun(ctx, obj, oneOfs); WriteWriteFun(ctx, obj, oneOfs); ctx.WriteLine("#endregion"); WriteMessageFooter(ctx, obj, ref state); }
/// <summary> /// Emit code representing 'oneof' elements as an enum discriminator /// </summary> protected virtual void WriteOneOf(GeneratorContext ctx, OneOfStub stub) { if (ctx.OneOfEnums) { int index = stub.Index; var obj = stub.OneOf; object state = null; WriteOneOfDiscriminator(ctx, obj, ref state); WriteOneOfEnumHeader(ctx, obj, ref state); foreach (var field in obj.Parent.Fields) { if (field.ShouldSerializeOneofIndex() && field.OneofIndex == index) { WriteOneOfEnumValue(ctx, field, ref state); } } WriteOneOfEnumFooter(ctx, obj, ref state); } }
internal static OneOfStub[] Build(GeneratorContext context, DescriptorProto message) { if (message.OneofDecls.Count == 0) { return(null); } var stubs = new OneOfStub[message.OneofDecls.Count]; int index = 0; foreach (var decl in message.OneofDecls) { stubs[index++] = new OneOfStub(decl); } foreach (var field in message.Fields) { if (field.ShouldSerializeOneofIndex()) { stubs[field.OneofIndex].AccountFor(field.type, field.TypeName); } } return(stubs); }
/// <summary> /// Write a field /// </summary> protected override void WriteField(GeneratorContext ctx, FieldDescriptorProto field, ref object state, OneOfStub[] oneOfs) { var name = ctx.NameNormalizer.GetName(field); var tw = ctx.Write($"[global::ProtoBuf.ProtoMember({field.Number}"); if (name != field.Name) { tw.Write($@", Name = @""{field.Name}"""); } var options = field.Options?.GetOptions(); if (options?.AsReference == true) { tw.Write(", AsReference = true"); } if (options?.DynamicType == true) { tw.Write(", DynamicType = true"); } bool isOptional = field.label == FieldDescriptorProto.Label.LabelOptional; bool isRepeated = field.label == FieldDescriptorProto.Label.LabelRepeated; OneOfStub oneOf = field.ShouldSerializeOneofIndex() ? oneOfs?[field.OneofIndex] : null; if (oneOf != null && !ctx.OneOfEnums && oneOf.CountTotal == 1) { oneOf = null; // not really a one-of, then! } bool explicitValues = isOptional && oneOf == null && ctx.Syntax == FileDescriptorProto.SyntaxProto2 && field.type != FieldDescriptorProto.Type.TypeMessage && field.type != FieldDescriptorProto.Type.TypeGroup; bool suppressDefaultAttribute = !isOptional; var typeName = GetTypeName(ctx, field, out var dataFormat, out var isMap); string defaultValue = GetDefaultValue(ctx, field, typeName); if (!string.IsNullOrWhiteSpace(dataFormat)) { tw.Write($", DataFormat = global::ProtoBuf.DataFormat.{dataFormat}"); } if (field.IsPacked(ctx.Syntax)) { tw.Write($", IsPacked = true"); } if (field.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, field.Options); if (isRepeated) { var mapMsgType = isMap ? ctx.TryFind <DescriptorProto>(field.TypeName) : null; bool allowSet = ctx.EmitListSetters; 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 ? "]" : ")]"); if (ctx.Supports(CSharp6)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}> {Escape(name)} {{ get; {(allowSet ? "set; " : "")}}} = new global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}>();"); } else { ctx.WriteLine($"{GetAccess(GetAccess(field))} global::System.Collections.Generic.Dictionary<{keyTypeName}, {valueTypeName}> {Escape(name)} {{ get; {(allowSet ? "" : "private ")}set; }}"); } } else if (UseArray(field)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} {typeName}[] {Escape(name)} {{ get; set; }}"); } else if (ctx.Supports(CSharp6)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} global::System.Collections.Generic.List<{typeName}> {Escape(name)} {{ get; {(allowSet ? "set; " : "")}}} = new global::System.Collections.Generic.List<{typeName}>();"); } else { ctx.WriteLine($"{GetAccess(GetAccess(field))} global::System.Collections.Generic.List<{typeName}> {Escape(name)} {{ get; {(allowSet ? "" : "private ")}set; }}"); } } else if (oneOf != null) { var defValue = string.IsNullOrWhiteSpace(defaultValue) ? (ctx.Supports(CSharp7_1) ? "default" : $"default({typeName})") : defaultValue; var fieldName = GetOneOfFieldName(oneOf.OneOf); var storage = oneOf.GetStorage(field.type, field.TypeName); ctx.WriteLine($"{GetAccess(GetAccess(field))} {typeName} {Escape(name)}").WriteLine("{").Indent(); switch (field.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({field.Number}) ? (({typeName}){fieldName}.{storage}) : {defValue}; }}"); break; default: ctx.WriteLine($"get {{ return {fieldName}.Is({field.Number}) ? {fieldName}.{storage} : {defValue}; }}"); break; } var unionType = oneOf.GetUnionType(); var cast = field.type == FieldDescriptorProto.Type.TypeEnum ? "(int)" : ""; ctx.WriteLine($"set {{ {fieldName} = new global::ProtoBuf.{unionType}({field.Number}, {cast}value); }}") .Outdent().WriteLine("}"); if (ctx.Supports(CSharp6)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} bool ShouldSerialize{name}() => {fieldName}.Is({field.Number});") .WriteLine($"{GetAccess(GetAccess(field))} void Reset{name}() => global::ProtoBuf.{unionType}.Reset(ref {fieldName}, {field.Number});"); } else { ctx.WriteLine($"{GetAccess(GetAccess(field))} bool ShouldSerialize{name}()").WriteLine("{").Indent() .WriteLine($"return {fieldName}.Is({field.Number});").Outdent().WriteLine("}") .WriteLine($"{GetAccess(GetAccess(field))} void Reset{name}()").WriteLine("{").Indent() .WriteLine($"global::ProtoBuf.{unionType}.Reset(ref {fieldName}, {field.Number});").Outdent().WriteLine("}"); } if (oneOf.IsFirst()) { ctx.WriteLine().WriteLine($"private global::ProtoBuf.{unionType} {fieldName};"); } } else if (explicitValues) { string fieldName = FieldPrefix + name, fieldType; bool isRef = false; switch (field.type) { case FieldDescriptorProto.Type.TypeString: case FieldDescriptorProto.Type.TypeBytes: fieldType = typeName; isRef = true; break; default: fieldType = typeName + "?"; break; } ctx.WriteLine($"{GetAccess(GetAccess(field))} {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("}"); if (ctx.Supports(CSharp6)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} bool ShouldSerialize{name}() => {fieldName} != null;") .WriteLine($"{GetAccess(GetAccess(field))} void Reset{name}() => {fieldName} = null;"); } else { ctx.WriteLine($"{GetAccess(GetAccess(field))} bool ShouldSerialize{name}()").WriteLine("{").Indent() .WriteLine($"return {fieldName} != null;").Outdent().WriteLine("}") .WriteLine($"{GetAccess(GetAccess(field))} void Reset{name}()").WriteLine("{").Indent() .WriteLine($"{fieldName} = null;").Outdent().WriteLine("}"); } ctx.WriteLine($"private {fieldType} {fieldName};"); } else { tw = ctx.Write($"{GetAccess(GetAccess(field))} {typeName} {Escape(name)} {{ get; set; }}"); if (!string.IsNullOrWhiteSpace(defaultValue) && ctx.Supports(CSharp6)) { tw.Write($" = {defaultValue};"); } tw.WriteLine(); } ctx.WriteLine(); }
/// <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(); }
/// <summary> /// Write a field /// </summary> protected override void WriteField(GeneratorContext ctx, FieldDescriptorProto field, ref object state, OneOfStub[] oneOfs) { var name = ctx.NameNormalizer.GetName(field); var tw = ctx.Write($"<Global.ProtoBuf.ProtoMember({field.Number}"); if (name != field.Name) { tw.Write($@", Name := ""{field.Name}"""); } var options = field.Options?.GetOptions(); if (options?.AsReference == true) { tw.Write(", AsReference := True"); } if (options?.DynamicType == true) { tw.Write(", DynamicType := True"); } bool isOptional = field.label == FieldDescriptorProto.Label.LabelOptional; bool isRepeated = field.label == FieldDescriptorProto.Label.LabelRepeated; OneOfStub oneOf = field.ShouldSerializeOneofIndex() ? oneOfs?[field.OneofIndex] : null; if (oneOf != null && !ctx.OneOfEnums && oneOf.CountTotal == 1) { oneOf = null; // not really a one-of, then! } bool explicitValues = isOptional && oneOf == null && ctx.Syntax == FileDescriptorProto.SyntaxProto2 && field.type != FieldDescriptorProto.Type.TypeMessage && field.type != FieldDescriptorProto.Type.TypeGroup; bool suppressDefaultAttribute = !isOptional; var typeName = GetTypeName(ctx, field, out var dataFormat, out var isMap); string defaultValue = GetDefaultValue(ctx, field, typeName); if (!string.IsNullOrWhiteSpace(dataFormat)) { tw.Write($", DataFormat := Global.ProtoBuf.DataFormat.{dataFormat}"); } if (field.IsPacked(ctx.Syntax)) { tw.Write($", IsPacked := True"); } if (field.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, field.Options); if (isRepeated) { var mapMsgType = isMap ? ctx.TryFind <DescriptorProto>(field.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 ? "> _" : ")> _"); if (ctx.Supports(VB14)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} Readonly Property {Escape(name)} As New Global.System.Collections.Generic.Dictionary(Of {keyTypeName}, {valueTypeName})"); } else { var fieldName = FieldPrefix + name; ctx.WriteLine($"{GetAccess(GetAccess(field))} Readonly Property {Escape(name)} As Global.System.Collections.Generic.Dictionary(Of {keyTypeName}, {valueTypeName})").Indent() .WriteLine("Get").Indent() .WriteLine($"Return {fieldName}") .Outdent().WriteLine("End Get").Outdent().WriteLine("End Property").WriteLine() .WriteLine($"Private ReadOnly {fieldName} As New Global.System.Collections.Generic.Dictionary(Of {keyTypeName}, {valueTypeName})").WriteLine(); } } else if (UseArray(field)) { if (ctx.Supports(VB11)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}()"); } else { var fieldName = FieldPrefix + name; ctx.WriteLine($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}()") .Indent().WriteLine("Get").Indent().WriteLine($"Return {fieldName}").Outdent().WriteLine("End Get").Outdent() .Indent().WriteLine($"Set(ByVal value as {typeName}())").Indent().WriteLine($"{fieldName} = value").Outdent().WriteLine("End Set").Outdent() .WriteLine("End Property").WriteLine() .WriteLine($"Private {fieldName} As {typeName}()").WriteLine(); } } else { if (ctx.Supports(VB14)) { ctx.WriteLine($"{GetAccess(GetAccess(field))} Readonly Property {Escape(name)} As New Global.System.Collections.Generic.List(Of {typeName})"); } else { var fieldName = FieldPrefix + name; ctx.WriteLine($"{GetAccess(GetAccess(field))} Readonly Property {Escape(name)} As Global.System.Collections.Generic.List(Of {typeName})").Indent() .WriteLine("Get").Indent() .WriteLine($"Return {fieldName}") .Outdent().WriteLine("End Get").Outdent().WriteLine("End Property").WriteLine() .WriteLine($"Private ReadOnly {fieldName} As New Global.System.Collections.Generic.List(Of {typeName})").WriteLine(); } } } else if (oneOf != null) { var defValue = string.IsNullOrWhiteSpace(defaultValue) ? $"CType(Nothing, {typeName})" : defaultValue; var fieldName = GetOneOfFieldName(oneOf.OneOf); var storage = oneOf.GetStorage(field.type, field.TypeName); ctx.WriteLine($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}").Indent().WriteLine("Get").Indent(); switch (field.type) { case FieldDescriptorProto.Type.TypeMessage: case FieldDescriptorProto.Type.TypeGroup: case FieldDescriptorProto.Type.TypeEnum: case FieldDescriptorProto.Type.TypeBytes: case FieldDescriptorProto.Type.TypeString: ctx.WriteLine($"Return If({fieldName}.Is({field.Number}), CType({fieldName}.{storage}, {typeName}), {defValue})"); break; default: ctx.WriteLine($"Return If({fieldName}.Is({field.Number}), {fieldName}.{storage}, {defValue})"); break; } ctx.Outdent().WriteLine("End Get"); var unionType = oneOf.GetUnionType(); ctx.WriteLine($"Set(ByVal value As {typeName})").Indent() .WriteLine($"{fieldName} = New Global.ProtoBuf.{unionType}({field.Number}, value)").Outdent().WriteLine("End Set"); ctx.Outdent().WriteLine("End Property").WriteLine(); ctx.WriteLine($"{GetAccess(GetAccess(field))} Function ShouldSerialize{name}() As Boolean").Indent() .WriteLine($"Return {fieldName}.Is({field.Number})").Outdent() .WriteLine("End Function").WriteLine() .WriteLine($"{GetAccess(GetAccess(field))} Sub Reset{name}()").Indent() .WriteLine($"Global.ProtoBuf.{unionType}.Reset({fieldName}, {field.Number})") .Outdent().WriteLine("End Sub"); if (oneOf.IsFirst()) { ctx.WriteLine().WriteLine($"Private {fieldName} As Global.ProtoBuf.{unionType}"); } } else if (explicitValues) { string fieldName = FieldPrefix + name, fieldType; bool isRef = false; switch (field.type) { case FieldDescriptorProto.Type.TypeString: case FieldDescriptorProto.Type.TypeBytes: fieldType = typeName; isRef = true; break; default: fieldType = typeName + "?"; break; } ctx.WriteLine($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}").Indent() .WriteLine("Get").Indent(); if (!string.IsNullOrWhiteSpace(defaultValue)) { ctx.WriteLine($"Return If({fieldName}, {defaultValue})"); } else if (!isRef) { ctx.WriteLine($"Return {fieldName}.GetValueOrDefault()"); } else { ctx.WriteLine($"Return {fieldName}"); } ctx.Outdent().WriteLine("End Get").WriteLine($"Set(ByVal value As {typeName})").Indent() .WriteLine($"{fieldName} = value").Outdent().WriteLine("End Set"). Outdent().WriteLine("End Property"); ctx.WriteLine($"{GetAccess(GetAccess(field))} Function ShouldSerialize{name}() As Boolean").Indent() .WriteLine($"Return Not ({fieldName} Is Nothing)").Outdent() .WriteLine("End Function") .WriteLine($"{GetAccess(GetAccess(field))} Sub Reset{name}()").Indent() .WriteLine($"{fieldName} = Nothing").Outdent().WriteLine("End Sub"); ctx.WriteLine($"Private {fieldName} As {fieldType}"); } else { if (ctx.Supports(VB11)) { tw = ctx.Write($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}"); if (!string.IsNullOrWhiteSpace(defaultValue)) { tw.Write($" = {defaultValue}"); } tw.WriteLine(); } else { var fieldName = FieldPrefix + name; tw = ctx.WriteLine($"{GetAccess(GetAccess(field))} Property {Escape(name)} As {typeName}") .Indent().WriteLine("Get").Indent().WriteLine($"Return {fieldName}").Outdent().WriteLine("End Get").Outdent() .Indent().WriteLine($"Set(ByVal value as {typeName})").Indent().WriteLine($"{fieldName} = value").Outdent().WriteLine("End Set").Outdent() .WriteLine("End Property").WriteLine() .Write($"Private {fieldName} As {typeName}"); if (!string.IsNullOrWhiteSpace(defaultValue)) { tw.Write($" = {defaultValue}"); } tw.WriteLine(); } } ctx.WriteLine(); }