Skip to content

Commit

Permalink
Add a generator option for a single line toString
Browse files Browse the repository at this point in the history
Fixes #82.
  • Loading branch information
thesamet committed Apr 12, 2016
1 parent f6fa0f9 commit dfda7d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Expand Up @@ -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)
}
}

0 comments on commit dfda7d9

Please sign in to comment.