diff --git a/ledger-service/http-json/BUILD.bazel b/ledger-service/http-json/BUILD.bazel index 291caebbcb..2e8ffc22bb 100644 --- a/ledger-service/http-json/BUILD.bazel +++ b/ledger-service/http-json/BUILD.bazel @@ -41,6 +41,7 @@ da_scala_library( "//ledger/ledger-api-common", "//libs-scala/auth-utils", "//libs-scala/ports", + "//libs-scala/scala-utils", "@maven//:com_chuusai_shapeless_2_12", "@maven//:com_github_scopt_scopt_2_12", "@maven//:com_lihaoyi_sourcecode_2_12", @@ -84,6 +85,7 @@ da_scala_binary( "//ledger/ledger-api-common", "//libs-scala/auth-utils", "//libs-scala/ports", + "//libs-scala/scala-utils", "@maven//:ch_qos_logback_logback_classic", "@maven//:com_chuusai_shapeless_2_12", "@maven//:com_github_scopt_scopt_2_12", @@ -129,6 +131,7 @@ da_scala_test( "//ledger-service/db-backend", "//ledger-service/lf-value-json", "//ledger/ledger-api-common", + "//libs-scala/scala-utils", "//libs-scala/scalatest-utils", "@maven//:com_chuusai_shapeless_2_12", "@maven//:com_lihaoyi_sourcecode_2_12", @@ -184,6 +187,7 @@ da_scala_test( "//libs-scala/auth-utils", "//libs-scala/ports", "//libs-scala/postgresql-testing", + "//libs-scala/scala-utils", "@maven//:ch_qos_logback_logback_classic", "@maven//:com_chuusai_shapeless_2_12", "@maven//:com_github_ghik_silencer_lib_2_12_11", diff --git a/ledger-service/http-json/src/it/scala/http/HttpServiceIntegrationTest.scala b/ledger-service/http-json/src/it/scala/http/HttpServiceIntegrationTest.scala index 2dab7a5299..2a42dd6e7f 100644 --- a/ledger-service/http-json/src/it/scala/http/HttpServiceIntegrationTest.scala +++ b/ledger-service/http-json/src/it/scala/http/HttpServiceIntegrationTest.scala @@ -8,7 +8,7 @@ import java.nio.file.Files import akka.http.scaladsl.Http 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 org.scalacheck.Gen import org.scalatest.{Assertion, BeforeAndAfterAll} diff --git a/ledger-service/http-json/src/it/scala/http/HttpServiceWithPostgresIntTest.scala b/ledger-service/http-json/src/it/scala/http/HttpServiceWithPostgresIntTest.scala index 00db72ea2d..16aba05e3a 100644 --- a/ledger-service/http-json/src/it/scala/http/HttpServiceWithPostgresIntTest.scala +++ b/ledger-service/http-json/src/it/scala/http/HttpServiceWithPostgresIntTest.scala @@ -3,7 +3,7 @@ package com.daml.http -import com.daml.http.Statement.discard +import com.daml.scalautil.Statement.discard import com.daml.testing.postgresql.PostgresAroundAll import spray.json.{JsString, JsValue} diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/ContractsFetch.scala b/ledger-service/http-json/src/main/scala/com/digitalasset/http/ContractsFetch.scala index fa964b4e82..7aeb0a899d 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/ContractsFetch.scala +++ b/ledger-service/http-json/src/main/scala/com/digitalasset/http/ContractsFetch.scala @@ -17,7 +17,7 @@ import akka.stream.scaladsl.{ Source, } 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, Queries} import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId} diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Endpoints.scala b/ledger-service/http-json/src/main/scala/com/digitalasset/http/Endpoints.scala index d4178dbb5a..fb658e7df0 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Endpoints.scala +++ b/ledger-service/http-json/src/main/scala/com/digitalasset/http/Endpoints.scala @@ -19,7 +19,7 @@ import akka.util.ByteString import com.daml.lf import com.daml.http.ContractsService.SearchResult 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.json._ import com.daml.http.util.Collections.toNonEmptySet diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/HttpService.scala b/ledger-service/http-json/src/main/scala/com/digitalasset/http/HttpService.scala index b0124c3289..46af05a636 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/HttpService.scala +++ b/ledger-service/http-json/src/main/scala/com/digitalasset/http/HttpService.scala @@ -12,7 +12,7 @@ import akka.http.scaladsl.settings.ServerSettings import akka.stream.Materializer import com.daml.auth.TokenHolder 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.json.{ ApiValueToJsValueConverter, diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Main.scala b/ledger-service/http-json/src/main/scala/com/digitalasset/http/Main.scala index 0c6200e907..f983816f6e 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Main.scala +++ b/ledger-service/http-json/src/main/scala/com/digitalasset/http/Main.scala @@ -10,7 +10,7 @@ import akka.actor.ActorSystem import akka.http.scaladsl.Http.ServerBinding import akka.stream.Materializer 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.ledger.api.tls.TlsConfigurationCli import com.daml.ledger.api.refinements.ApiTypes.ApplicationId diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/WebSocketService.scala b/ledger-service/http-json/src/main/scala/com/digitalasset/http/WebSocketService.scala index ffc03c3515..4a7327527b 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/WebSocketService.scala +++ b/ledger-service/http-json/src/main/scala/com/digitalasset/http/WebSocketService.scala @@ -292,7 +292,7 @@ class WebSocketService( extends LazyLogging { import WebSocketService._ - import Statement.discard + import com.daml.scalautil.Statement.discard import util.ErrorOps._ import com.daml.http.json.JsonProtocol._ diff --git a/ledger-service/http-json/src/test/scala/com/digitalasset/http/json/JsonProtocolTest.scala b/ledger-service/http-json/src/test/scala/com/digitalasset/http/json/JsonProtocolTest.scala index 8b4c7c3341..c8ad6694fa 100644 --- a/ledger-service/http-json/src/test/scala/com/digitalasset/http/json/JsonProtocolTest.scala +++ b/ledger-service/http-json/src/test/scala/com/digitalasset/http/json/JsonProtocolTest.scala @@ -16,7 +16,7 @@ import com.daml.http.Generators.{ genUnknownTemplateIds, genWarningsWrapper } -import com.daml.http.Statement.discard +import com.daml.scalautil.Statement.discard import com.daml.http.domain import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.{identifier, listOf} diff --git a/libs-scala/scala-utils/BUILD.bazel b/libs-scala/scala-utils/BUILD.bazel new file mode 100644 index 0000000000..5f306d34e6 --- /dev/null +++ b/libs-scala/scala-utils/BUILD.bazel @@ -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", + ], +) diff --git a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Statement.scala b/libs-scala/scala-utils/src/main/scala/com/daml/scalautil/Statement.scala similarity index 51% rename from ledger-service/http-json/src/main/scala/com/digitalasset/http/Statement.scala rename to libs-scala/scala-utils/src/main/scala/com/daml/scalautil/Statement.scala index e1a7ae6a1d..d7f539ba0b 100644 --- a/ledger-service/http-json/src/main/scala/com/digitalasset/http/Statement.scala +++ b/libs-scala/scala-utils/src/main/scala/com/daml/scalautil/Statement.scala @@ -1,9 +1,16 @@ // Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package com.daml.http +package com.daml.scalautil 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 = { val _: A = evaluateForSideEffectOnly () //Return unit to prevent warning due to discarding value diff --git a/libs-scala/scala-utils/src/test/scala/com/daml/scalautil/StatementSpec.scala b/libs-scala/scala-utils/src/test/scala/com/daml/scalautil/StatementSpec.scala new file mode 100644 index 0000000000..b08b9fc2bb --- /dev/null +++ b/libs-scala/scala-utils/src/test/scala/com/daml/scalautil/StatementSpec.scala @@ -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) + } +} diff --git a/release/artifacts.yaml b/release/artifacts.yaml index 73dfcd45de..fd1604ddc4 100644 --- a/release/artifacts.yaml +++ b/release/artifacts.yaml @@ -149,3 +149,5 @@ type: jar-scala - target: //libs-scala/build-info:build-info type: jar-scala +- target: //libs-scala/scala-utils:scala-utils + type: jar-scala