Skip to content

Commit 42bc5d9

Browse files
authored
scala3: edit sources on-the-fly in sbt to support @pre213 methods in shared source code (#4113)
1 parent 8d8b94a commit 42bc5d9

File tree

10 files changed

+105
-60
lines changed

10 files changed

+105
-60
lines changed

.github/workflows/validate-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
strategy:
5454
fail-fast: false
5555
matrix:
56-
SCALA_VERSION: [2.13, 3]
56+
SCALA_VERSION: [2.12, 2.13, 3]
5757
JABBA_JDK: [1.8]
5858
steps:
5959
- name: Checkout

akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpMessage.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,27 @@ sealed trait HttpMessage extends jm.HttpMessage {
7272
def discardEntityBytes(system: ClassicActorSystemProvider): HttpMessage.DiscardedEntity = entity.discardBytes()(SystemMaterializer(system).materializer)
7373

7474
/** Returns a copy of this message with the list of headers set to the given ones. */
75-
/*@pre213
76-
def withHeaders(headers: HttpHeader*): Self = withHeaders(headers.toList)*/
77-
78-
/** Returns a copy of this message with the list of headers set to the given ones. */
79-
def withHeaders(headers: immutable.Seq[HttpHeader]): Self
75+
@pre213
76+
def withHeaders(headers: HttpHeader*): Self = withHeaders(headers.toList)
8077

8178
/** Returns a copy of this message with the list of headers set to the given ones. */
8279
@since213
8380
def withHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Self =
8481
withHeaders(firstHeader +: otherHeaders.toList)
8582

83+
/** Returns a copy of this message with the list of headers set to the given ones. */
84+
def withHeaders(headers: immutable.Seq[HttpHeader]): Self
85+
8686
/**
8787
* Returns a new message that contains all of the given default headers which didn't already
8888
* exist (by case-insensitive header name) in this message.
8989
*/
90-
/*@pre213
91-
def withDefaultHeaders(defaultHeaders: HttpHeader*): Self = withDefaultHeaders(defaultHeaders.toList)*/
90+
@pre213
91+
def withDefaultHeaders(defaultHeaders: HttpHeader*): Self = withDefaultHeaders(defaultHeaders.toList)
92+
93+
@since213
94+
def withDefaultHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Self =
95+
withDefaultHeaders(firstHeader +: otherHeaders.toList)
9296

9397
/**
9498
* Returns a new message that contains all of the given default headers which didn't already
@@ -100,10 +104,6 @@ sealed trait HttpMessage extends jm.HttpMessage {
100104
else defaultHeaders.foldLeft(headers) { (acc, h) => if (headers.exists(_ is h.lowercaseName)) acc else h +: acc }
101105
}
102106

103-
@since213
104-
def withDefaultHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Self =
105-
withDefaultHeaders(firstHeader +: otherHeaders.toList)
106-
107107
/** Returns a copy of this message with the attributes set to the given ones. */
108108
def withAttributes(headers: Map[AttributeKey[_], _]): Self
109109

akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/CacheDirective.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ object CacheDirectives {
8383
* http://tools.ietf.org/html/rfc7234#section-5.2.1.4
8484
*/
8585
case object `no-cache` extends SingletonValueRenderable with RequestDirective with ResponseDirective {
86-
/*@pre213
86+
@pre213
8787
def apply(fieldNames: String*): `no-cache` =
88-
new `no-cache`(immutable.Seq(fieldNames: _*))*/
88+
new `no-cache`(immutable.Seq(fieldNames: _*))
8989
@since213
9090
def apply(firstFieldName: String, otherFieldNames: String*): `no-cache` =
9191
new `no-cache`(firstFieldName +: otherFieldNames.toList)
@@ -138,8 +138,8 @@ object CacheDirectives {
138138
*/
139139
final case class `private`(fieldNames: immutable.Seq[String]) extends FieldNamesDirective with ResponseDirective
140140
object `private` {
141-
/*@pre213
142-
def apply(fieldNames: String*): `private` = new `private`(immutable.Seq(fieldNames: _*))*/
141+
@pre213
142+
def apply(fieldNames: String*): `private` = new `private`(immutable.Seq(fieldNames: _*))
143143
@since213
144144
def apply(): `private` = new `private`(immutable.Seq.empty)
145145
@since213

akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/LanguageRange.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ object Language {
7272
val tags = compoundTag.split('-')
7373
new Language(tags.head, immutable.Seq(tags.tail: _*))
7474
} else new Language(compoundTag, immutable.Seq.empty)
75-
/*@pre213
75+
@pre213
7676
def apply(primaryTag: String, subTags: String*): Language =
77-
new Language(primaryTag, immutable.Seq(subTags: _*))*/
77+
new Language(primaryTag, immutable.Seq(subTags: _*))
7878
@since213
7979
def apply(primaryTag: String, firstSubTag: String, otherSubTags: String*): Language =
8080
new Language(primaryTag, firstSubTag +: otherSubTags)

akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/LinkValue.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ final case class LinkValue(uri: Uri, params: immutable.Seq[LinkParam]) extends j
2525
}
2626

2727
object LinkValue {
28-
/*@pre213
29-
def apply(uri: Uri, params: LinkParam*): LinkValue = apply(uri, immutable.Seq(params: _*))*/
28+
@pre213
29+
def apply(uri: Uri, params: LinkParam*): LinkValue = apply(uri, immutable.Seq(params: _*))
3030
@since213
3131
def apply(uri: Uri, firstParam: LinkParam, otherParams: LinkParam*): LinkValue = apply(uri, firstParam +: otherParams)
3232
}

akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ import akka.http.impl.util.JavaMapping.Implicits._
136136

137137
// https://tools.ietf.org/html/rfc7231#section-5.3.2
138138
object Accept extends ModeledCompanion[Accept] {
139-
/*@pre213
139+
@pre213
140140
def apply(mediaRanges: MediaRange*): Accept =
141-
apply(immutable.Seq(mediaRanges: _*))*/
141+
apply(immutable.Seq(mediaRanges: _*))
142142
@since213
143143
def apply(firstMediaRange: MediaRange, otherMediaRanges: MediaRange*): Accept =
144144
apply(firstMediaRange +: otherMediaRanges)
@@ -172,9 +172,9 @@ final case class `Accept-Charset`(charsetRanges: immutable.Seq[HttpCharsetRange]
172172

173173
// https://tools.ietf.org/html/rfc7231#section-5.3.4
174174
object `Accept-Encoding` extends ModeledCompanion[`Accept-Encoding`] {
175-
/*@pre213
175+
@pre213
176176
def apply(encodings: HttpEncodingRange*): `Accept-Encoding` =
177-
apply(immutable.Seq(encodings: _*))*/
177+
apply(immutable.Seq(encodings: _*))
178178
@since213
179179
def apply(): `Accept-Encoding` =
180180
apply(immutable.Seq.empty)
@@ -211,9 +211,9 @@ final case class `Accept-Language`(languages: immutable.Seq[LanguageRange]) exte
211211

212212
// https://tools.ietf.org/html/rfc7233#section-2.3
213213
object `Accept-Ranges` extends ModeledCompanion[`Accept-Ranges`] {
214-
/*@pre213
214+
@pre213
215215
def apply(rangeUnits: RangeUnit*): `Accept-Ranges` =
216-
apply(immutable.Seq(rangeUnits: _*))*/
216+
apply(immutable.Seq(rangeUnits: _*))
217217
@since213
218218
def apply(): `Accept-Ranges` =
219219
apply(immutable.Seq.empty)
@@ -242,9 +242,9 @@ final case class `Access-Control-Allow-Credentials`(allow: Boolean)
242242

243243
// https://www.w3.org/TR/cors/#access-control-allow-headers-response-header
244244
object `Access-Control-Allow-Headers` extends ModeledCompanion[`Access-Control-Allow-Headers`] {
245-
/*@pre213
245+
@pre213
246246
def apply(headers: String*): `Access-Control-Allow-Headers` =
247-
apply(immutable.Seq(headers: _*))*/
247+
apply(immutable.Seq(headers: _*))
248248
@since213
249249
def apply(firstHeader: String, otherHeaders: String*): `Access-Control-Allow-Headers` =
250250
apply(firstHeader +: otherHeaders)
@@ -262,9 +262,9 @@ final case class `Access-Control-Allow-Headers`(headers: immutable.Seq[String])
262262

263263
// https://www.w3.org/TR/cors/#access-control-allow-methods-response-header
264264
object `Access-Control-Allow-Methods` extends ModeledCompanion[`Access-Control-Allow-Methods`] {
265-
/*@pre213
265+
@pre213
266266
def apply(methods: HttpMethod*): `Access-Control-Allow-Methods` =
267-
apply(immutable.Seq(methods: _*))*/
267+
apply(immutable.Seq(methods: _*))
268268
@since213
269269
def apply(firstMethod: HttpMethod, otherMethods: HttpMethod*): `Access-Control-Allow-Methods` =
270270
apply(firstMethod +: otherMethods)
@@ -303,9 +303,9 @@ final case class `Access-Control-Allow-Origin` private (range: HttpOriginRange)
303303

304304
// https://www.w3.org/TR/cors/#access-control-expose-headers-response-header
305305
object `Access-Control-Expose-Headers` extends ModeledCompanion[`Access-Control-Expose-Headers`] {
306-
/*@pre213
306+
@pre213
307307
def apply(headers: String*): `Access-Control-Expose-Headers` =
308-
apply(immutable.Seq(headers: _*))*/
308+
apply(immutable.Seq(headers: _*))
309309
@since213
310310
def apply(firstHeader: String, otherHeaders: String*): `Access-Control-Expose-Headers` =
311311
apply(firstHeader +: otherHeaders)
@@ -331,9 +331,9 @@ final case class `Access-Control-Max-Age`(deltaSeconds: Long) extends jm.headers
331331

332332
// https://www.w3.org/TR/cors/#access-control-request-headers-request-header
333333
object `Access-Control-Request-Headers` extends ModeledCompanion[`Access-Control-Request-Headers`] {
334-
/*@pre213
334+
@pre213
335335
def apply(headers: String*): `Access-Control-Request-Headers` =
336-
apply(immutable.Seq(headers: _*))*/
336+
apply(immutable.Seq(headers: _*))
337337
@since213
338338
def apply(firstHeader: String, otherHeaders: String*): `Access-Control-Request-Headers` =
339339
apply(firstHeader +: otherHeaders)
@@ -366,9 +366,9 @@ final case class Age(deltaSeconds: Long) extends jm.headers.Age with ResponseHea
366366

367367
// https://tools.ietf.org/html/rfc7231#section-7.4.1
368368
object Allow extends ModeledCompanion[Allow] {
369-
/*@pre213
369+
@pre213
370370
def apply(methods: HttpMethod*): Allow =
371-
apply(immutable.Seq(methods: _*))*/
371+
apply(immutable.Seq(methods: _*))
372372
@since213
373373
def apply(): `Allow` =
374374
apply(immutable.Seq.empty)
@@ -546,8 +546,8 @@ final case class `Content-Type` private[http] (contentType: ContentType) extends
546546
object Cookie extends ModeledCompanion[Cookie] {
547547
def apply(first: HttpCookiePair, more: HttpCookiePair*): Cookie = apply(immutable.Seq(first +: more: _*))
548548
def apply(name: String, value: String): Cookie = apply(HttpCookiePair(name, value))
549-
/*@pre213
550-
def apply(values: (String, String)*): Cookie = apply(values.map(HttpCookiePair(_)).toList)*/
549+
@pre213
550+
def apply(values: (String, String)*): Cookie = apply(values.map(HttpCookiePair(_)).toList)
551551
@since213
552552
def apply(first: (String, String), more: (String, String)*): Cookie = apply((first +: more).map(HttpCookiePair(_)))
553553
implicit val cookiePairsRenderer: Renderer[immutable.Iterable[HttpCookiePair]] = Renderer.seqRenderer[HttpCookiePair](separator = "; ") // cache
@@ -695,8 +695,8 @@ final case class `Last-Modified`(date: DateTime) extends jm.headers.LastModified
695695
// https://tools.ietf.org/html/rfc5988#section-5
696696
object Link extends ModeledCompanion[Link] {
697697
def apply(uri: Uri, first: LinkParam, more: LinkParam*): Link = apply(immutable.Seq(LinkValue(uri, first +: more.toList)))
698-
/*@pre213
699-
def apply(values: LinkValue*): Link = apply(immutable.Seq(values: _*))*/
698+
@pre213
699+
def apply(values: LinkValue*): Link = apply(immutable.Seq(values: _*))
700700
@since213
701701
def apply(firstValue: LinkValue, otherValues: LinkValue*): Link = apply(firstValue +: otherValues)
702702

@@ -723,8 +723,8 @@ final case class Location(uri: Uri) extends jm.headers.Location with ResponseHea
723723

724724
// https://tools.ietf.org/html/rfc6454#section-7
725725
object Origin extends ModeledCompanion[Origin] {
726-
/*@pre213
727-
def apply(origins: HttpOrigin*): Origin = apply(immutable.Seq(origins: _*))*/
726+
@pre213
727+
def apply(origins: HttpOrigin*): Origin = apply(immutable.Seq(origins: _*))
728728
@since213
729729
def apply(firstOrigin: HttpOrigin, otherOrigins: HttpOrigin*): Origin = apply(firstOrigin +: otherOrigins)
730730
}

akka-http/src/main/scala/akka/http/scaladsl/server/directives/FormFieldDirectives.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ trait FormFieldDirectives extends FormFieldDirectivesInstances with ToNameRecept
5454
*
5555
* @group form
5656
*/
57-
/*@pre213
57+
@pre213
5858
@deprecated("Use new `formField` overloads with FieldSpec parameters. Kept for binary compatibility", "10.2.0")
59-
private[http] def formField(pdm: FieldMagnet): pdm.Out = formFields(pdm)*/
59+
private[http] def formField(pdm: FieldMagnet): pdm.Out = formFields(pdm)
6060

6161
/**
6262
* Extracts an HTTP form field from the request.
@@ -74,10 +74,10 @@ trait FormFieldDirectives extends FormFieldDirectivesInstances with ToNameRecept
7474
*
7575
* @group form
7676
*/
77-
/*@pre213
77+
@pre213
7878
@deprecated("Use new `formField` overloads with FieldSpec parameters. Kept for binary compatibility", "10.2.0")
7979
private[http] def formFields(pdm: FieldMagnet): pdm.Out =
80-
pdm.convert(toStrictEntity(StrictForm.toStrictTimeout).wrap { pdm() })*/
80+
pdm.convert(toStrictEntity(StrictForm.toStrictTimeout).wrap { pdm() })
8181

8282
/**
8383
* Extracts a number of HTTP form field from the request.

akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ trait RespondWithDirectives {
3737
*
3838
* @group response
3939
*/
40-
/*@pre213
40+
@pre213
4141
def respondWithHeaders(responseHeaders: HttpHeader*): Directive0 =
42-
respondWithHeaders(responseHeaders.toList)*/
42+
respondWithHeaders(responseHeaders.toList)
43+
44+
@since213
45+
def respondWithHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Directive0 =
46+
respondWithHeaders(firstHeader +: otherHeaders.toList)
4347

4448
/**
4549
* Unconditionally adds the given response headers to all HTTP responses of its inner Route.
@@ -49,39 +53,34 @@ trait RespondWithDirectives {
4953
def respondWithHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0 =
5054
mapResponseHeaders(responseHeaders.toList ++ _)
5155

52-
@since213
53-
def respondWithHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Directive0 =
54-
respondWithHeaders(firstHeader +: otherHeaders.toList)
55-
5656
/**
5757
* Adds the given response headers to all HTTP responses of its inner Route,
5858
* if a header already exists it is not added again.
5959
*
6060
* @group response
6161
*/
62-
/*@pre213
62+
@pre213
6363
def respondWithDefaultHeaders(responseHeaders: HttpHeader*): Directive0 =
64-
respondWithDefaultHeaders(responseHeaders.toList)*/
64+
respondWithDefaultHeaders(responseHeaders.toList)
6565

6666
/**
6767
* Adds the given response headers to all HTTP responses of its inner Route,
6868
* if a header already exists it is not added again.
6969
*
7070
* @group response
7171
*/
72-
def respondWithDefaultHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0 =
73-
mapResponse(_.withDefaultHeaders(responseHeaders))
72+
@since213
73+
def respondWithDefaultHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Directive0 =
74+
respondWithDefaultHeaders(firstHeader +: otherHeaders.toList)
7475

7576
/**
7677
* Adds the given response headers to all HTTP responses of its inner Route,
7778
* if a header already exists it is not added again.
7879
*
7980
* @group response
8081
*/
81-
@since213
82-
def respondWithDefaultHeaders(firstHeader: HttpHeader, otherHeaders: HttpHeader*): Directive0 =
83-
respondWithDefaultHeaders(firstHeader +: otherHeaders.toList)
84-
82+
def respondWithDefaultHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0 =
83+
mapResponse(_.withDefaultHeaders(responseHeaders))
8584
}
8685

8786
object RespondWithDirectives extends RespondWithDirectives

build.sbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ lazy val httpCore = project("akka-http-core")
161161
.settings(scalaMacroSupport)
162162
.enablePlugins(BootstrapGenjavadoc)
163163
.enablePlugins(ReproducibleBuildsPlugin)
164+
.enablePlugins(Pre213Preprocessor).settings(
165+
akka.http.sbt.Pre213Preprocessor.pre213Files := Seq(
166+
"headers.scala", "HttpMessage.scala", "LanguageRange.scala", "CacheDirective.scala", "LinkValue.scala"
167+
)
168+
)
164169
.disablePlugins(ScalafixPlugin)
165170

166171
lazy val http = project("akka-http")
@@ -173,6 +178,11 @@ lazy val http = project("akka-http")
173178
Compile / scalacOptions += "-language:_"
174179
)
175180
.settings(scalaMacroSupport)
181+
.enablePlugins(Pre213Preprocessor).settings(
182+
akka.http.sbt.Pre213Preprocessor.pre213Files := Seq(
183+
"scaladsl/server/directives/FormFieldDirectives.scala", "scaladsl/server/directives/RespondWithDirectives.scala"
184+
)
185+
)
176186
.enablePlugins(BootstrapGenjavadoc, BoilerplatePlugin)
177187
.enablePlugins(ReproducibleBuildsPlugin)
178188

project/Pre213Preprocessor.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package akka.http.sbt
2+
3+
import sbt._
4+
import Keys._
5+
6+
object Pre213Preprocessor extends AutoPlugin {
7+
val pre213Files = settingKey[Seq[String]]("files that should be edited for pre213 annotation")
8+
9+
val pattern = """(?s)@pre213.*?@since213""".r
10+
11+
override def projectSettings: Seq[Def.Setting[_]] = {
12+
Compile / sources := {
13+
if (scalaVersion.value startsWith "3") {
14+
val filter: File => Boolean = f => pre213Files.value.exists(suffix => f.getAbsolutePath.replace("\\", "//").endsWith(suffix))
15+
(Compile / sources).value.map { s =>
16+
if (filter(s)) {
17+
val data = IO.read(s)
18+
val targetFile = sourceManaged.value / s.getName
19+
val newData = pattern.replaceAllIn(data, "@since213")
20+
IO.write(targetFile, newData)
21+
targetFile
22+
} else s
23+
}
24+
} else (Compile / sources).value
25+
}
26+
}
27+
}
28+
29+
object ReplaceApp extends App {
30+
val pattern = """(?s)@pre213.*?@since213""".r
31+
val f = file("/home/johannes/git/opensource/akka-http/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala")
32+
val data = IO.read(f)
33+
val targetFile = file("/tmp/test")
34+
val newData = pattern.replaceAllIn(data, "@since213")
35+
IO.write(targetFile, newData)
36+
}

0 commit comments

Comments
 (0)