Skip to content

Commit ade1a6b

Browse files
authored
Merge branch 'master' into master
2 parents 98f72d5 + eea7eba commit ade1a6b

File tree

137 files changed

+4107
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+4107
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ project/plugins/project/
1818
.idea
1919
/project/
2020
out
21+
*.iml
2122

2223
# Ignore files used by Metals and Visual Studio Code
2324
.bloop

.scalafmt.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
version = 2.4.2
1+
version = 2.5.0
2+
continuationIndent.defnSite = 2
3+
continuationIndent.ctorSite = 2
4+
continuationIndent.extendSite = 2

build.sbt

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
ThisBuild / scalaVersion := "2.12.7"
2-
ThisBuild / version := "1.0-SNAPSHOT"
3-
ThisBuild / organization := "com.baeldung"
1+
ThisBuild / scalaVersion := "2.12.7"
2+
ThisBuild / version := "1.0-SNAPSHOT"
3+
ThisBuild / organization := "com.baeldung"
44
ThisBuild / organizationName := "core-scala"
55

6-
val scalaTest = "org.scalatest" %% "scalatest" % "3.1.2" % Test
7-
val junit = "com.novocode" % "junit-interface" % "0.11" % "test"
6+
val scalaTest = "org.scalatest" %% "scalatest" % "3.1.2" % Test
7+
val junit = "com.novocode" % "junit-interface" % "0.11" % "test"
88
val catsEffect = "org.typelevel" % "cats-effect_2.12" % "2.1.4"
9-
val catsCore= "org.typelevel" % "cats-effect_2.12" % "2.1.4"
9+
val catsCore = "org.typelevel" % "cats-effect_2.12" % "2.1.4"
1010

1111
lazy val scala_core = (project in file("scala-core"))
1212
.settings(
@@ -16,21 +16,40 @@ lazy val scala_core = (project in file("scala-core"))
1616
scalaTest,
1717
junit,
1818
catsCore,
19-
catsEffect)
19+
catsEffect
20+
)
2021
)
2122

2223
lazy val scala_core_2 = (project in file("scala-core-2"))
2324
.settings(
2425
name := "scala-core-2",
2526
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
2627
libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test,
27-
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test",
28-
)
28+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
29+
)
2930

3031
lazy val scala_core_3 = (project in file("scala-core-3"))
3132
.settings(
3233
name := "scala-core-3",
3334
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
35+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test",
36+
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.12.7",
37+
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "2.0.0-M1"
38+
)
39+
40+
lazy val scala_core_4 = (project in file("scala-core-4"))
41+
.settings(
42+
name := "scala-core-4",
43+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
44+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test",
45+
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.12.7",
46+
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "2.0.0-M1"
47+
)
48+
49+
lazy val scala_core_io = (project in file("scala-core-io"))
50+
.settings(
51+
name := "scala-core-io",
52+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
3453
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
3554
)
3655

@@ -39,6 +58,7 @@ lazy val scala_core_oop = (project in file("scala-core-oop"))
3958
name := "scala-core-oop",
4059
libraryDependencies ++=
4160
Seq(
61+
catsCore,
4262
scalaTest,
4363
junit)
4464
)
@@ -83,26 +103,30 @@ lazy val scala_test = (project in file("scala-test"))
83103
lazy val scala_akka = (project in file("scala-akka"))
84104
.settings(
85105
name := "scala-akka",
86-
libraryDependencies += "com.typesafe.akka" % "akka-actor-typed_2.12" % "2.6.6",
87-
libraryDependencies += "com.typesafe.akka" % "akka-actor-testkit-typed_2.12" % "2.6.6" % Test,
106+
libraryDependencies += "com.typesafe.akka" % "akka-actor-typed_2.12" % "2.6.9",
107+
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3",
108+
libraryDependencies += "com.typesafe.akka" % "akka-actor-testkit-typed_2.12" % "2.6.9" % Test,
88109
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
89110
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
90111
)
91112

