daml
daml copied to clipboard
[FEATURE REQUEST] Add a decoder for exercise events to the Java codegen
What is the problem you want to solve?
Can we extend the Java codegen to include a decoder for choices as well as templates? From an ExercisedEvent.
This would allow us to traverse transaction tree's and write logic that depends on a specific model event as opposed to reinterpreting the structure of a general ExercisedEvent.
cc @asarpeshkar-da
Right now the Java codegen includes a decoder which allows you to translate a created event into a the generated Java representation of the Daml contract whose creation happens as part of said event. In this representation, choices are modeled as methods on the Java object that "wraps" a Daml contract
Based on what you wrote, you would like to:
- have some kind of data representation of a choice (let's call it
Choice) that has been taken, whose exact structure is expressed as a Java class generated in the same way asContracts, with fields and/or accessor methods that allow to directly tap into- the arguments of the choice
- the result of exercising the choice
- possibly (but this could be more complicated) the sequence of consequences that exercising the choice has had in terms of
ChoiceandContractobjects, rather than a sequence of child event identifiers as in theExercisedEvent
- have another decoder that turns an
ExercisedEventinto aChoiceobject
Is the phrasing I used to expand on your ask correct and complete?
I'd also like a more finely typed way of traversing the transaction tree. To the above, I'd add that the arguments and results should also use generated types rather than the generic Value AST type.
It would also be nice if the ExercisedEvent -> Choice decoder implemented a deep transformation so one could then implement a tree traversal as a bunch of type tests on Choice values.
Yes, I probably wrote it badly, but both were points I was trying to make in my reply, thanks for confirming.
@stefanobaghino-da Yes. I think 1.i, 1.ii and 2 would go a really long way already. But once you have those, I think that you're in reach of 1.iii too, which would be awesome.
Depends on:
- [x] #14313
- [x] #13768
- [x] #14329
With those we will not even need to generate any new code to support an exercise event decoder.
Implementation plan for this feature:
- [ ] a builder
<Z>that takes a series of pairs<Id, Arg, Res> (Choice<Id, Arg, Res>, EEv<ContractId<Id>, Arg, Res> => Z)whereEEvis a generalization ofExercisedto produce a stream ofZ- [ ] a function that takes unhandled exercises to
Zcan be supplied as well; if undefined, unrecognized choices will simply be discarded - [ ] the utility that takes a single
Choice<Id, Arg, Res>and produces a stream ofEEv<ContractId<Id>, Arg, Res>is trivially derivable from that by simply passingidentityas the function arg - [ ] likewise, if desired, a "callback" variant that discards results can be defined simply by setting
Z = Void
- [ ] a function that takes unhandled exercises to
- [ ] a function from
ContractDecodertoExerciseDecoderthat produces untyped but codegen-classedEEvs- [ ] this can be defined using the above-described builder
- [ ] it will also require extending
ContractDecoderto takeInterfaceCompanions as well, possibly by taking anyContractTypeCompanions, and - [ ] changing codegen to pass in all the
INTERFACEs as well when generating a ContractDecoder subclass
- [ ] perhaps in ouroboros fashion a synthesis of these, where a fallback can be assembled that itself uses an untyped
ExerciseDecoder, which uses its own builder...this implements 1(iii) from @stefanobaghino-da 's post - [ ] #15437, perhaps
For posterity, the thing we have called Choice in the bindings now is not what @stefanobaghino-da refers to as Choice above; instead I have called the latter Choice EEv in the above plan.