Skip to content

Commit

Permalink
Editor caching startup process changes (#46)
Browse files Browse the repository at this point in the history
* update Openlaw object for new caching

* fix serializing execution result

* make it better for our case

* upgrade core version
  • Loading branch information
adridadou authored and outkaj committed Apr 8, 2019
1 parent 15331cd commit 901afa1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lazy val root = (project in file(".")).settings(
name := "openlaw-core-client",
scalaVersion := scalaV,
libraryDependencies ++= Seq(
"org.openlaw" %%% "openlaw-core" % "0.1.25"
"org.openlaw" %%% "openlaw-core" % "0.1.26"
),
relativeSourceMaps := true,
artifactPath in (Compile, fullOptJS) := crossTarget.value / "client.js",
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "0.2.6")

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")

addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.3.7")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.1")

addSbtPlugin("com.thoughtworks.sbt-scala-js-map" % "sbt-scala-js-map" % "3.0.0")
69 changes: 34 additions & 35 deletions src/main/scala/org/adridadou/openlaw/client/Openlaw.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import org.adridadou.openlaw.values.{TemplateParameters, TemplateTitle}
import org.adridadou.openlaw.vm.OpenlawExecutionEngine
import slogging.LazyLogging

import io.circe.parser._
import io.circe.syntax._

import scala.scalajs.js.Dictionary
import scala.scalajs.js.JSConverters._

