forked from digital-asset/daml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LF: rationalize archive Parser/Reader/Decoder (digital-asset#10239)
CHANGELOG_BEGIN CHANGELOG_END
- Loading branch information
1 parent
0043b81
commit caf85a2
Showing
63 changed files
with
245 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 29 additions & 82 deletions
111
daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/Decode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,36 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.lf | ||
package archive | ||
package com.daml.lf.archive | ||
|
||
import com.daml.lf.data.Ref._ | ||
import com.daml.lf.language.Ast._ | ||
import com.daml.lf.language.LanguageMajorVersion._ | ||
import com.daml.lf.language.LanguageVersion | ||
import com.daml.daml_lf_dev.DamlLf | ||
import com.google.protobuf.CodedInputStream | ||
|
||
sealed class Decode(onlySerializableDataDefs: Boolean) { | ||
import Decode._ | ||
|
||
private[lf] val decoders: PartialFunction[LanguageVersion, PayloadDecoder] = { | ||
case LanguageVersion(V1, minor) if V1.supportedMinorVersions.contains(minor) => | ||
PayloadDecoder(new DecodeV1(minor))(_.getDamlLf1) | ||
} | ||
|
||
def decode(archive: DamlLf.Archive): (PackageId, Package) = | ||
decode(Reader.readArchive(archive)) | ||
|
||
def decode(payload: ArchivePayload): (PackageId, Package) = { | ||
val decoder = | ||
decoders | ||
.lift(payload.version) | ||
.getOrElse(throw Error.Parsing(s"${payload.version} unsupported")) | ||
( | ||
payload.pkgId, | ||
decoder.decoder.decodePackage( | ||
payload.pkgId, | ||
decoder.extract(payload.proto), | ||
onlySerializableDataDefs, | ||
), | ||
) | ||
} | ||
} | ||
|
||
object Decode extends Decode(onlySerializableDataDefs = false) { | ||
|
||
/** inlined [[scalaz.ContravariantCoyoneda]]`[OfPackage, DamlLf.ArchivePayload]` */ | ||
private[lf] sealed abstract class PayloadDecoder { | ||
type I | ||
val extract: DamlLf.ArchivePayload => I | ||
val decoder: OfPackage[I] | ||
} | ||
|
||
private[archive] object PayloadDecoder { | ||
def apply[I0](fi: OfPackage[I0])(k: DamlLf.ArchivePayload => I0): PayloadDecoder = | ||
new PayloadDecoder { | ||
type I = I0 | ||
override val extract = k | ||
override val decoder = fi | ||
} | ||
} | ||
|
||
private[lf] trait OfPackage[-Pkg] { | ||
type ProtoScenarioModule | ||
def protoScenarioModule(cis: CodedInputStream): ProtoScenarioModule | ||
@throws[Error.Parsing] | ||
def decodePackage( | ||
packageId: PackageId, | ||
lfPackage: Pkg, | ||
onlySerializableDataDefs: Boolean = false, | ||
): Package | ||
@throws[Error.Parsing] | ||
def decodeScenarioModule(packageId: PackageId, lfModuleForScenario: ProtoScenarioModule): Module | ||
} | ||
|
||
private def identifierStart(c: Char) = | ||
'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '$' || c == '_' | ||
|
||
private def identifierPart(c: Char): Boolean = | ||
identifierStart(c) || '0' <= c && c <= '9' | ||
|
||
def checkIdentifier(s: String): Unit = { | ||
if (s.isEmpty) | ||
throw Error.Parsing("empty identifier") | ||
else if (!(identifierStart(s.head) && s.tail.forall(identifierPart))) | ||
throw Error.Parsing(s"identifier $s contains invalid character") | ||
} | ||
|
||
private val decimalPattern = "[+-]*[0-9]{0,28}(\\.[0-9]{0,10})*".r.pattern | ||
def checkDecimal(s: String): Boolean = | ||
decimalPattern.matcher(s).matches() | ||
import com.daml.lf.data.Ref.PackageId | ||
import com.daml.lf.language.{Ast, LanguageMajorVersion, LanguageVersion} | ||
|
||
object Decode { | ||
|
||
// decode an ArchivePayload | ||
def decodeArchivePayload( | ||
payload: ArchivePayload, | ||
onlySerializableDataDefs: Boolean = false, | ||
): (PackageId, Ast.Package) = | ||
payload.version match { | ||
case LanguageVersion(LanguageMajorVersion.V1, minor) | ||
if LanguageMajorVersion.V1.supportedMinorVersions.contains(minor) => | ||
payload.pkgId -> | ||
new DecodeV1(minor).decodePackage( | ||
payload.pkgId, | ||
payload.proto.getDamlLf1, | ||
onlySerializableDataDefs, | ||
) | ||
case v => throw Error.Parsing(s"$v unsupported") | ||
} | ||
|
||
// decode an Archive | ||
def decodeArchive( | ||
archive: DamlLf.Archive, | ||
onlySerializableDataDefs: Boolean = false, | ||
): (PackageId, Ast.Package) = | ||
decodeArchivePayload(Reader.readArchive(archive), onlySerializableDataDefs) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/GenReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.lf | ||
package archive | ||
|
||
import com.google.protobuf.{ByteString, CodedInputStream} | ||
|
||
import scala.util.Using | ||
|
||
final class GenReader[X] private[archive] (val fromCodedInputStream: CodedInputStream => X) { | ||
|
||
def fromInputStream(is: java.io.InputStream): X = | ||
fromCodedInputStream(CodedInputStream.newInstance(is)) | ||
|
||
def fromByteArray(bytes: Array[Byte]): X = | ||
fromCodedInputStream(CodedInputStream.newInstance(bytes)) | ||
|
||
def fromByteString(bytes: ByteString): X = | ||
fromCodedInputStream(CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer())) | ||
|
||
def fromBytes(bytes: data.Bytes): X = | ||
fromByteString(bytes.toByteString) | ||
|
||
def fromFile(file: java.nio.file.Path): X = | ||
Using.resource(java.nio.file.Files.newInputStream(file))(fromInputStream) | ||
|
||
def fromFile(file: java.io.File): X = | ||
fromFile(file.toPath) | ||
|
||
private[archive] def andThen[Y](f: X => Y): GenReader[Y] = | ||
new GenReader(x => f(fromCodedInputStream(x))) | ||
|
||
} |
Oops, something went wrong.