forked from playframework/twirl
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scala3): WIP working on scala 3 cross compilation. playframework…
- Loading branch information
Showing
16 changed files
with
1,035 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 0 additions & 3 deletions
3
...a/play/twirl/api/TwirlHelperImports.scala → ...2/play/twirl/api/TwirlHelperImports.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
api/shared/src/main/scala-2.13/play/twirl/api/BaseScalaTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import java.util.Optional | ||
import scala.collection.immutable | ||
import scala.jdk.CollectionConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) { | ||
// The overloaded methods are here for speed. The compiled templates | ||
// can take advantage of them for a 12% performance boost | ||
def _display_(x: AnyVal): T = format.escape(x.toString) | ||
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x) | ||
def _display_(x: Unit): T = format.empty | ||
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString()) | ||
def _display_(x: T): T = if (x eq null) format.empty else x | ||
|
||
def _display_(o: Any)(implicit m: ClassTag[T]): T = { | ||
o match { | ||
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T] | ||
case () => format.empty | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case key: Optional[_] => | ||
(if (key.isPresent) Some(key.get) else None) match { | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case _ => format.empty | ||
} | ||
case xml: scala.xml.NodeSeq => format.raw(xml.toString()) | ||
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_)) | ||
case escapeds: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList) | ||
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList) | ||
case escapeds: java.util.List[_] => | ||
format.fill(escapeds.asScala.map(_display_).toList) | ||
case string: String => format.escape(string) | ||
case v if v != null => format.escape(v.toString) | ||
case _ => format.empty | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
api/shared/src/main/scala-2.13/play/twirl/api/TwirlHelperImports.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package play.twirl.api | ||
|
||
import scala.language.implicitConversions | ||
|
||
/** | ||
* Imports for useful Twirl helpers. | ||
*/ | ||
object TwirlHelperImports { | ||
|
||
/** Allows Java collections to be used as if they were Scala collections. */ | ||
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = { | ||
import scala.jdk.CollectionConverters._ | ||
x.asScala | ||
} | ||
|
||
/** Allows inline formatting of java.util.Date */ | ||
implicit class TwirlRichDate(date: java.util.Date) { | ||
def format(pattern: String): String = { | ||
new java.text.SimpleDateFormat(pattern).format(date) | ||
} | ||
} | ||
|
||
/** Adds a when method to Strings to control when they are rendered. */ | ||
implicit class TwirlRichString(string: String) { | ||
def when(predicate: => Boolean): String = { | ||
predicate match { | ||
case true => string | ||
case false => "" | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
api/shared/src/main/scala-3/play/twirl/api/BaseScalaTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import java.util.Optional | ||
import scala.collection.immutable | ||
import scala.jdk.CollectionConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) { | ||
// The overloaded methods are here for speed. The compiled templates | ||
// can take advantage of them for a 12% performance boost | ||
def _display_(x: AnyVal): T = format.escape(x.toString) | ||
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x) | ||
def _display_(x: Unit): T = format.empty | ||
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString()) | ||
def _display_(x: T): T = if (x eq null) format.empty else x | ||
|
||
def _display_(o: Any)(implicit m: ClassTag[T]): T = { | ||
o match { | ||
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T] | ||
case () => format.empty | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case key: Optional[_] => | ||
(if (key.isPresent) Some(key.get) else None) match { | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case null => format.empty | ||
} | ||
case xml: scala.xml.NodeSeq => format.raw(xml.toString()) | ||
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_)) | ||
case escapeds: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList) | ||
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList) | ||
case escapeds: java.util.List[_] => | ||
format.fill(escapeds.asScala.map(_display_).toList) | ||
case string: String => format.escape(string) | ||
case v if v != null => format.escape(v.toString) | ||
case null => format.empty | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
api/shared/src/main/scala-3/play/twirl/api/TwirlHelperImports.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package play.twirl.api | ||
|
||
import scala.language.implicitConversions | ||
|
||
/** | ||
* Imports for useful Twirl helpers. | ||
*/ | ||
object TwirlHelperImports { | ||
|
||
/** Allows Java collections to be used as if they were Scala collections. */ | ||
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = { | ||
import scala.jdk.CollectionConverters._ | ||
x.asScala | ||
} | ||
|
||
/** Allows inline formatting of java.util.Date */ | ||
implicit class TwirlRichDate(date: java.util.Date) { | ||
def format(pattern: String): String = { | ||
new java.text.SimpleDateFormat(pattern).format(date) | ||
} | ||
} | ||
|
||
/** Adds a when method to Strings to control when they are rendered. */ | ||
implicit class TwirlRichString(string: String) { | ||
def when(predicate: => Boolean): String = { | ||
predicate match { | ||
case true => string | ||
case false => "" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.