mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-19 08:48:21 +03:00
Generate json encoders for choice args/results (#18834)
* Generate json encoders for choice args/results
This commit is contained in:
parent
9fa944a38b
commit
78e02aed89
@ -85,7 +85,7 @@ object DecoderClass {
|
||||
|
||||
private val decodersField = FieldSpec
|
||||
.builder(contractDecoderType, "contractDecoder")
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
|
||||
.build()
|
||||
|
||||
def generateStaticInitializer(templateNames: Iterable[ClassName]): CodeBlock = {
|
||||
|
@ -524,7 +524,7 @@ private[inner] object TemplateClass extends StrictLogging {
|
||||
Modifier.PUBLIC,
|
||||
)
|
||||
.initializer(
|
||||
"$Z$T.create($>$S, value$$ -> $L,$Wvalue$$ ->$W$L,$Wvalue$$ ->$W$L,$W$L,$W$L)$<",
|
||||
"$Z$T.create($>$S, value$$ -> $L,$Wvalue$$ ->$W$L,$Wvalue$$ ->$W$L,$W$L,$W$L,$W$L,$W$L)$<",
|
||||
fieldClass,
|
||||
choiceName,
|
||||
generateToValueConverter(
|
||||
@ -546,6 +546,8 @@ private[inner] object TemplateClass extends StrictLogging {
|
||||
),
|
||||
FromJsonGenerator.jsonDecoderForType(choice.param),
|
||||
FromJsonGenerator.jsonDecoderForType(choice.returnType),
|
||||
ToJsonGenerator.encoderOf(choice.param),
|
||||
ToJsonGenerator.encoderOf(choice.returnType),
|
||||
)
|
||||
.build()
|
||||
}.toSeq
|
||||
|
@ -244,7 +244,7 @@ private[inner] object ToJsonGenerator {
|
||||
.returns(fieldClass)
|
||||
.build()
|
||||
|
||||
private def encoderOf(
|
||||
private[inner] def encoderOf(
|
||||
damlType: Type,
|
||||
nesting: Int = 0, // Used to avoid clashing argument identifiers in nested encoder definitions
|
||||
)(implicit packagePrefixes: PackagePrefixes): CodeBlock = {
|
||||
|
@ -3,12 +3,24 @@
|
||||
|
||||
module Bar where
|
||||
|
||||
data Result = Result
|
||||
with
|
||||
result : Int
|
||||
deriving (Eq, Show)
|
||||
|
||||
template Bar
|
||||
with
|
||||
owner : Party
|
||||
where
|
||||
signatory owner
|
||||
|
||||
choice AddOne : Result
|
||||
with
|
||||
value : Int
|
||||
controller owner
|
||||
do
|
||||
return Result with result = value + 1
|
||||
|
||||
data ParameterizedContractId a = ParameterizedContractId
|
||||
with
|
||||
parameterizedContractId: ContractId a
|
||||
@ -20,4 +32,3 @@ template Haha
|
||||
owner : Party
|
||||
where
|
||||
signatory owner
|
||||
|
||||
|
@ -5,9 +5,12 @@ package com.daml.lf.codegen.backend.java
|
||||
|
||||
import com.daml.ledger.javaapi.data.Unit
|
||||
import com.daml.ledger.javaapi.data.codegen.Choice
|
||||
import com.daml.ledger.javaapi.data.codegen.json.JsonLfReader
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import org.scalatest.wordspec.AnyWordSpec
|
||||
import ut.bar.Bar
|
||||
import ut.bar.AddOne
|
||||
import ut.bar.Result
|
||||
import ut.da.internal.template.Archive
|
||||
|
||||
import scala.jdk.CollectionConverters._
|
||||
@ -23,8 +26,30 @@ final class ChoiceFieldsSpec extends AnyWordSpec with Matchers {
|
||||
val choices = Bar.COMPANION.choices
|
||||
val names = choices.keySet()
|
||||
|
||||
choices.size() shouldBe 1
|
||||
names shouldBe Set("Archive").asJava
|
||||
choices.size() shouldBe 2
|
||||
names shouldBe Set("Archive", "AddOne").asJava
|
||||
}
|
||||
|
||||
"encode and decode choice arguments in json" in {
|
||||
val choice: Choice[Bar, AddOne, Result] = Bar.CHOICE_AddOne
|
||||
val dummyArg = new AddOne(4)
|
||||
val encodedArg = choice.argJsonEncoder(dummyArg).intoString()
|
||||
|
||||
"{\"value\": \"4\"}" shouldBe encodedArg
|
||||
|
||||
val decodedArg = choice.argJsonDecoder.decode(new JsonLfReader(encodedArg))
|
||||
dummyArg shouldBe decodedArg
|
||||
}
|
||||
|
||||
"encode and decode choice results in json" in {
|
||||
val choice: Choice[Bar, AddOne, Result] = Bar.CHOICE_AddOne
|
||||
val dummyResult = new Result(5)
|
||||
val encodedResult = choice.resultJsonEncoder(dummyResult).intoString()
|
||||
|
||||
"{\"result\": \"5\"}" shouldBe encodedResult
|
||||
|
||||
val decodedResult = choice.resultJsonDecoder.decode(new JsonLfReader(encodedResult))
|
||||
dummyResult shouldBe decodedResult
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user