92113
val monocleVersion = "2.0.4"
114+
val slickVersion = "3.3.2"
93115
val scalazVersion = "7.3.2"
94116
lazy val scala_libraries = (project in file("scala-libraries"))
95-
.settings(
96-
name := "scala-libraries",
97-
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
98-
libraryDependencies ++= Seq(
99-
"com.github.julien-truffaut" %% "monocle-core" % monocleVersion,
100-
"com.github.julien-truffaut" %% "monocle-macro" % monocleVersion,
101-
"com.github.julien-truffaut" %% "monocle-law" % monocleVersion % "test",
102-
"org.scalaz" %% "scalaz-core" % scalazVersion,
103-
"junit" % "junit" % "4.13" % Test
104-
)
105-
)
117+
.settings(
118+
name := "scala-libraries",
119+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
120+
libraryDependencies ++= Seq(
121+
"com.github.julien-truffaut" %% "monocle-core" % monocleVersion,
122+
"com.github.julien-truffaut" %% "monocle-macro" % monocleVersion,
123+
"com.github.julien-truffaut" %% "monocle-law" % monocleVersion % "test",
124+
"com.typesafe.slick" %% "slick" % slickVersion,
125+
"com.h2database" % "h2" % "1.4.200",
126+
"org.scalaz" %% "scalaz-core" % scalazVersion,
127+
"junit" % "junit" % "4.13" % Test
128+
)
129+
)
106130

107131
lazy val scala_strings = (project in file("scala-strings"))
108132
.settings(
@@ -118,3 +142,4 @@ lazy val scala_design_patterns = (project in file("scala-design-patterns"))
118142
libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test,
119143
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test",
120144
)
145+

play-scala/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
### Relevant Articles:
22

