From 3b5055444075bf649d4a10a6cbe2028b443b3e57 Mon Sep 17 00:00:00 2001 From: Remy Haemmerle Date: Tue, 27 Jul 2021 15:40:04 +0200 Subject: [PATCH 1/2] LF: Simpler parser for LF version CHANGELOG_BEGIN CHANGELOG_END --- .../daml/lf/archive/testing/DamlLfEncoder.scala | 13 ++----------- .../daml/lf/language/LanguageVersion.scala | 12 +++++++++--- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala index bcd12af5d326..012aec130b12 100644 --- a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala +++ b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala @@ -9,7 +9,7 @@ import java.nio.file.Paths import com.daml.lf.archive.{Dar, DarWriter} import com.daml.lf.data.Ref import com.daml.lf.data.Ref.PackageId -import com.daml.lf.language.{Ast, Interface, LanguageMajorVersion, LanguageVersion} +import com.daml.lf.language.{Ast, Interface, LanguageVersion} import com.daml.lf.testing.parser.{ParserParameters, parseModules} import com.daml.lf.validation.Validation import com.daml.SdkVersion @@ -113,7 +113,7 @@ private[daml] object DamlLfEncoder extends App { } else args(i) match { case "--target" if i + 1 < nAgrs => - go(appArgs.copy(languageVersion = parseVersion(args(i + 1))), i + 2) + go(appArgs.copy(languageVersion = LanguageVersion.assertFromString(args(i + 1))), i + 2) case "--output" if i + 1 < nAgrs => go(appArgs.copy(outputFile = args(i + 1)), i + 2) case _ if i + 1 >= nAgrs => @@ -127,15 +127,6 @@ private[daml] object DamlLfEncoder extends App { go() } - private def parseVersion(version: String) = - version.split("""\.""").toSeq match { - case Seq("1", minor) - if LanguageMajorVersion.V1.supportsMinorVersion(minor) || minor == "dev" => - LanguageVersion(LanguageMajorVersion.V1, LanguageVersion.Minor(minor)) - case _ => - error(s"version '$version' not supported") - } - main() } diff --git a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala index 41fcaeb802e4..b659863c87b0 100644 --- a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala +++ b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala @@ -1,9 +1,8 @@ // Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package com.daml.lf.language - -import com.daml.lf.VersionRange +package com.daml.lf +package language final case class LanguageVersion(major: LanguageMajorVersion, minor: LanguageMinorVersion) { def pretty: String = s"${major.pretty}.${minor.toProtoIdentifier}" @@ -17,6 +16,13 @@ object LanguageVersion { type Minor = LanguageMinorVersion val Minor = LanguageMinorVersion + private[this] lazy val stringToVersions = All.iterator.map(v => v.pretty -> v).toMap + + def fromString(s: String): Either[String, LanguageVersion] = + stringToVersions.get(s).toRight(s + " is not supported") + + def assertFromString(s: String): LanguageVersion = data.assertRight(fromString(s)) + implicit val Ordering: scala.Ordering[LanguageVersion] = { case (LanguageVersion(Major.V1, leftMinor), LanguageVersion(Major.V1, rightMinor)) => Major.V1.minorVersionOrdering.compare(leftMinor, rightMinor) From 242557f7d95c921624f2698e5987a0f0c8a67dbc Mon Sep 17 00:00:00 2001 From: Remy Haemmerle Date: Tue, 27 Jul 2021 16:17:12 +0200 Subject: [PATCH 2/2] add a test --- .../daml/lf/language/LanguageVersionSpec.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/daml-lf/language/src/test/scala/com/digitalasset/daml/lf/language/LanguageVersionSpec.scala b/daml-lf/language/src/test/scala/com/digitalasset/daml/lf/language/LanguageVersionSpec.scala index b5ca7e38459d..6c1fb06a7515 100644 --- a/daml-lf/language/src/test/scala/com/digitalasset/daml/lf/language/LanguageVersionSpec.scala +++ b/daml-lf/language/src/test/scala/com/digitalasset/daml/lf/language/LanguageVersionSpec.scala @@ -30,7 +30,21 @@ class LanguageVersionSpec extends AnyWordSpec with Matchers with TableDrivenProp .signum shouldBe (versionRank(v1) compareTo versionRank(v2)).signum ) ) + } + + "fromString" should { + + "recognize known versions" in { + val testCases = Table("version", LV.All: _*) + + forEvery(testCases)(v => LV.fromString(v.pretty) shouldBe Right(v)) + } + + "reject invalid versions" in { + val testCases = Table("invalid version", "1", "14", "2.1", "version", "1.11.11") + forEvery(testCases)(s => LV.fromString(s) shouldBe a[Left[_, _]]) + } } }