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] make enricher paramtric to drop type info #20243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -28,17 +28,26 @@ final class ValueEnricher(
compiledPackages: CompiledPackages,
translateValue: (Ast.Type, Value) => Result[SValue],
loadPackage: (PackageId, language.Reference) => Result[Unit],
keepTypeInfo: Boolean,
keepFieldName: Boolean,
) {

def this(engine: Engine) =
def this(
engine: Engine,
keepTypeInfo: Boolean = true,
keepFieldName: Boolean = true,
) =
this(
engine.compiledPackages(),
engine.preprocessor.translateValue,
engine.loadPackage,
keepTypeInfo = keepTypeInfo,
keepFieldName = keepFieldName,
)

def enrichValue(typ: Ast.Type, value: Value): Result[Value] =
translateValue(typ, value).map(_.toUnnormalizedValue)
translateValue(typ, value)
.map(_.toValue(keepTypeInfo = keepTypeInfo, keepFieldName = keepFieldName))

def enrichVersionedValue(
typ: Ast.Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,23 @@ class ValueEnricherSpec(majorLanguageVersion: LanguageMajorVersion)
enricher.enrichVersionedTransaction(inputTransaction) shouldBe ResultDone(outputTransaction)

}

"enricher can keep field name without type annotation" in {
val enrich = new ValueEnricher(engine, keepTypeInfo = false, keepFieldName = true)
import enrich.enrichValue

val tRecord = TTyCon("Mod:Record")
val normalizedRecord = ValueRecord(None, ImmArray(None -> ValueInt64(33)))
val enrichedRecord = ValueRecord(None, ImmArray(Some[Ref.Name]("field") -> ValueInt64(33)))
val tVariant = TTyCon("Mod:Variant")
val normalizedVariant = ValueVariant(None, "variant1", ValueText("some test"))
val tEnum = TTyCon("Mod:Enum")
val normalizedEnum = ValueEnum(None, "value1")

enrichValue(tRecord, normalizedRecord) shouldBe ResultDone(enrichedRecord)
enrichValue(tVariant, normalizedVariant) shouldBe ResultDone(normalizedVariant)
enrichValue(tEnum, normalizedEnum) shouldBe ResultDone(normalizedEnum)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,21 @@ sealed abstract class SValue {
*/
def toUnnormalizedValue: V = {
toValue(
normalize = false
keepTypeInfo = true,
keepFieldName = true,
)
}

/** Convert a speedy-value to a value normalized according to the LF version.
*/
@scala.annotation.nowarn("cat=unused")
def toNormalizedValue(version: TransactionVersion): V =
toValue(normalize = true)
toValue(keepTypeInfo = false, keepFieldName = false)

private[this] def toValue(normalize: Boolean): V = {

def maybeEraseTypeInfo[X](x: X): Option[X] =
if (normalize) {
None
} else {
Some(x)
}
private[lf] def toValue(
keepTypeInfo: Boolean,
keepFieldName: Boolean,
): V = {

def go(v: SValue, maxNesting: Int = V.MAXIMUM_NESTING): V = {
if (maxNesting < 0)
Expand All @@ -69,17 +66,17 @@ sealed abstract class SValue {
case SDate(x) => V.ValueDate(x)
case r: SRecord =>
V.ValueRecord(
maybeEraseTypeInfo(r.id),
Option.when(keepTypeInfo)(r.id),
(r.fields.toSeq.view zip r.values.iterator().asScala)
.map { case (field, sv) =>
(maybeEraseTypeInfo(field), go(sv, nextMaxNesting))
(Option.when(keepFieldName)(field), go(sv, nextMaxNesting))
}
.to(ImmArray),
)
case SVariant(id, variant, _, sv) =>
V.ValueVariant(maybeEraseTypeInfo(id), variant, go(sv, nextMaxNesting))
V.ValueVariant(Option.when(keepTypeInfo)(id), variant, go(sv, nextMaxNesting))
case SEnum(id, constructor, _) =>
V.ValueEnum(maybeEraseTypeInfo(id), constructor)
V.ValueEnum(Option.when(keepTypeInfo)(id), constructor)
case SList(lst) =>
V.ValueList(lst.map(go(_, nextMaxNesting)))
case SOptional(mbV) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,13 @@ private[lf] object ScenarioRunner {
def loadPackage(pkgId: PackageId, context: language.Reference): Result[Unit] = {
crash(LookupError.MissingPackage.pretty(pkgId, context))
}
val enricher = new ValueEnricher(compiledPackages, translateValue, loadPackage)
val enricher = new ValueEnricher(
compiledPackages = compiledPackages,
translateValue = translateValue,
loadPackage = loadPackage,
keepTypeInfo = true,
keepFieldName = true,
)
def consume[V](res: Result[V]): V =
res match {
case ResultDone(x) => x
Expand Down