33
- [Introduction to the Play Framework in Scala](https://www.baeldung.com/scala/play-framework-intro)
4+
- [Building a REST API in Scala with Play Framework](https://www.baeldung.com/scala/play-rest-api)
5+
- [Access Play Configuration in Scala](https://www.baeldung.com/scala/access-play-configuration)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
logs
2+
target
3+
/.idea
4+
/.idea_modules
5+
/.classpath
6+
/.project
7+
/.settings
8+
/RUNNING_PID
9+
/.g8
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package controllers
2+
3+
import javax.inject._
4+
import play.api.mvc._
5+
import services.MyService
6+
7+
/**
8+
* This controller creates an `Action` to handle HTTP requests to the
9+
* application's home page.
10+
*/
11+
@Singleton
12+
class HomeController @Inject() (
13+
val myService: MyService,
14+
val controllerComponents: ControllerComponents
15+
) extends BaseController {
16+
17+
/**
18+
* Create an Action to render an HTML page.
19+
*
20+
* The configuration in the `routes` file means that this method
21+
* will be called when the application receives a `GET` request with
22+
* a path of `/`.
23+
*/
24+
def index(): Action[AnyContent] =
25+
Action { implicit request: Request[AnyContent] =>
26+
Ok(views.html.index(myService.getPlayerInfoVersion1))
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package services
2+
3+
import java.util.Date
4+
5+
import com.typesafe.config.Config
6+
import javax.inject.Inject
7+
import play.api.{ConfigLoader, Configuration}
8+
9+
import scala.util.Try
10+
11+
object ISO8601DateConfigLoader {
12+
implicit val iso8601DateConfigLoader: ConfigLoader[Date] = {
13+
ConfigLoader(_.getString)
14+
.map[Date](javax.xml.bind.DatatypeConverter.parseDateTime(_).getTime)
15+
}
16+
}
17+
18+
case class PlayerInfo(
19+
name: String,
20+
email: String,
21+
age: Int,
22+
signUpDate: Date,
23+
twitterHandle: Option[String] = None
24+
)
25+
26+
object PlayerInfo {
27+
implicit val playerInfoConfigLoader: ConfigLoader[PlayerInfo] =
28+
(rootConfig: Config, path: String) => {
29+
val config = rootConfig.getConfig(path)
30+
new PlayerInfo(
31+
config.getString("name"),
32+
config.getString("email"),
33+
config.getInt("age"),
34+
javax.xml.bind.DatatypeConverter
35+
.parseDateTime(config.getString("signUpDate"))
36+
.getTime,
37+
Try(config.getString("twitterHandle")).toOption
38+
)
39+
}
40+
}
41+
42+
class MyService @Inject() (configuration: Configuration) {
43+
44+
def getPlayerInfoVersion1: PlayerInfo = {
45+
PlayerInfo(
46+
configuration.get[String]("player.name"),
47+
configuration.get[String]("player.email"),
48+
configuration.get[Int]("player.age"),
49+
configuration.get[Date]("player.signUpDate")(
50+
ISO8601DateConfigLoader.iso8601DateConfigLoader
51+
),
52+
configuration.get[Option[String]]("player.twitterHandle")
53+
)
54+
}
55+
56+
def getPlayerInfoVersion2: PlayerInfo = {
57+
configuration.get[PlayerInfo]("player")
58+
}
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@(playerInfo: services.PlayerInfo)
2+
3+
@main("Welcome to Play") {
4+
<h1>Welcome to Play: @playerInfo.name</h1>
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@*
2+
* This template is called from the `index` template. This template
3+
* handles the rendering of the page header and body tags. It takes
4+
* two arguments, a `String` for the title of the page and an `Html`
5+
* object to insert into the body of the page.
6+
*@
7+
@(title: String)(content: Html)
8+
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
@* Here's where we render the page title `String`. *@
13+
<title>@title</title>
14+
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
15+
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
16+
17+
</head>
18+
<body>
19+
@* And here's where we render the `Html` object containing
20+
* the page content. *@
21+
@content
22+
23+
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
24+
</body>
25+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name := """configuration-access"""
2+
organization := "com.baeldung"
3+
4+
version := "1.0-SNAPSHOT"
5+
6+
lazy val root = (project in file(".")).enablePlugins(PlayScala)
7+
8+
scalaVersion := "2.13.3"
9+
10+
libraryDependencies += guice
11+
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
12+
libraryDependencies += "org.mockito" % "mockito-scala_2.13" % "1.15.0" % Test
13+
14+
// Adds additional packages into Twirl
15+
//TwirlKeys.templateImports += "com.baeldung.controllers._"
16+
17+
// Adds additional packages into conf/routes
18+
// play.sbt.routes.RoutesKeys.routesImport += "com.baeldung.binders._"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://www.playframework.com/documentation/latest/Configuration
2+
player {
3+
name = "Player 1"
4+
5+
age = 18
6+
twitterHandle = null
7+
signUpDate = "2020-09-22T10:10:06"
8+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
2+
<configuration>
3+
4+
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
5+
6+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
7+
<file>${application.home:-.}/logs/application.log</file>
8+
<encoder>
9+
<charset>UTF-8</charset>
10+
<pattern>
11+
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
12+
</pattern>
13+
</encoder>
14+
</appender>
15+
16+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
17+
<withJansi>true</withJansi>
18+
<encoder>
19+
<charset>UTF-8</charset>
20+
<pattern>
21+
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
22+
</pattern>
23+
</encoder>
24+
</appender>
25+
26+
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
27+
<appender-ref ref="FILE" />
28+
</appender>
29+
30+
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
31+
<appender-ref ref="STDOUT" />
32+
</appender>
33+
34+
<logger name="play" level="INFO" />
35+
<logger name="application" level="DEBUG" />
36+
37+
<root level="WARN">
38+
<!--<appender-ref ref="ASYNCFILE" />-->
39+
<appender-ref ref="ASYNCSTDOUT" />
40+
</root>
41+
42+
</configuration>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# https://www.playframework.com/documentation/latest/ScalaI18N
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Routes
2+
# This file defines all application routes (Higher priority routes first)
3+
# https://www.playframework.com/documentation/latest/ScalaRouting
4+
# ~~~~
5+
6+
# An example controller showing a sample home page
7+
GET / controllers.HomeController.index()
8+
9+
# Map static resources from the /public folder to the /assets URL path
10+
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.3.13
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.2")
2+
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0")
3+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0")
Loading

play-scala/configuration-access/public/javascripts/main.js

Whitespace-only changes.

play-scala/configuration-access/public/stylesheets/main.css

Whitespace-only changes.

0 commit comments

Comments
 (0)