Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LF: parser for LF versions #10424

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =>
Expand All @@ -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()

}
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scala question: What does it mean to have multiple package statements? What's the purpose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it means you can reference com.daml.lf.X without qualifying as well as com.daml.lf.language.X.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! :-)


final case class LanguageVersion(major: LanguageMajorVersion, minor: LanguageMinorVersion) {
def pretty: String = s"${major.pretty}.${minor.toProtoIdentifier}"
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[_, _]])
}
}

}