Skip to content

Commit

Permalink
LF encoder: make package validation optional (#11849)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da authored Nov 24, 2021
1 parent 25b476f commit 393893a
Showing 1 changed file with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ private[daml] object DamlLfEncoder extends App {
languageVersion = appArgs.languageVersion,
)

makeDar(readSources(appArgs.inputFiles), Paths.get(appArgs.outputFile).toFile)
makeDar(
readSources(appArgs.inputFiles),
Paths.get(appArgs.outputFile).toFile,
validation = appArgs.validation,
)

} catch {
case e: EncodeError =>
Expand All @@ -54,7 +58,8 @@ private[daml] object DamlLfEncoder extends App {
files.view.flatMap(file => Source.fromFile(Paths.get(file).toFile, "UTF8")).mkString

private def makeArchive(
source: String
source: String,
validation: Boolean,
)(implicit parserParameters: ParserParameters[this.type]) = {

val modules = parseModules[this.type](source).fold(error, identity)
Expand All @@ -73,15 +78,16 @@ private[daml] object DamlLfEncoder extends App {
Ast.Package.build(modules, Set.empty[PackageId], parserParameters.languageVersion, metadata)
val pkgs = PackageInterface(Map(pkgId -> pkg))

Validation.checkPackage(pkgs, pkgId, pkg).left.foreach(e => error(e.pretty))
if (validation)
Validation.checkPackage(pkgs, pkgId, pkg).left.foreach(e => error(e.pretty))

encodeArchive(pkgId -> pkg, parserParameters.languageVersion)
}

private def makeDar(source: String, file: File)(implicit
private def makeDar(source: String, file: File, validation: Boolean)(implicit
parserParameters: ParserParameters[this.type]
) = {
val archive = makeArchive(source)
val archive = makeArchive(source, validation = validation)
DarWriter.encode(
SdkVersion.sdkVersion,
Dar(("archive.dalf", archive.toByteArray), List()),
Expand All @@ -93,38 +99,35 @@ private[daml] object DamlLfEncoder extends App {
inputFiles: List[String],
outputFile: String,
languageVersion: LanguageVersion,
validation: Boolean,
)

private def parseArgs() = {
val nAgrs = args.length

@tailrec
def go(
appArgs: Arguments = Arguments(List.empty, "", LanguageVersion.default),
i: Int = 0,
): Arguments =
if (i == nAgrs) {
if (appArgs.outputFile.isEmpty)
error("output file not set")
if (appArgs.inputFiles.isEmpty)
error("no input files set")
else
appArgs
} else
args(i) match {
case "--target" if i + 1 < nAgrs =>
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 =>
error(
s"usage: encoder_binary inputFile1 ... inputFileN --output outputFile [--target version]"
)
case x =>
go(appArgs.copy(inputFiles = x :: appArgs.inputFiles), i + 1)
}

go()
def go(appArgs: Arguments, args: List[String]): Arguments =
args match {
case "--target" :: version :: tail =>
go(appArgs.copy(languageVersion = LanguageVersion.assertFromString(version)), tail)
case "--output" :: file :: tail =>
go(appArgs.copy(outputFile = file), tail)
case "--skip-validation" :: tail =>
go(appArgs.copy(validation = false), tail)
case option :: _ if option.startsWith("-") =>
error(
s"usage: encoder_binary inputFile1 ... inputFileN --output outputFile [--target version]"
)
case Nil =>
if (appArgs.outputFile.isEmpty)
error("output file not set")
if (appArgs.inputFiles.isEmpty)
error("no input files set")
else
appArgs
case x :: tail =>
go(appArgs.copy(inputFiles = x :: appArgs.inputFiles), tail)
}

go(Arguments(List.empty, "", LanguageVersion.default, true), args.toList)
}

main()
Expand Down

0 comments on commit 393893a

Please sign in to comment.