Skip to content

Commit 50a408e

Browse files
authored
introduce scalafmt usage (#166)
1 parent 2663419 commit 50a408e

7 files changed

Lines changed: 153 additions & 55 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ jobs:
1212
scala_versions: '["2.13.18"]'
1313
# TODO no sbt-version-policy in this repo, so binary compatibility was never checked
1414
version_policy_check: false
15-
# TODO no sbt-scalafmt in this repo, so formatting was never checked
16-
scalafmt_check: false

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
release:
10-
uses: evolution-gaming/scala-github-actions/.github/workflows/release.yml@v4
10+
uses: evolution-gaming/scala-github-actions/.github/workflows/release.yml@v5
1111
secrets: inherit
1212
with:
1313
verify_sbt_command: 'clean; check'

.scalafmt.conf

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,96 @@
1-
version = "3.11.5"
2-
runner.dialect = scala213
1+
# Main goals:
2+
# - nicer commit diffs (trailing commas, no alignment for pattern matching, force new lines)
3+
# - better interop with default IntelliJ IDEA setup (matching import and modifiers sorting logic)
4+
# - better developer experience on laptop screens (like 16' MBPs) with IntelliJ IDEA (line wraps)
5+
6+
version = 3.11.5
7+
8+
runner.dialect = scala213source3
9+
10+
# only format files tracked by git
11+
project.git = true
12+
13+
maxColumn = 120
14+
trailingCommas = always
15+
16+
preset = default
17+
# do not align to make nicer commit diffs
18+
align.preset = none
19+
20+
indent {
21+
# altering defnSite and extendSite to have this:
22+
# final class MyErr extends RuntimeException(
23+
# "super error message",
24+
# )
25+
# instead of this:
26+
# final class MyErr extends RuntimeException(
27+
# "super error message",
28+
# )
29+
defnSite = 2
30+
extendSite = 0
31+
}
32+
33+
spaces {
34+
# makes string interpolation with curlies more visually distinct
35+
inInterpolatedStringCurlyBraces = true
36+
}
37+
38+
newlines {
39+
# keep author new lines where possible
40+
source = keep
41+
# force new line after "(implicit" for multi-line arg lists
42+
implicitParamListModifierForce = [after]
43+
avoidForSimpleOverflow = [
44+
tooLong, # if the line would be too long even after newline inserted, do nothing
45+
slc, # do nothing if overflow caused by single line comment
46+
]
47+
}
48+
49+
verticalMultiline {
50+
atDefnSite = true
51+
arityThreshold = 4 # more than 3 args in a list will be turned vertical
52+
newlineAfterOpenParen = true # for nicer commit diffs
53+
}
54+
55+
# for nicer commit diffs - forces new line before last parenthesis:
56+
# class MyCls(
57+
# arg1: String,
58+
# arg2: String,
59+
# ) extends MyTrait {
60+
#
61+
# without it:
62+
# class MyCls(
63+
# arg1: String,
64+
# arg2: String) extends MyTrait {
65+
danglingParentheses.exclude = []
66+
67+
docstrings {
68+
# easier to view diffs in IDEA on 16' MBP screen if docs max line are shorter than code
69+
wrapMaxColumn = 100
70+
# next settings make it similar to the default IDEA javadoc formatting
71+
style = Asterisk
72+
oneline = unfold
73+
blankFirstLine = unfold
74+
}
75+
76+
rewrite.rules = [
77+
Imports,
78+
RedundantParens,
79+
SortModifiers,
80+
prefercurlyfors,
81+
]
82+
83+
# put visibility modifier first
84+
rewrite.sortModifiers.preset = styleGuide
85+
86+
# Import sorting as similar as possible to scalafix's "OrganizeImports.preset = INTELLIJ_2020_3".
87+
# Scalafix is not used as its commands mess up "all .." build aliases and it takes long time to run,
88+
# while its code semantic based features are not needed here.
89+
# I.e. detection of unused imports is done with Scala compiler options.
90+
rewrite.imports {
91+
sort = ascii
92+
groups = [
93+
[".*"],
94+
["java\\..*", "javax\\..*", "scala\\..*"],
95+
]
96+
}

build.sbt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@ ThisBuild / scalaVersion := ScalaVersions.head
44

55
lazy val commonSettings = Seq(
66
organization := "com.evolutiongaming",
7-
homepage := Some(
8-
uri("https://github.com/evolution-gaming/akka-http-play-json")
9-
),
7+
homepage := Some(uri("https://github.com/evolution-gaming/akka-http-play-json")),
108
startYear := Some(2016),
119
publishMavenStyle := true,
1210
organizationName := "Evolution",
1311
organizationHomepage := Some(uri("https://evolution.com")),
1412
publishTo := Some(Resolver.evolutionReleases),
15-
licenses := Seq(
16-
("Apache-2.0", uri("http://www.apache.org/licenses/LICENSE-2.0"))
17-
),
13+
licenses := Seq(("Apache-2.0", uri("http://www.apache.org/licenses/LICENSE-2.0"))),
1814
crossScalaVersions := ScalaVersions,
1915
Compile / doc / scalacOptions ++= Seq(
2016
"-groups",
2117
"-implicits",
22-
"-no-link-warnings"
18+
"-no-link-warnings",
2319
),
2420
libraryDependencies ++= Seq(
25-
"org.playframework" %% "play-json" % "3.0.6"
26-
)
21+
"org.playframework" %% "play-json" % "3.0.6",
22+
),
2723
)
2824

