Skip to content

Commit

Permalink
Merge pull request #9 from nimrof/repeatedFieldFormatting
Browse files Browse the repository at this point in the history
configurable repeated field (containing value types) formatting
  • Loading branch information
Oceania2018 authored Oct 9, 2023
2 parents 91ac689 + a65f2d7 commit 5cf2c46
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/protobuf.Text/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,34 @@ private void WriteSingleField(TextWriter writer, FieldDescriptor field, object v

private void WriteRepeatedField(TextWriter writer, FieldDescriptor field, object value)
{
// Repeated field. Print each element.
foreach (object element in (IEnumerable) value)
if(settings.FormatRepeatedValueOnSingleLine && (value as IList)[0].GetType().IsValueType)
{
WriteSingleField(writer, field, element);
writer.Write(field.Name);
writer.Write(NameValueSeparator);
writer.Write("[");
bool first = true;
foreach (object element in (IEnumerable) value)
{
if(!first)
{
writer.Write(", ");
}

WriteValue(writer, element);
first = false;
}
writer.Write(" ]");
writer.Write(PropertySeparator);
}
else
{
// Repeated field. Print each element.
foreach (object element in (IEnumerable) value)
{
WriteSingleField(writer, field, element);
writer.Write(PropertySeparator);
}
}
}

// Converted from java/core/src/main/java/com/google/protobuf/Descriptors.java
Expand Down Expand Up @@ -1032,7 +1054,7 @@ public sealed class Settings
// valid language elements.
static Settings()
{
Default = new Settings(false, TypeRegistry.Empty, true);
Default = new Settings(false, TypeRegistry.Empty, true, false);
}

/// <summary>
Expand All @@ -1041,6 +1063,12 @@ static Settings()
/// </summary>
public bool FormatDefaultValues { get; }

/// <summary>
/// Whether repeated fields that contains value types will be printed on a single line
/// (repeated : [0, 0, 0, 0 ]) instead of one line pr. contained value
/// </summary>
public bool FormatRepeatedValueOnSingleLine { get; }

/// <summary>
/// The type registry used to format <see cref="Any"/> messages.
/// </summary>
Expand All @@ -1067,7 +1095,7 @@ public Settings(bool formatDefaultValues) : this(formatDefaultValues, TypeRegist
/// </summary>
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false)
public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false, false)
{
}

Expand All @@ -1077,32 +1105,41 @@ public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(form
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages. TypeRegistry.Empty will be used if it is null.</param>
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
/// <param name="formatRepeatedValueOnSingleLine"><c>true</c> to format the repeated containing value types on a single line; <c>false</c> format repeated to print one line pr. entry</param>
private Settings(bool formatDefaultValues,
TypeRegistry typeRegistry,
bool formatEnumsAsIntegers)
bool formatEnumsAsIntegers,
bool formatRepeatedValueOnSingleLine)
{
FormatDefaultValues = formatDefaultValues;
TypeRegistry = typeRegistry ?? TypeRegistry.Empty;
FormatEnumsAsIntegers = formatEnumsAsIntegers;
FormatRepeatedValueOnSingleLine = formatRepeatedValueOnSingleLine;
}

/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values and the current settings.
/// </summary>
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers);
public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, FormatRepeatedValueOnSingleLine);

/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified type registry and the current settings.
/// </summary>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers);
public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers, FormatRepeatedValueOnSingleLine);

/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified enums formatting option and the current settings.
/// </summary>
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers);
public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers, FormatRepeatedValueOnSingleLine);

/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified repeated formatting option and the current settings.
/// </summary>
/// <param name="formatRepeatedValueOnSingleLine"><c>true</c> to format the repeated containing value types on a single line; <c>false</c> format repeated to print one line pr. entry</param>
public Settings WithFormatRepeatedValueOnsingleLine(bool formatRepeatedValueOnSingleLine) => new Settings(FormatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, formatRepeatedValueOnSingleLine);
}

}
Expand Down

0 comments on commit 5cf2c46

Please sign in to comment.