Expand All @@ -26,7 +29,7 @@ object Openlaw extends LazyLogging {

val clock: Clock = Clock.systemDefaultZone()
val engine = new OpenlawExecutionEngine
val markdown = new OpenlawTemplateLanguageParserService(Clock.systemUTC())
val markdown = new OpenlawTemplateLanguageParserService(clock)

@JSExport
def compileTemplate(text:String) : js.Dictionary[Any] = markdown.compileTemplate(text, clock) match {
Expand Down Expand Up @@ -62,7 +65,7 @@ object Openlaw extends LazyLogging {
}

@JSExport
def resumeExecution(executionResult:TemplateExecutionResult, jsTemplates:js.Dictionary[CompiledTemplate]) : js.Dictionary[Any] = {
def resumeExecution(executionResult:OpenlawExecutionState, jsTemplates:js.Dictionary[CompiledTemplate]) : js.Dictionary[Any] = {
val templates = jsTemplates.map({ case (name, template) => TemplateSourceIdentifier(TemplateTitle(name)) -> template}).toMap
handleExecutionResult(engine.resumeExecution(executionResult, templates))
}
Expand All @@ -87,14 +90,9 @@ object Openlaw extends LazyLogging {
def validationErrors(result:ValidationResult):js.Array[String] = result.validationExpressionErrors.toJSArray

@JSExport
def validateContract(executionResult:TemplateExecutionResult):ValidationResult =
def validateContract(executionResult:OpenlawExecutionState):ValidationResult =
executionResult.validateExecution

private def resultFromMissingInput(seq: Result[Seq[VariableName]]): (Seq[VariableName], Seq[String]) = seq match {
case Success(inputs) => (inputs, Seq())
case Failure(_, message) => (Seq(), Seq(message))
}

@JSExport
def showInForm(variable:VariableDefinition, executionResult:TemplateExecutionResult):Boolean =
variable.varType(executionResult) match {
Expand Down Expand Up @@ -126,18 +124,16 @@ object Openlaw extends LazyLogging {
variable.defaultValue.map(getDefaultChoices(_, variable.varType(executionResult), executionResult)).getOrElse(Seq()).toJSArray
}

private def getDefaultChoices(parameter:Parameter, variableType:VariableType, executionResult: TemplateExecutionResult):Seq[String] = {
parameter match {
case Parameters(parameterMap) =>
parameterMap.toMap.get("options").map({
case ListParameter(params) =>
params.flatMap(_.evaluate(executionResult)).map(variableType.internalFormat)
case OneValueParameter(expr) =>
expr.evaluate(executionResult).map(variableType.internalFormat).toSeq
case _ => Seq()
}).getOrElse(Seq())
case _ => Seq()
}
private def getDefaultChoices(parameter:Parameter, variableType:VariableType, executionResult: TemplateExecutionResult):Seq[String] = parameter match {
case Parameters(parameterMap) =>
parameterMap.toMap.get("options").map({
case ListParameter(params) =>
params.flatMap(_.evaluate(executionResult)).map(variableType.internalFormat)
case OneValueParameter(expr) =>
expr.evaluate(executionResult).map(variableType.internalFormat).toSeq
case _ => Seq()
}).getOrElse(Seq())
case _ => Seq()
}

@JSExport
Expand Down Expand Up @@ -205,7 +201,16 @@ object Openlaw extends LazyLogging {
@JSExport
def missingAllIdentities(result:ValidationResult):Boolean = result.identities.nonEmpty && result.missingIdentities.length === result.identities.length

private def handleExecutionResult(executionResult:Result[TemplateExecutionResult]):js.Dictionary[Any] = executionResult match {
@JSExport
def deserializeExecutionResult(resultJson:String):SerializableTemplateExecutionResult = decode[SerializableTemplateExecutionResult](resultJson) match {
case Right(value) => value
case Left(ex) => throw new RuntimeException(ex.getMessage)
}

@JSExport
def serializeExecutionResult(executionResult:OpenlawExecutionState):String = executionResult.toSerializable.asJson.noSpaces

private def handleExecutionResult(executionResult:Result[OpenlawExecutionState]):js.Dictionary[Any] = executionResult match {
case Success(result) =>
result.state match {
case ExecutionFinished =>
Expand All @@ -214,7 +219,7 @@ object Openlaw extends LazyLogging {
"isError" -> false,
"missingTemplate" -> false,
"errorMessage" -> "")
case ExecutionWaitForTemplate(_, definition) =>
case ExecutionWaitForTemplate(_, definition, _) =>
js.Dictionary(
"executionResult" -> result,
"isError" -> false,
Expand Down Expand Up @@ -242,7 +247,7 @@ object Openlaw extends LazyLogging {
case (_, variable) => variable.varType(executionResult) match {
case _:NoShowInForm => false
case _ => true
}}).filter({case (_, variable) => variable.defaultValue.exists(_ => true)})
}}).filter({case (_, variable) => variable.defaultValue.isDefined})
.map({ case (result, variable) => js.Dictionary(
"name" -> variable.name.name,
"value" -> getInitialParameter(variable, result))
Expand Down Expand Up @@ -336,7 +341,7 @@ object Openlaw extends LazyLogging {
executionResult.agreements.map(agreement => {
Dictionary[Any](
"agreement" -> agreement,
"executionResult" -> agreement.executionResult,
"executionResult" -> executionResult.findExecutionResult(agreement.executionResultId).getOrElse(executionResult),
"mainTemplate" -> agreement.mainTemplate,
"showTitle" -> agreement.header.shouldShowTitle,
"name" -> agreement.name,
Expand Down Expand Up @@ -369,19 +374,15 @@ object Openlaw extends LazyLogging {
}

@JSExport
def isSignatory(email:String, executionResult: TemplateExecutionResult):Boolean = {
executionResult
def isSignatory(email:String, executionResult: TemplateExecutionResult):Boolean = executionResult
.getVariableValues[Identity](IdentityType)
.exists(_.email.email === email)
}

@JSExport
def getSections(document:TemplateExecutionResult):js.Array[String] =
document.variableSectionList.toJSArray
def getSections(document:TemplateExecutionResult):js.Array[String] = document.variableSectionList.toJSArray

@JSExport
def getVariableSections(document:TemplateExecutionResult):js.Dictionary[js.Array[String]] =
document.sections
def getVariableSections(document:TemplateExecutionResult):js.Dictionary[js.Array[String]] = document.sections
.map({case (key,variables) => key -> variables.map(_.name).toJSArray}).toJSDictionary

@JSExport
Expand All @@ -394,20 +395,18 @@ object Openlaw extends LazyLogging {
def isHidden(variableDefinition: VariableDefinition):Boolean = variableDefinition.isHidden

@JSExport
def getCollectionSize(variable:VariableDefinition, value:String, executionResult: TemplateExecutionResult):Int = {
def getCollectionSize(variable:VariableDefinition, value:String, executionResult: TemplateExecutionResult):Int =
getCollection(variable, executionResult, value).size
}

@JSExport
def createVariableFromCollection(variable:VariableDefinition, index:Int, executionResult: TemplateExecutionResult):VariableDefinition = {
def createVariableFromCollection(variable:VariableDefinition, index:Int, executionResult: TemplateExecutionResult):VariableDefinition =
variable.varType(executionResult) match {
case collectionType:CollectionType =>
VariableDefinition(VariableName(variable.name.name + "_" + index), variableTypeDefinition = Some(VariableTypeDefinition(collectionType.typeParameter.name)), description = Some(getDescription(variable)))

case _ =>
throw new RuntimeException(s"add element to collection only works for a variable of type Collection, not '${variable.varType(executionResult).name}'")
}
}

@JSExport
def addElementToCollection(variable:VariableDefinition, value:String, executionResult: TemplateExecutionResult):String = {
Expand Down

0 comments on commit 901afa1

Please sign in to comment.