2925
lazy val root = (project in file("."))
@@ -32,8 +28,8 @@ lazy val root = (project in file("."))
3228
moduleName := "akka-http-play-json",
3329
libraryDependencies ++= Seq(
3430
"com.typesafe.akka" %% "akka-stream" % "2.6.21", // `2.6.21` is last open source version before switch to BSL
35-
"com.typesafe.akka" %% "akka-http" % "10.2.10" // `10.2.10` is last open source version before switch to BSL
36-
)
31+
"com.typesafe.akka" %% "akka-http" % "10.2.10", // `10.2.10` is last open source version before switch to BSL
32+
),
3733
)
3834

3935
lazy val `root-pekko` = (project in file("pekko"))
@@ -47,13 +43,14 @@ lazy val `root-pekko` = (project in file("pekko"))
4743
"org.apache.pekko" %% "pekko-http" % "1.4.0",
4844
"com.evolution" %% "akka-to-pekko-adapter-stream" % "1.0.4",
4945
"com.evolution" %% "akka-to-pekko-adapter-http" % "1.0.4",
50-
"com.evolution" %% "akka-to-pekko-adapter-actor" % "1.0.4"
51-
)
46+
"com.evolution" %% "akka-to-pekko-adapter-actor" % "1.0.4",
47+
),
5248
)
5349

5450
//addCommandAlias("check", "all versionPolicyCheck Compile/doc")
55-
addCommandAlias("check", "all root/compile root-pekko/compile")
51+
addCommandAlias("check", "all scalafmtCheckRepo")
52+
addCommandAlias("fmt", "+scalafmtRepo")
5653
addCommandAlias(
5754
"build",
58-
"all root/compile root-pekko/compile root/publish root-pekko/publish"
55+
"all root/testFull root-pekko/testFull root/compile root-pekko/compile root/publish root-pekko/publish",
5956
)

pekko/src/main/scala/org/apache/pekko/http/scaladsl/marshallers/playjson/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package org.apache.pekko.http.scaladsl.marshallers
33
package object playjson {
44
type PlayJsonSupport = akka.http.scaladsl.marshallers.playjson.PlayJsonSupport
55
val PlayJsonSupport
6-
: akka.http.scaladsl.marshallers.playjson.PlayJsonSupport.type =
6+
: akka.http.scaladsl.marshallers.playjson.PlayJsonSupport.type =
77
akka.http.scaladsl.marshallers.playjson.PlayJsonSupport
88
}

project/plugins.sbt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
addSbtPlugin("com.github.sbt" % "sbt-dynver" % "5.1.1")
2+
23
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.4.4")
4+
35
addSbtPlugin("com.evolution" % "sbt-scalac-opts-plugin" % "0.2.0")
6+
47
addSbtPlugin("com.evolution" % "sbt-artifactory-plugin" % "0.1.2")
8+
9+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.6.2")

