Skip to content

Commit

Permalink
remove protobuf printer options
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Dec 15, 2024
1 parent 4a332f1 commit 47140c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,4 @@ src/StarBreaker.Sandbox/mats.txt
src/StarBreaker.Sandbox/keys.txt
scripts/dump
src/StarBreaker.Grpc/protos
scripts/__pycache__
93 changes: 9 additions & 84 deletions src/StarBreaker.Protobuf/DynamicGrpcPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = " ";
}

/// <summary>
/// Extension methods for printing descriptors back to proto language.
/// </summary>
Expand All @@ -25,12 +18,11 @@ public static class DynamicGrpcPrinter
/// Prints the proto description of the specified <see cref="FileDescriptor"/> to a string.
/// </summary>
/// <param name="file">The descriptor to print.</param>
/// <param name="options">The printing options.</param>
/// <returns>A proto description of the specified descriptor.</returns>
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();
}

Expand All @@ -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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -139,36 +125,24 @@ 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("}");
}

private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPrinterContext context)
{
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)
{
Expand All @@ -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
Expand Down Expand Up @@ -293,18 +266,13 @@ private static void ToProtoString(this MessageDescriptor message, DynamicGrpcPri
}

context.UnIndent();
context.PopContextName(message.Name);
context.WriteLine("}");
}

private static string Bool(this bool value) => value ? "true" : "false";

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)
Expand Down Expand Up @@ -363,17 +331,11 @@ private static void ToProtoString(FileOptions options, DynamicGrpcPrinterContext

private class DynamicGrpcPrinterContext
{
private readonly List<string> _contextNames;

public DynamicGrpcPrinterContext(TextWriter writer, DynamicGrpcPrinterOptions options)
public DynamicGrpcPrinterContext(TextWriter writer)
{
_contextNames = new List<string>();
Writer = writer;
Options = options;
}

public DynamicGrpcPrinterOptions Options { get; }

public int Level { get; set; }

public void Indent() => Level++;
Expand All @@ -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)
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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}";
}
}
7 changes: 1 addition & 6 deletions src/StarBreaker.Protobuf/ProtobufExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public void WriteProtos(string protoPath, Func<FileDescriptor, bool>? filter = n
var fileDescriptors = FileDescriptor.BuildFromByteStrings(protoByteStrings);

var targetFolder = Directory.CreateDirectory(protoPath);

var opts = new DynamicGrpcPrinterOptions
{
FullyQualified = true
};

foreach (var fileDescriptor in fileDescriptors)
{
Expand All @@ -46,7 +41,7 @@ public void WriteProtos(string protoPath, Func<FileDescriptor, bool>? filter = n
if (!string.IsNullOrWhiteSpace(dir))
Directory.CreateDirectory(dir);

File.WriteAllText(path, fileDescriptor.ToProtoString(opts));
File.WriteAllText(path, fileDescriptor.ToProtoString());
}
}

Expand Down

0 comments on commit 47140c2

Please sign in to comment.