Skip to content

Commit

Permalink
Introduce toProtoString to returns the ASCII format, instead of toString
Browse files Browse the repository at this point in the history
To keep the original behavior where toString returns the ASCII format,
use scalapb.gen(asciiFormatToString=true) if using sbt-protoc, or
pass the generator parameter `ascii_format_to_string` if using ScalaPBC.

Fix #235
  • Loading branch information
thesamet committed Dec 10, 2017
1 parent 493ab0a commit 19a10f5
Show file tree
Hide file tree
Showing 114 changed files with 152 additions and 134 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Changes:
type classes that deal with Oneofs.
- Add support for Scala annotations on field-level, and companion object level.
- It is now possible to specify additional traits for oneofs.
- #235: toString is now not overridden by ScalaPB and does not generate the
default TextFormat. To get the text format, use toProtoString. See issue #235
for how to achieve a backwards-compatible behavior.

## [v0.6.7](https://github.com/scalapb/ScalaPB/tree/v0.6.6) (2017-11-23)
[Full Changelog](https://github.com/scalapb/ScalaPB/compare/v0.6.0...v0.6.7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import scala.collection.JavaConverters._

case class GeneratorParams(
javaConversions: Boolean = false, flatPackage: Boolean = false,
grpc: Boolean = false, singleLineToString: Boolean = false)
grpc: Boolean = false, singleLineToProtoString: Boolean = false,
asciiFormatToString: Boolean = false)

// Exceptions that are caught and passed upstreams as errors.
case class GeneratorException(message: String) extends Exception(message)
Expand Down Expand Up @@ -1228,8 +1229,9 @@ class ProtobufGenerator(val params: GeneratorParams) extends DescriptorPimps {
)
.call(generateGetField(message))
.call(generateGetFieldPValue(message))
.when(!params.singleLineToString)(_.add(s"override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)"))
.when(params.singleLineToString)(_.add(s"override def toString: String = _root_.scalapb.TextFormat.printToSingleLineUnicodeString(this)"))
.when(!params.singleLineToProtoString)(_.add(s"def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)"))
.when(params.singleLineToProtoString)(_.add(s"def toProtoString: String = _root_.scalapb.TextFormat.printToSingleLineUnicodeString(this)"))
.when(params.asciiFormatToString)(_.add("override def toString: String = toProtoString"))
.add(s"def companion = ${message.scalaTypeNameWithMaybeRoot(message)}")
.outdent
.outdent
Expand Down Expand Up @@ -1389,7 +1391,8 @@ 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), "single_line_to_proto_string") => Right(params.copy(singleLineToProtoString = true))
case (Right(params), "ascii_format_to_string") => Right(params.copy(asciiFormatToString = true))
case (Right(params), p) => Left(s"Unrecognized parameter: '$p'")
case (x, _) => x
}
Expand Down
7 changes: 5 additions & 2 deletions compiler-plugin/src/main/scala/scalapb/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ package object scalapb {
flatPackage: Boolean = false,
javaConversions: Boolean = false,
grpc: Boolean = true,
singleLineToString: Boolean = false): (JvmGenerator, Seq[String]) =
singleLineToProtoString: Boolean = false,
asciiFormatToString: Boolean = false
): (JvmGenerator, Seq[String]) =
(JvmGenerator(
"scala",
ScalaPbCodeGenerator),
Seq(
"flat_package" -> flatPackage,
"java_conversions" -> javaConversions,
"grpc" -> grpc,
"single_line_to_string" -> singleLineToString
"single_line_to_proto_string" -> singleLineToProtoString,
"ascii_format_to_string" -> asciiFormatToString
).collect { case (name, v) if v => name })
}
2 changes: 1 addition & 1 deletion e2e/src/test/scala/CollectionTypesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CollectionTypesSpec extends FlatSpec with MustMatchers {
"custom collection" should "work" in {
val c = CustomCollection(repeatedInt32 = MyVector(Vector(11, 24, 19)))
CustomCollection.parseFrom(c.toByteArray) must be(c)
CustomCollection.fromAscii(c.toString) must be(c)
CustomCollection.fromAscii(c.toProtoString) must be(c)
CustomCollection.fromJavaProto(CustomCollection.toJavaProto(c)) must be(c)
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/src/test/scala/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class EnumSpec extends FlatSpec with MustMatchers with OptionValues {

"Unrecognized" should "be printable" in {
// See https://github.com/scalapb/ScalaPB/issues/225
unrecognized.toString must be ("color: 37\n")
unrecognized.toProtoString must be ("color: 37\n")
}

"Unrecognized" should "be fine" in {
Expand Down
4 changes: 2 additions & 2 deletions e2e/src/test/scala/MapsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ class MapsSpec extends FlatSpec with GeneratorDrivenPropertyChecks with MustMatc
personToYear = Map(PersonId("275") -> Years(188)))

CustomMaps.parseFrom(c1.toByteArray) must be(c1)
CustomMaps.fromAscii(c1.toString) must be(c1)
CustomMaps.fromAscii(c1.toProtoString) must be(c1)
CustomMaps.fromJavaProto(CustomMaps.toJavaProto(c1)) must be (c1)

CustomMaps2.parseFrom(c2.toByteArray) must be(c2)
CustomMaps2.fromAscii(c2.toString) must be(c2)
CustomMaps2.fromAscii(c2.toProtoString) must be(c2)
CustomMaps2.fromJavaProto(CustomMaps2.toJavaProto(c2)) must be (c2)
}
}
2 changes: 1 addition & 1 deletion e2e/src/test/scala/RepeatablesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RepeatablesSpec extends FlatSpec with GeneratorDrivenPropertyChecks with M
Arbitrary.arbitrary[Option[Int]].map(s => Nested(nestedField = s))

"toString" should "give empty string" in {
RepeatablesTest().toString must be("")
RepeatablesTest().toProtoString must be("")
}

def mergeRepeatables(rep1: RepeatablesTest, rep2: RepeatablesTest) =
Expand Down
6 changes: 3 additions & 3 deletions e2e/src/test/scala/scalapb/TextFormatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class TextFormatSpec extends FlatSpec with GeneratorDrivenPropertyChecks with Mu
"repeated_double: Infinity\n" +
"repeated_double: -Infinity\n" +
"repeated_double: NaN\n";
TestAllTypes.fromAscii(original).toString must be(canonical)
TestAllTypes.fromAscii(original).toProtoString must be(canonical)
}

