Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-line support to toString support #47

Merged
merged 1 commit into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/src/main/scala/CodecCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class CodecCodeGen(codecParents: List[String],
s"""${genPackage(s)}
|trait $name $parents
|object $name extends $name""".stripMargin
val syntheticDefinition = Interface(name, "Scala", None, VersionNumber("0.0.0"), Nil, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil)
val syntheticDefinition = Interface(name, "Scala", None, VersionNumber("0.0.0"), Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil)
ListMap(new File(genFile(s, syntheticDefinition).getParentFile, s"$name.scala") -> code)
}

Expand Down
6 changes: 3 additions & 3 deletions library/src/main/scala/JavaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ class JavaCodeGen(lazyInterface: String, optionalInterface: String) extends Code
|}""".stripMargin
}

private def genToString(cl: ClassLike, superFields: List[Field], toString: Option[String]) = {
val body = toString getOrElse {
private def genToString(cl: ClassLike, superFields: List[Field], toString: List[String]) = {
val body = if (toString.isEmpty) {
val allFields = superFields ++ cl.fields
if (allFields exists (_.tpe.lzy)) {
"return super.toString(); // Avoid evaluating lazy members in toString to avoid circularity."
Expand All @@ -260,7 +260,7 @@ class JavaCodeGen(lazyInterface: String, optionalInterface: String) extends Code
s""" + "${f.name}: " + ${f.name}()"""
}.mkString(s"""return "${cl.name}(" """, " + \", \"", " + \")\";")
}
}
} else toString mkString s"$EOL "

s"""public String toString() {
| $body
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/scala/ScalaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class ScalaCodeGen(scalaArray: String, genFile: Definition => File, sealProtocol
|}""".stripMargin
}

private def genToString(cl: ClassLike, superFields: List[Field], toString: Option[String]) = {
val body = toString getOrElse {
private def genToString(cl: ClassLike, superFields: List[Field], toString: List[String]) = {
val body = if (toString.isEmpty) {
val allFields = superFields ++ cl.fields
if (allFields exists (_.tpe.lzy)) {
s"super.toString // Avoid evaluating lazy members in toString to avoid circularity."
Expand All @@ -197,7 +197,7 @@ class ScalaCodeGen(scalaArray: String, genFile: Definition => File, sealProtocol
val fieldsToString = allFields.map(f => bq(f.name)).mkString(" + ", """ + ", " + """, " + ")
s""""${cl.name}("$fieldsToString")""""
}
}
} else toString mkString s"$EOL "

s"""override def toString: String = {
| $body
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/scala/SchemaData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ case class Interface(name: String,
messages: List[Message],
children: List[Definition],
extra: List[String],
toStringBody: Option[String],
toStringBody: List[String],
extraCompanion: List[String],
parents: List[String],
parentsCompanion: List[String]) extends ClassLike
Expand All @@ -139,7 +139,7 @@ object Interface extends Parser[Interface] {
json ->* "messages" map Message.parse,
json ->* "types" map Definition.parse,
json multiLineOpt "extra" getOrElse Nil,
json ->? "toString",
json multiLineOpt "toString" getOrElse Nil,
json multiLineOpt "extraCompanion" getOrElse Nil,
json multiLineOpt "parents" getOrElse Nil,
json multiLineOpt "parentsCompanion" getOrElse Nil)
Expand All @@ -162,7 +162,7 @@ case class Record(name: String,
doc: List[String],
fields: List[Field],
extra: List[String],
toStringImpl: Option[String],
toStringImpl: List[String],
extraCompanion: List[String],
parents: List[String],
parentsCompanion: List[String]) extends ClassLike
Expand All @@ -176,7 +176,7 @@ object Record extends Parser[Record] {
json multiLineOpt "doc" getOrElse Nil,
json ->* "fields" map Field.parse,
json multiLineOpt "extra" getOrElse Nil,
json ->? "toString",
json multiLineOpt "toString" getOrElse Nil,
json multiLineOpt "extraCompanion" getOrElse Nil,
json multiLineOpt "parents" getOrElse Nil,
json multiLineOpt "parentsCompanion" getOrElse Nil)
Expand Down
16 changes: 8 additions & 8 deletions library/src/test/scala/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SchemaSpec extends FlatSpec {
(abstractMethods.size === 0) &&
(children.size === 0) &&
(extra === List()) &&
(toString === None) &&
(toString === Nil) &&
(extraCompanion === Nil) &&
(parents === Nil) &&
(parentsCompanion === Nil))
Expand All @@ -46,7 +46,7 @@ class SchemaSpec extends FlatSpec {
(doc === List()) &&
(fields.size === 0) &&
(extra === List()) &&
(toString === None) &&
(toString === Nil) &&
(extraCompanion === Nil) &&
(parents === Nil) &&
(parentsCompanion === Nil))
Expand Down Expand Up @@ -85,7 +85,7 @@ class SchemaSpec extends FlatSpec {
(abstractMethods.size === 0) &&
(children.size === 0) &&
(extra === List("// Some extra code...")) &&
(toString === Some("return \"custom\";")) &&
(toString === List("return \"custom\";")) &&
(extraCompanion === List("// Some extra companion code...")) &&
(parents === List("Interface1", "Interface2")) &&
(parentsCompanion === List("CompanionInterface1", "CompanionInterface2")))
Expand All @@ -104,9 +104,9 @@ class SchemaSpec extends FlatSpec {
(abstractMethods.size === 0) &&
(children.size === 1) &&
(children(0) === Record("childRecord", "Scala", None, VersionNumber("0.0.0"), Nil,
Field("x", Nil, TpeRef("int", false, false, false), Field.emptyVersion, None) :: Nil, Nil, None, Nil, Nil, Nil)) &&
Field("x", Nil, TpeRef("int", false, false, false), Field.emptyVersion, None) :: Nil, Nil, Nil, Nil, Nil, Nil)) &&
(extra === List()) &&
(toString === None) &&
(toString === Nil) &&
(extraCompanion === Nil) &&
(parents === Nil) &&
(parentsCompanion === Nil))
Expand All @@ -122,9 +122,9 @@ class SchemaSpec extends FlatSpec {
(fields.size === 0) &&
(abstractMethods.size === 0) &&
(children.size === 1) &&
(children(0) === Interface("nestedProtocol", "Scala", None, VersionNumber("0.0.0"), Nil, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil)) &&
(children(0) === Interface("nestedProtocol", "Scala", None, VersionNumber("0.0.0"), Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil)) &&
(extra === List()) &&
(toString === None) &&
(toString === Nil) &&
(extraCompanion === Nil) &&
(parents === Nil) &&
(parentsCompanion === Nil))
Expand All @@ -141,7 +141,7 @@ class SchemaSpec extends FlatSpec {
(fields.size === 1) &&
(fields(0) === Field("field", Nil, TpeRef("java.net.URL", false, false, false), Field.emptyVersion, None)) &&
(extra === List("// Some extra code...")) &&
(toString === None) &&
(toString === Nil) &&
(extraCompanion === Nil) &&
(parents === Nil) &&
(parentsCompanion === Nil))
Expand Down
7 changes: 7 additions & 0 deletions notes/0.2.8/multiline-toString.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

### enhancements

- Adds multi-line support to the custom toString support. [#47][47] by [@dwijnand][@dwijnand]

[47]: https://github.com/sbt/sbt-datatype/pull/47
[@dwijnand]: http://github.com/dwijnand