I am building a workflow where the blockchain holds the state of the workflow. I have built a DAML template that is a 1:1 representation of a JSON schema I interact with, however, some of the JSON keys have names like “type” and when I try to use these words as field names, I get “parse error on input ‘type’”
How can I use the word “type” as a field name?
I don’t think there is any way to do this in the language.
It would technically be possible to write a Daml-LF directly that uses such a record field name. However, even in LF, field names are restricted to [a-zA-Z_\$][a-zA-Z0-9_\$]{0,999}, so this will not work for all possible JSON field names.
Taking into account the exceptional difficulty of writing LF directly, as well as that it is not possible to customize the JSON representation of a given Daml type as used by JSON API, it would probably be simpler to assume that some translation between queried ledger values and the JSON schema you refer to will be required, sooner or later.
@Mark_Broadhead I tend to add an underscore to that name when I want to use that name, type_.
I’ll go a bit on a tangent here: this sounds like a bit of a smell. While we do have tooling that allows you to do this, I would recommend avoiding coupling tightly between your Daml models and the client representation of data. Daml allows you to tap in into a virtual global shared ledger, a common bus where applications can interact with data across organizational borders. I would strongly recommend you strive to make sure that your Daml models and the API exposed to clients by your Daml app can evolve independently.
1 Like
Thanks for pointing this out. You’re right, this is a code smell. I need to refactor this model to not be tightly coupled.