Skip to content

Commit

Permalink
LF: Simpler parser for LF version
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed Jul 27, 2021
1 parent 7c88b56 commit 0c8701a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
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

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] def 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

0 comments on commit 0c8701a

Please sign in to comment.