Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Add Scala 2.13 support
Browse files Browse the repository at this point in the history
This project now compiles with Scala 2.13 by default, while still
cross-compiling to Scala 2.12 & 2.11.

Issues addressed with this upgrade:

### Underscore (`_`) is no longer a valid identifer for `val`s

Using underscore like this is no longer allowed...

```
implicit val _ = ...
```

...we have to give the `val` a valid name (even if it's just `v`):


```
implicit val v = ...
```

See also:

* scala/bug#10384
* scala/bug#7691
* scala/scala#6218

### `Map.mapValues()` now returns a `MapView` rather than a `Map`

We usually want a `Map`, so we can just add in a `.toMap` to the end of
the expression - it's harmless in Scala 2.12, and allows the code to
compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala
2.13, but using the new recommended alternative doesn't work in Scala 2.12.

https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes

See also:

* scala/bug#10919
* scala/scala#7014

### `.to` can no longer infer what resulting collection type you want

This is all part of `CanBuildFrom` going away in Scala 2.13:

https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
  • Loading branch information
rtyley committed Oct 20, 2019
1 parent 30923b5 commit c22bd81
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enablePlugins(GhpagesPlugin, SiteScaladocPlugin)
name := "twitter4s"
version := "6.2-SNAPSHOT"

scalaVersion := "2.12.4"
scalaVersion := "2.13.1"

resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Expand Down Expand Up @@ -53,7 +53,7 @@ lazy val standardSettings = Seq(
ScmInfo(url("https://github.com/DanielaSfregola/twitter4s"),
"scm:git:[email protected]:DanielaSfregola/twitter4s.git")),
apiURL := Some(url("http://DanielaSfregola.github.io/twitter4s/latest/api/")),
crossScalaVersions := Seq("2.12.4", "2.11.12"),
crossScalaVersions := Seq(scalaVersion.value, "2.12.10", "2.11.12"),
pomExtra := (
<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait Client extends OAuthClient {
protected def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])(
implicit system: ActorSystem,
materializer: Materializer): Future[T] = {
implicit val _ = request
implicit val r: HttpRequest = request
val requestStartTime = System.currentTimeMillis

if (withLogRequest) logRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ trait BodyEncoder {

// TODO - improve performance with Macros?
private def asMap(cc: Product): Map[String, Any] =
cc.getClass.getDeclaredFields.map(_.getName).zip(cc.productIterator.to).toMap
cc.getClass.getDeclaredFields.map(_.getName).zip(cc.productIterator.toSeq).toMap

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private[twitter4s] class OAuth1Provider(consumerToken: ConsumerToken, accessToke
val params = basicOAuth2Params(callback)
for {
signature <- oauth1Signature(params)
} yield (params + signature).mapValues(_.urlEncoded)
} yield (params + signature).mapValues(_.urlEncoded).toMap
}

def oauth1Signature(oauth2Params: Map[String, String])(implicit request: HttpRequest, materializer: Materializer) = {
Expand Down Expand Up @@ -67,7 +67,7 @@ private[twitter4s] class OAuth1Provider(consumerToken: ConsumerToken, accessToke
val method = request.method.name.urlEncoded
val baseUrl = request.uri.endpoint.urlEncoded
val oauthParams = oauth2Params.map {
case (k, v) =>
case (k: String, v: String) =>
if (k == "oauth_callback") k -> v.urlEncoded
else k -> v
}
Expand All @@ -90,7 +90,8 @@ private[twitter4s] class OAuth1Provider(consumerToken: ConsumerToken, accessToke
}
}

def queryParams(implicit request: HttpRequest) = request.uri.query().toMap.mapValues(_.urlEncoded)
def queryParams(implicit request: HttpRequest): Map[String, String] =
request.uri.query().toMap.mapValues(_.urlEncoded).toMap

private def encodeParams(params: Map[String, String]) =
params.keySet.toList.sorted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private[twitter4s] object StreamingMessageFormats extends FormatsComposer {
private val tweetUnmarshaller = FieldSerializer[Tweet](deserializer = FieldSerializer.renameFrom("full_text", "text"))

private def withCustomUnmarshaller[T <: StreamingMessage: Manifest](json: JValue, formatter: Formats): Option[T] = {
implicit val _: Formats = formatter
implicit val formats: Formats = formatter
Extraction.extractOpt[T](json)
}

Expand Down

0 comments on commit c22bd81

Please sign in to comment.