From dfda7d91ff5d6d1426621b8bd810e93a5e401b8c Mon Sep 17 00:00:00 2001 From: Nadav Samet Date: Mon, 11 Apr 2016 23:14:11 -0700 Subject: [PATCH] Add a generator option for a single line toString Fixes #82. --- .../trueaccord/scalapb/compiler/ProtobufGenerator.scala | 8 ++++++-- .../main/scala/com/trueaccord/scalapb/TextFormat.scala | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/ProtobufGenerator.scala b/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/ProtobufGenerator.scala index 3a2850648..9f6ea93c5 100644 --- a/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/ProtobufGenerator.scala +++ b/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/ProtobufGenerator.scala @@ -6,7 +6,9 @@ import com.google.protobuf.{ByteString => GoogleByteString} import com.google.protobuf.compiler.PluginProtos.{CodeGeneratorRequest, CodeGeneratorResponse} import scala.collection.JavaConversions._ -case class GeneratorParams(javaConversions: Boolean = false, flatPackage: Boolean = false, grpc: Boolean = false) +case class GeneratorParams( + javaConversions: Boolean = false, flatPackage: Boolean = false, + grpc: Boolean = false, singleLineToString: Boolean = false) // Exceptions that are caught and passed upstreams as errors. case class GeneratorException(message: String) extends Exception(message) @@ -880,7 +882,8 @@ class ProtobufGenerator(val params: GeneratorParams) extends DescriptorPimps { |def with${oneof.upperScalaName}(__v: ${oneof.scalaTypeName}): ${message.nameSymbol} = copy(${oneof.scalaName.asSymbol} = __v)""") } .call(generateGetField(message)) - .add(s"override def toString: String = com.trueaccord.scalapb.TextFormat.printToUnicodeString(this)") + .when(!params.singleLineToString)(_.add(s"override def toString: String = com.trueaccord.scalapb.TextFormat.printToUnicodeString(this)")) + .when(params.singleLineToString)(_.add(s"override def toString: String = com.trueaccord.scalapb.TextFormat.printToSingleLineUnicodeString(this)")) .add(s"def companion = ${message.scalaTypeName}") .outdent .outdent @@ -1045,6 +1048,7 @@ object ProtobufGenerator { case (Right(params), "java_conversions") => Right(params.copy(javaConversions = true)) case (Right(params), "flat_package") => Right(params.copy(flatPackage = true)) case (Right(params), "grpc") => Right(params.copy(grpc = true)) + case (Right(params), "single_line_to_string") => Right(params.copy(singleLineToString = true)) case (Right(params), p) => Left(s"Unrecognized parameter: '$p'") case (x, _) => x } diff --git a/scalapb-runtime/shared/src/main/scala/com/trueaccord/scalapb/TextFormat.scala b/scalapb-runtime/shared/src/main/scala/com/trueaccord/scalapb/TextFormat.scala index 11109c746..e462f8b42 100644 --- a/scalapb-runtime/shared/src/main/scala/com/trueaccord/scalapb/TextFormat.scala +++ b/scalapb-runtime/shared/src/main/scala/com/trueaccord/scalapb/TextFormat.scala @@ -33,5 +33,9 @@ object TextFormat { def printToUnicodeString(m: GeneratedMessage) = { Printer.printToString(m, singleLineMode = false, escapeNonAscii = false) } + + def printToSingleLineUnicodeString(m: GeneratedMessage) = { + Printer.printToString(m, singleLineMode = true, escapeNonAscii = false) + } }