"testParseExotic" should "pass" in {
Expand Down Expand Up @@ -469,7 +469,7 @@ class TextFormatSpec extends FlatSpec with GeneratorDrivenPropertyChecks with Mu
"repeated_bool: false\n" +
"repeated_bool: true\n"
val good = TestAllTypes.fromAscii(goodText)
goodTextCanonical must be(good.toString)
goodTextCanonical must be(good.toProtoString)

"optional_bool:2" must failParsingWith("")
"optional_bool:foo" must failParsingWith("")
Expand Down Expand Up @@ -573,7 +573,7 @@ class TextFormatSpec extends FlatSpec with GeneratorDrivenPropertyChecks with Mu
_.bazInt := 102,
_.bazString := "103"
)
TestOneof2.fromAscii(p.toString) must be(p)
TestOneof2.fromAscii(p.toProtoString) must be(p)
}

"testOneofOverwriteAllowed" should "pass" in {
Expand Down
2 changes: 1 addition & 1 deletion proptest/src/test/scala/GeneratedCodeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GeneratedCodeSpec extends PropSpec with GeneratorDrivenPropertyChecks with

// Parsing in Scala the serialized bytes should give the same object.
val scalaParsedFromBytes = companion.parseFrom(scalaBytes)
scalaParsedFromBytes.toString should be(scalaProto.toString)
scalaParsedFromBytes.toProtoString should be(scalaProto.toProtoString)
scalaParsedFromBytes should be(scalaProto)

// Parsing in Java the bytes serialized by Scala should give back javaProto:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ final case class Any(
case 2 => _root_.scalapb.descriptors.PByteString(value)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.any.Any
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ final case class Api(
case 7 => _root_.scalapb.descriptors.PEnum(syntax.scalaValueDescriptor)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.api.Api
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ final case class Method(
case 7 => _root_.scalapb.descriptors.PEnum(syntax.scalaValueDescriptor)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.api.Method
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ final case class Mixin(
case 2 => _root_.scalapb.descriptors.PString(root)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.api.Mixin
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ final case class CodeGeneratorRequest(
case 3 => compilerVersion.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.compiler.plugin.CodeGeneratorRequest
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final case class CodeGeneratorResponse(
case 15 => _root_.scalapb.descriptors.PRepeated(file.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.compiler.plugin.CodeGeneratorResponse
}

Expand Down Expand Up @@ -263,7 +263,7 @@ object CodeGeneratorResponse extends scalapb.GeneratedMessageCompanion[com.googl
case 15 => content.map(_root_.scalapb.descriptors.PString).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.compiler.plugin.CodeGeneratorResponse.File
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final case class Version(
case 4 => suffix.map(_root_.scalapb.descriptors.PString).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.compiler.plugin.Version
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ final case class DescriptorProto(
case 10 => _root_.scalapb.descriptors.PRepeated(reservedName.map(_root_.scalapb.descriptors.PString)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.DescriptorProto
}

Expand Down Expand Up @@ -345,7 +345,7 @@ object DescriptorProto extends scalapb.GeneratedMessageCompanion[com.google.prot
case 2 => end.map(_root_.scalapb.descriptors.PInt).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.DescriptorProto.ExtensionRange
}

Expand Down Expand Up @@ -462,7 +462,7 @@ object DescriptorProto extends scalapb.GeneratedMessageCompanion[com.google.prot
case 2 => end.map(_root_.scalapb.descriptors.PInt).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.DescriptorProto.ReservedRange
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final case class EnumDescriptorProto(
case 3 => options.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.EnumDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final case class EnumOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.EnumOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final case class EnumValueDescriptorProto(
case 3 => options.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.EnumValueDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final case class EnumValueOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.EnumValueOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ final case class FieldDescriptorProto(
case 8 => options.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.FieldDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ final case class FieldOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.FieldOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ final case class FileDescriptorProto(
case 12 => syntax.map(_root_.scalapb.descriptors.PString).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.FileDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final case class FileDescriptorSet(
case 1 => _root_.scalapb.descriptors.PRepeated(file.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.FileDescriptorSet
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ final case class FileOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.FileOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final case class GeneratedCodeInfo(
case 1 => _root_.scalapb.descriptors.PRepeated(annotation.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.GeneratedCodeInfo
}

Expand Down Expand Up @@ -238,7 +238,7 @@ object GeneratedCodeInfo extends scalapb.GeneratedMessageCompanion[com.google.pr
case 4 => end.map(_root_.scalapb.descriptors.PInt).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.GeneratedCodeInfo.Annotation
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final case class MessageOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.MessageOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ final case class MethodDescriptorProto(
case 6 => serverStreaming.map(_root_.scalapb.descriptors.PBoolean).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.MethodDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ final case class MethodOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.MethodOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final case class OneofDescriptorProto(
case 2 => options.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.OneofDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final case class OneofOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.OneofOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final case class ServiceDescriptorProto(
case 3 => options.map(_.toPMessage).getOrElse(_root_.scalapb.descriptors.PEmpty)
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.ServiceDescriptorProto
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final case class ServiceOptions(
case 999 => _root_.scalapb.descriptors.PRepeated(uninterpretedOption.map(_.toPMessage)(_root_.scala.collection.breakOut))
}
}
override def toString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def toProtoString: String = _root_.scalapb.TextFormat.printToUnicodeString(this)
def companion = com.google.protobuf.descriptor.ServiceOptions
}

Expand Down
Loading

0 comments on commit 19a10f5

Please sign in to comment.