From 47140c2875d877e9a2effcde95d46ae887744948 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Sun, 15 Dec 2024 17:03:04 +0000 Subject: [PATCH] remove protobuf printer options --- .gitignore | 1 + .../DynamicGrpcPrinter.cs | 93 ++----------------- src/StarBreaker.Protobuf/ProtobufExtractor.cs | 7 +- 3 files changed, 11 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 2570dd9..c7fe965 100644 --- a/.gitignore +++ b/.gitignore @@ -206,3 +206,4 @@ src/StarBreaker.Sandbox/mats.txt src/StarBreaker.Sandbox/keys.txt scripts/dump src/StarBreaker.Grpc/protos +scripts/__pycache__ diff --git a/src/StarBreaker.Protobuf/DynamicGrpcPrinter.cs b/src/StarBreaker.Protobuf/DynamicGrpcPrinter.cs index 775c9a0..9a8dc59 100644 --- a/src/StarBreaker.Protobuf/DynamicGrpcPrinter.cs +++ b/src/StarBreaker.Protobuf/DynamicGrpcPrinter.cs @@ -9,13 +9,6 @@ namespace StarBreaker.Protobuf; //Note: this is a modified version of the original code. I don't really remember what changes make sense or why, // but this is working and the whole thing is very brittle so I'm leaving it like this :) -public class DynamicGrpcPrinterOptions -{ - public bool AddMetaComments { get; set; } - public bool FullyQualified { get; set; } - public string Indent { get; set; } = " "; -} - /// /// Extension methods for printing descriptors back to proto language. /// @@ -25,12 +18,11 @@ public static class DynamicGrpcPrinter /// Prints the proto description of the specified to a string. /// /// The descriptor to print. - /// The printing options. /// A proto description of the specified descriptor. - public static string ToProtoString(this FileDescriptor file, DynamicGrpcPrinterOptions? options = null) + public static string ToProtoString(this FileDescriptor file) { var writer = new StringWriter(); - ToProtoString(file, new DynamicGrpcPrinterContext(writer, options ?? new DynamicGrpcPrinterOptions())); + ToProtoString(file, new DynamicGrpcPrinterContext(writer)); return writer.ToString(); } @@ -53,11 +45,6 @@ private static string GetEnumName(Enum enumValue, [DynamicallyAccessedMembers(Dy private static void ToProtoString(this FileDescriptor file, DynamicGrpcPrinterContext context) { - if (context.Options.AddMetaComments) - { - context.WriteLine($"// {file.Name} is a proto file."); - } - bool requiresNewLine = false; switch (file.Syntax) { @@ -79,7 +66,6 @@ private static void ToProtoString(this FileDescriptor file, DynamicGrpcPrinterCo context.WriteLine($"package {file.Package};"); requiresNewLine = true; } - context.PushContextName(file.Package); // Dump imports if (requiresNewLine) context.WriteLine(); @@ -139,24 +125,17 @@ private static void ToProtoString(this FileDescriptor file, DynamicGrpcPrinterCo context.WriteLine("}"); } - context.PopContextName(file.Package); } private static void ToProtoString(this ServiceDescriptor service, DynamicGrpcPrinterContext context) { - if (context.Options.AddMetaComments) - { - context.WriteLine($"// {service.FullName} is a service:"); - } context.WriteLine($"service {service.Name} {{"); - context.PushContextName(service.Name); context.Indent(); foreach (var method in service.Methods) { context.WriteLine($"rpc {method.Name} ({(method.IsClientStreaming ? "stream" : "")} {context.GetTypeName(method.InputType)} ) returns ({(method.IsServerStreaming ? "stream" : "")} {context.GetTypeName(method.OutputType)} );"); } context.UnIndent(); - context.PopContextName(service.Name); context.WriteLine("}"); } @@ -164,11 +143,6 @@ private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPri { bool isEmpty = message.Fields.InDeclarationOrder().Count == 0 && message.NestedTypes.Count == 0 && message.EnumTypes.Count == 0 && message.GetOptions() == null; - if (context.Options.AddMetaComments) - { - context.WriteLine($"// {message.FullName} is {(isEmpty ? "an empty" : "a")} message:"); - } - // Compact form, if a message is empty, output a single line if (isEmpty) { @@ -177,7 +151,6 @@ private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPri } context.WriteLine($"message {message.Name} {{"); - context.PushContextName(message.Name); context.Indent(); // handle options @@ -293,7 +266,6 @@ private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPri } context.UnIndent(); - context.PopContextName(message.Name); context.WriteLine("}"); } @@ -301,10 +273,6 @@ private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPri private static void ToProtoString(this EnumDescriptor enumDescriptor, DynamicGrpcPrinterContext context) { - if (context.Options.AddMetaComments) - { - context.WriteLine($"// {enumDescriptor.FullName} is an enum:"); - } context.WriteLine($"enum {enumDescriptor.Name} {{"); context.Indent(); foreach (var item in enumDescriptor.Values) @@ -363,17 +331,11 @@ private static void ToProtoString(FileOptions options, DynamicGrpcPrinterContext private class DynamicGrpcPrinterContext { - private readonly List _contextNames; - - public DynamicGrpcPrinterContext(TextWriter writer, DynamicGrpcPrinterOptions options) + public DynamicGrpcPrinterContext(TextWriter writer) { - _contextNames = new List(); Writer = writer; - Options = options; } - public DynamicGrpcPrinterOptions Options { get; } - public int Level { get; set; } public void Indent() => Level++; @@ -395,34 +357,15 @@ public void WriteLine(string text) private void WriteIndent() { - var indent = Options.Indent; - for (int i = 0; i < Level; i++) + for (var i = 0; i < Level; i++) { - Writer.Write(indent); - } - } - - public void PushContextName(string name) - { - if (string.IsNullOrEmpty(name)) return; - foreach (var partName in name.Split('.')) - { - _contextNames.Add($"{partName}."); - } - } - - public void PopContextName(string name) - { - if (string.IsNullOrEmpty(name)) return; - foreach (var _ in name.Split('.')) - { - _contextNames.RemoveAt(_contextNames.Count - 1); + Writer.Write(" "); } } public string GetTypeName(MessageDescriptor descriptor) { - return GetContextualTypeName(descriptor.FullName); + return GetAbsoluteTypeName(descriptor.FullName); } public string GetTypeName(FieldDescriptor field) @@ -483,7 +426,7 @@ public string GetTypeName(FieldDescriptor field) case FieldType.Group: break; case FieldType.Message: - builder.Append(GetContextualTypeName(field.MessageType.FullName)); + builder.Append(GetAbsoluteTypeName(field.MessageType.FullName)); break; case FieldType.Bytes: builder.Append("bytes"); @@ -504,7 +447,7 @@ public string GetTypeName(FieldDescriptor field) builder.Append("sint64"); break; case FieldType.Enum: - builder.Append(GetContextualTypeName(field.EnumType.FullName)); + builder.Append(GetAbsoluteTypeName(field.EnumType.FullName)); break; default: throw new ArgumentOutOfRangeException(); @@ -513,24 +456,6 @@ public string GetTypeName(FieldDescriptor field) return builder.ToString(); } - //TODO: this is a bit buggy, avoid using. - private string GetContextualTypeName(string fullTypeName) - { - if (Options.FullyQualified) return $".{fullTypeName}"; - - int nextIndex = 0; - foreach (var partName in _contextNames) - { - var currentIndex = fullTypeName.IndexOf(partName, nextIndex, StringComparison.OrdinalIgnoreCase); - if (currentIndex != nextIndex) - { - break; - } - - nextIndex = currentIndex + partName.Length; - } - - return nextIndex > 0 ? fullTypeName.Substring(nextIndex) : $".{fullTypeName}"; - } + private static string GetAbsoluteTypeName(string fullTypeName) => $".{fullTypeName}"; } } \ No newline at end of file diff --git a/src/StarBreaker.Protobuf/ProtobufExtractor.cs b/src/StarBreaker.Protobuf/ProtobufExtractor.cs index 905e2a3..8d139b5 100644 --- a/src/StarBreaker.Protobuf/ProtobufExtractor.cs +++ b/src/StarBreaker.Protobuf/ProtobufExtractor.cs @@ -29,11 +29,6 @@ public void WriteProtos(string protoPath, Func? filter = n var fileDescriptors = FileDescriptor.BuildFromByteStrings(protoByteStrings); var targetFolder = Directory.CreateDirectory(protoPath); - - var opts = new DynamicGrpcPrinterOptions - { - FullyQualified = true - }; foreach (var fileDescriptor in fileDescriptors) { @@ -46,7 +41,7 @@ public void WriteProtos(string protoPath, Func? filter = n if (!string.IsNullOrWhiteSpace(dir)) Directory.CreateDirectory(dir); - File.WriteAllText(path, fileDescriptor.ToProtoString(opts)); + File.WriteAllText(path, fileDescriptor.ToProtoString()); } }