Introduce //libs-scala/scala-utils (#6935)

* Moving `Statements.discard` from //ledger-server/http-json into //libs-scala/scala-utils

changelog_begin
changelog_end

* Add new module to the published artifacts

* `com.daml.scalautil` instead of `com.daml.scala.util`

@S11001001: That's because if this is in classpath and you import com.daml._,
you have a different scala in scope than the one you expect.
This commit is contained in:
Leonid Shlyapnikov 2020-07-30 13:51:07 -04:00 committed by GitHub
parent 83031f3edf
commit 05d49b37c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 98 additions and 9 deletions

View File

@ -41,6 +41,7 @@ da_scala_library(
"//ledger/ledger-api-common", "//ledger/ledger-api-common",
"//libs-scala/auth-utils", "//libs-scala/auth-utils",
"//libs-scala/ports", "//libs-scala/ports",
"//libs-scala/scala-utils",
"@maven//:com_chuusai_shapeless_2_12", "@maven//:com_chuusai_shapeless_2_12",
"@maven//:com_github_scopt_scopt_2_12", "@maven//:com_github_scopt_scopt_2_12",
"@maven//:com_lihaoyi_sourcecode_2_12", "@maven//:com_lihaoyi_sourcecode_2_12",
@ -84,6 +85,7 @@ da_scala_binary(
"//ledger/ledger-api-common", "//ledger/ledger-api-common",
"//libs-scala/auth-utils", "//libs-scala/auth-utils",
"//libs-scala/ports", "//libs-scala/ports",
"//libs-scala/scala-utils",
"@maven//:ch_qos_logback_logback_classic", "@maven//:ch_qos_logback_logback_classic",
"@maven//:com_chuusai_shapeless_2_12", "@maven//:com_chuusai_shapeless_2_12",
"@maven//:com_github_scopt_scopt_2_12", "@maven//:com_github_scopt_scopt_2_12",
@ -129,6 +131,7 @@ da_scala_test(
"//ledger-service/db-backend", "//ledger-service/db-backend",
"//ledger-service/lf-value-json", "//ledger-service/lf-value-json",
"//ledger/ledger-api-common", "//ledger/ledger-api-common",
"//libs-scala/scala-utils",
"//libs-scala/scalatest-utils", "//libs-scala/scalatest-utils",
"@maven//:com_chuusai_shapeless_2_12", "@maven//:com_chuusai_shapeless_2_12",
"@maven//:com_lihaoyi_sourcecode_2_12", "@maven//:com_lihaoyi_sourcecode_2_12",
@ -184,6 +187,7 @@ da_scala_test(
"//libs-scala/auth-utils", "//libs-scala/auth-utils",
"//libs-scala/ports", "//libs-scala/ports",
"//libs-scala/postgresql-testing", "//libs-scala/postgresql-testing",
"//libs-scala/scala-utils",
"@maven//:ch_qos_logback_logback_classic", "@maven//:ch_qos_logback_logback_classic",
"@maven//:com_chuusai_shapeless_2_12", "@maven//:com_chuusai_shapeless_2_12",
"@maven//:com_github_ghik_silencer_lib_2_12_11", "@maven//:com_github_ghik_silencer_lib_2_12_11",

View File

@ -8,7 +8,7 @@ import java.nio.file.Files
import akka.http.scaladsl.Http import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, StatusCodes, Uri} import akka.http.scaladsl.model.{HttpMethods, HttpRequest, StatusCodes, Uri}
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.util.TestUtil.writeToFile import com.daml.http.util.TestUtil.writeToFile
import org.scalacheck.Gen import org.scalacheck.Gen
import org.scalatest.{Assertion, BeforeAndAfterAll} import org.scalatest.{Assertion, BeforeAndAfterAll}

View File

@ -3,7 +3,7 @@
package com.daml.http package com.daml.http
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.testing.postgresql.PostgresAroundAll import com.daml.testing.postgresql.PostgresAroundAll
import spray.json.{JsString, JsValue} import spray.json.{JsString, JsValue}

View File

@ -17,7 +17,7 @@ import akka.stream.scaladsl.{
Source, Source,
} }
import akka.stream.{ClosedShape, FanOutShape2, FlowShape, Graph, Materializer} import akka.stream.{ClosedShape, FanOutShape2, FlowShape, Graph, Materializer}
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.dbbackend.ContractDao.StaleOffsetException import com.daml.http.dbbackend.ContractDao.StaleOffsetException
import com.daml.http.dbbackend.{ContractDao, Queries} import com.daml.http.dbbackend.{ContractDao, Queries}
import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId} import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId}

View File

@ -19,7 +19,7 @@ import akka.util.ByteString
import com.daml.lf import com.daml.lf
import com.daml.http.ContractsService.SearchResult import com.daml.http.ContractsService.SearchResult
import com.daml.http.EndpointsCompanion._ import com.daml.http.EndpointsCompanion._
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.domain.JwtPayload import com.daml.http.domain.JwtPayload
import com.daml.http.json._ import com.daml.http.json._
import com.daml.http.util.Collections.toNonEmptySet import com.daml.http.util.Collections.toNonEmptySet

