Skip to content

Commit

Permalink
building structure from execution result includes aliases now too (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
adridadou authored Aug 27, 2020
1 parent b575f8f commit 06e2f67
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1343,15 +1343,20 @@ final case class OpenlawExecutionState(
}
}

def buildStructureValueFromVariables
def buildStructureValueFromVariablesAndAliases
: Result[OpenlawMap[VariableName, OpenlawValue]] =
for {
values <- variablesThatAreNotTypeDefinition
variableValues <- variablesThatAreNotTypeDefinition
.map(variable => variable.evaluate(this).map(variable.name -> _))
.toList
.sequence
aliasValues <- aliases
.map(alias => alias.evaluate(this).map(alias.name -> _))
.sequence
} yield OpenlawMap(
values.flatMap({ case (name, optValue) => optValue.map(name -> _) }).toMap
(variableValues ++ aliasValues)
.flatMap({ case (name, optValue) => optValue.map(name -> _) })
.toMap
)

private def variablesThatAreNotTypeDefinition
Expand All @@ -1363,12 +1368,18 @@ final case class OpenlawExecutionState(
case _ => true
})

def buildStructureFromVariables: Structure =
buildStructure(
variablesThatAreNotTypeDefinition
.map(variable => variable.name -> variable)
.toMap
)
def buildStructureFromVariablesAndAliases: Result[Structure] =
aliases
.map(alias => alias.toVariableDefinition(this).map(alias.name -> _))
.sequence
.map(aliasMap =>
buildStructure(
variablesThatAreNotTypeDefinition
.map(variable => variable.name -> variable)
.toMap
++ aliasMap.toMap
)
)

def buildStructure(
typeDefinition: Map[VariableName, VariableDefinition]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ object VariableAliasing {
final case class VariableAliasing(name: VariableName, expr: Expression)
extends Expression
with TemplatePart {

def toVariableDefinition(
executionResult: TemplateExecutionResult
): Result[VariableDefinition] =
this.expr
.expressionType(executionResult)
.map(aliasType =>
VariableDefinition(
name,
Some(VariableTypeDefinition(aliasType.name))
)
)

def validate(executionResult: TemplateExecutionResult): Result[Unit] =
if (name.isAnonymous) {
Success.unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2269,24 +2269,29 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {

it should "extract all the variables and define a Structure out of it" in {
val template =
compile("""[[text var:Text]] [[num var:Number]]""".stripMargin)
compile(
"""[[text var:Text]] [[num var:Number]] [[@num alias = num var + 5 ]]""".stripMargin
)

engine.execute(
template,
TemplateParameters("text var" -> "hello world", "num var" -> "21213")
) match {
case Success(result) =>
result.state shouldBe ExecutionFinished
val structure = result.buildStructureFromVariables
val structureType = AbstractStructureType.generateType(structure)
val Success(structure) = result.buildStructureFromVariablesAndAliases
val structureType =
AbstractStructureType.generateType(structure)
structure.names shouldBe List(
VariableName("text var"),
VariableName("num var")
VariableName("num var"),
VariableName("num alias")
)
val Success(values) = result.buildStructureValueFromVariables
val Success(values) = result.buildStructureValueFromVariablesAndAliases
values.underlying shouldBe Map[VariableName, OpenlawValue](
VariableName("text var") -> OpenlawString("hello world"),
VariableName("num var") -> OpenlawBigDecimal(21213)
VariableName("num var") -> OpenlawBigDecimal(21213),
VariableName("num alias") -> OpenlawBigDecimal(21218)
)
val Success(newDefinedResult) = OpenlawExecutionState.empty
.withVariable(VariableName("parameters"), values, structureType)
Expand Down Expand Up @@ -2699,7 +2704,7 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {

val result = engine.execute(template).getOrThrow()

result.buildStructureValueFromVariables.getOrThrow()
result.buildStructureValueFromVariablesAndAliases.getOrThrow()
}

it should "check slow execution" in {
Expand Down

0 comments on commit 06e2f67

Please sign in to comment.