Manipulating record in template

60 Views Asked by At

I'd like to make some structure over the code and introduce records in the template. With the simple version it works fine, though with records not working. Can you advise? Thanks

-- WORKING VERSION --

daml 1.2
module Magic_potion where

template Potion_Taken
  with
    holder : Party
    tube_id : Text
  where
    signatory holder

    controller holder can
      UpdateData : ContractId Potion_Taken
        with
          newTube_id : Text
        do
          create this with
            tube_id = newTube_id

sampling_1 = scenario do
  w <- getParty "Wizard 1"
  potionTaken <- submit w do create Potion_Taken with holder = w; tube_id = "tube 1"
  newPotionTaken <- submit w do exercise potionTaken UpdateData with newTube_id = "tube 2"
  submit w do 
    newP <- fetch newPotionTaken
    assert (newP.tube_id == "tube 2")

-- FAULTY VERSION --

data Content = Content
  with
    tube_id : Text

template Potion_Taken
  with
    holder : Party
    content : Content
  where
    signatory holder

    controller holder can
      UpdateData : ContractId Potion_Taken
        with
          newTube_id : Text
        do
          create this with 
            content.tube_id = newTube_id -- This line seems to be the trouble

sampling_1 = scenario do
  w <- getParty "Wizard 1"
  potionTaken <- submit w do create Potion_Taken with holder = h; content = Content with tube_id = "tube 1"
  newPotionTaken <- submit h do exercise potionTaken UpdateData with newTube_id = "tube 2"
  submit w do 
    newB <- fetch newPotionTaken
    assert (newP.content.tube_id == "tube 2")

1

There are 1 best solutions below

1
On
create this with 
  content.tube_id = newTube_id -- This line seems to be the trouble

This is indeed your problem. To avoid ambiguity, the with clause only allows a single level of naming. What this means is that if you want a new Content record in your Potion_Taken record, you will need to create one. Fortunately with nests cleanly so it isn't too cumbersome.

create this with
  content = Content with tube_id = newTube_id

Moreover, if Content has multiple fields and you only want to update a subset, the copy-constructor syntax also works here:

create this with
  content = this.content with tube_id = newTube_id