View File

@ -12,7 +12,7 @@ import akka.http.scaladsl.settings.ServerSettings
import akka.stream.Materializer import akka.stream.Materializer
import com.daml.auth.TokenHolder import com.daml.auth.TokenHolder
import com.daml.grpc.adapter.ExecutionSequencerFactory import com.daml.grpc.adapter.ExecutionSequencerFactory
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.dbbackend.ContractDao import com.daml.http.dbbackend.ContractDao
import com.daml.http.json.{ import com.daml.http.json.{
ApiValueToJsValueConverter, ApiValueToJsValueConverter,

View File

@ -10,7 +10,7 @@ import akka.actor.ActorSystem
import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.Http.ServerBinding
import akka.stream.Materializer import akka.stream.Materializer
import com.daml.grpc.adapter.{AkkaExecutionSequencerPool, ExecutionSequencerFactory} import com.daml.grpc.adapter.{AkkaExecutionSequencerPool, ExecutionSequencerFactory}
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.dbbackend.ContractDao import com.daml.http.dbbackend.ContractDao
import com.daml.ledger.api.tls.TlsConfigurationCli import com.daml.ledger.api.tls.TlsConfigurationCli
import com.daml.ledger.api.refinements.ApiTypes.ApplicationId import com.daml.ledger.api.refinements.ApiTypes.ApplicationId

View File

@ -292,7 +292,7 @@ class WebSocketService(
extends LazyLogging { extends LazyLogging {
import WebSocketService._ import WebSocketService._
import Statement.discard import com.daml.scalautil.Statement.discard
import util.ErrorOps._ import util.ErrorOps._
import com.daml.http.json.JsonProtocol._ import com.daml.http.json.JsonProtocol._

View File

@ -16,7 +16,7 @@ import com.daml.http.Generators.{
genUnknownTemplateIds, genUnknownTemplateIds,
genWarningsWrapper genWarningsWrapper
} }
import com.daml.http.Statement.discard import com.daml.scalautil.Statement.discard
import com.daml.http.domain import com.daml.http.domain
import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen.{identifier, listOf} import org.scalacheck.Gen.{identifier, listOf}

View File

@ -0,0 +1,36 @@
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
load(
"//bazel_tools:scala.bzl",
"da_scala_library",
"da_scala_test",
"lf_scalacopts",
)
scalacopts = lf_scalacopts + [
"-P:wartremover:traverser:org.wartremover.warts.NonUnitStatements",
]
da_scala_library(
name = "scala-utils",
srcs = glob(["src/main/scala/**/*.scala"]),
scalacopts = scalacopts,
tags = ["maven_coordinates=com.daml:scala-utils:__VERSION__"],
visibility = [
"//visibility:public",
],
deps = [
],
)
da_scala_test(
name = "test",
srcs = glob(["src/test/scala/**/*.scala"]),
scalacopts = scalacopts,
deps = [
":scala-utils",
"//libs-scala/scalatest-utils",
"@maven//:org_scalaz_scalaz_core_2_12",
],
)

View File

@ -1,9 +1,16 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package com.daml.http package com.daml.scalautil
object Statement { object Statement {
/**
* Suppresses `traverser:org.wartremover.warts.NonUnitStatements` warning on the expression level.
*
* @param evaluateForSideEffectOnly an expression with a side-effect that needs to be evaluated.
* @tparam A return type of the expression that gets discarded.
*/
@specialized def discard[A](evaluateForSideEffectOnly: A): Unit = { @specialized def discard[A](evaluateForSideEffectOnly: A): Unit = {
val _: A = evaluateForSideEffectOnly val _: A = evaluateForSideEffectOnly
() //Return unit to prevent warning due to discarding value () //Return unit to prevent warning due to discarding value

View File

@ -0,0 +1,40 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.scalautil
import org.scalatest.{FlatSpec, Matchers}
class StatementSpec extends FlatSpec with Matchers {
import com.daml.scalatest.Equalz._
import scalaz.std.anyVal._
behavior of Statement.getClass.getSimpleName
it should "evaluate passed expression for side effects" in {
var counter: Int = 0
def increment(): Int = {
counter += 1
counter
}
Statement.discard(increment()): Unit
Statement.discard {
counter += 1
}: Unit
counter shouldx equalz(2)
}
it should "not evaluate passed lambda expression" in {
var counter: Int = 0
Statement.discard { () =>
counter += 1
}
counter shouldx equalz(0)
}
}

View File

@ -149,3 +149,5 @@
type: jar-scala type: jar-scala
- target: //libs-scala/build-info:build-info - target: //libs-scala/build-info:build-info
type: jar-scala type: jar-scala
- target: //libs-scala/scala-utils:scala-utils
type: jar-scala