src/main/scala/akka/http/scaladsl/marshallers/playjson/PlayJsonSupport.scala

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package akka.http.scaladsl.marshallers.playjson
22

33
import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
4-
import akka.http.scaladsl.model._
54
import akka.http.scaladsl.model.MediaTypes.`application/json`
5+
import akka.http.scaladsl.model._
66
import akka.http.scaladsl.server.{RejectionError, ValidationRejection}
77
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
88
import akka.util.ByteString
99
import play.api.libs.json._
1010

11-
/** Automatic to and from JSON marshalling/unmarshalling using an in-scope
12-
* *play-json* protocol.
13-
*/
11+
/**
12+
* Automatic to and from JSON marshalling/unmarshalling using an in-scope *play-json* protocol.
13+
*/
1414
trait PlayJsonSupport {
1515
import PlayJsonSupport._
1616

@@ -19,7 +19,7 @@ trait PlayJsonSupport {
1919
.forContentTypes(`application/json`)
2020
.mapWithCharset {
2121
case (ByteString.empty, _) => throw Unmarshaller.NoContentException
22-
case (data, charset) => data.decodeString(charset.nioCharset.name)
22+
case (data, charset) => data.decodeString(charset.nioCharset.name)
2323
}
2424

2525
private val jsonStringMarshaller: ToEntityMarshaller[String] = {
@@ -37,45 +37,49 @@ trait PlayJsonSupport {
3737
}
3838
}
3939

40-
/** HTTP entity => `A`
41-
*
42-
* @param reads
43-
* reader for `A`
44-
* @tparam A
45-
* type to decode
46-
* @return
47-
* unmarshaller for `A`
48-
*/
49-
implicit def playJsonUnmarshaller[A](implicit
50-
reads: Reads[A]
40+
/**
41+
* HTTP entity => `A`
42+
*
43+
* @param reads
44+
* reader for `A`
45+
* @tparam A
46+
* type to decode
47+
* @return
48+
* unmarshaller for `A`
49+
*/
50+
implicit def playJsonUnmarshaller[A](
51+
implicit
52+
reads: Reads[A],
5153
): FromEntityUnmarshaller[A] = {
5254
def read(json: JsValue) = reads.reads(json) recoverTotal { error =>
5355
throw RejectionError(
5456
ValidationRejection(
5557
JsError.toJson(error).toString,
56-
Some(PlayJsonError(error))
57-
)
58+
Some(PlayJsonError(error)),
59+
),
5860
)
5961
}
6062

6163
jsonStringUnmarshaller.map(data => read(Json.parse(data)))
6264
}
6365

64-
/** `A` => HTTP entity
65-
*
66-
* @param writes
67-
* writer for `A`
68-
* @param printer
69-
* output generation function, default to `stringify`, could be overridden
70-
* with `Json.prettyPrint`
71-
* @tparam A
72-
* type to encode
73-
* @return
74-
* marshaller for any `A` value
75-
*/
76-
implicit def playJsonMarshaller[A](implicit
77-
writes: Writes[A],
78-
printer: JsValue => String = Json.stringify
66+
/**
67+
* `A` => HTTP entity
68+
*
69+
* @param writes
70+
* writer for `A`
71+
* @param printer
72+
* output generation function, default to `stringify`, could be overridden with
73+
* `Json.prettyPrint`
74+
* @tparam A
75+
* type to encode
76+
* @return
77+
* marshaller for any `A` value
78+
*/
79+
implicit def playJsonMarshaller[A](
80+
implicit
81+
writes: Writes[A],
82+
printer: JsValue => String = Json.stringify,
7983
): ToEntityMarshaller[A] = {
8084

8185
jsonStringMarshaller.compose(printer).compose(writes.writes)
@@ -87,7 +91,7 @@ object PlayJsonSupport extends PlayJsonSupport {
8791
val `application/json; charset=UTF-8`: ContentType.WithCharset =
8892
MediaType.customWithOpenCharset(
8993
"application",
90-
"json"
94+
"json",
9195
) withCharset HttpCharsets.`UTF-8`
9296

9397
case class PlayJsonError(error: JsError) extends RuntimeException {

0 commit comments

Comments
 (0)