docassemble
docassemble copied to clipboard
DADicts can't fill keys with `[` or `]` in them
If I have a string key of a DADict that contains [ or ] in it, Docassemble can no longer find the generic object questions that can operate on it.
A minimally reproducible example: changes the player example from the documentation to include a [0] at the end of one of the objects.
---
objects:
- player: DADict
---
mandatory: True
code: |
player.initializeObject('trustee', DAObject)
player.initializeObject('beneficiary[0]', DAObject)
player.initializeObject('grantor', DAObject)
player.gathered = True
---
mandatory: True
question: The players
subquestion: |
% for type in player.keys():
${ player[type].firstname }
${ player[type].lastname } is here.
% endfor
---
generic object: DAObject
question: |
What is
${ x[i].object_possessive('name') }?
fields:
- First Name: x[i].firstname
- Last Name: x[i].lastname
I can look more into it soon-ish :tm:.
Why would you ever do this?
player.initializeObject('beneficiary[0]', DAObject)
It's related to some development I've been doing on our Efiling Server. Essentially, Docassemble asks that server if there are any additional questions it needs to ask to be able to Efile (as courts are allowed to change what pieces of information they require in filings dynamically), and it gets back a list of variables that are still required and some information about them. For example, an email might suddenly be required, so we need to ask users[0].email. I was trying to put those pieces of information into a DADict and then ask their value as a way of dynamically asking for variables from docassemble, kinda like DACatchAll, but with values from some external source, not already written in the interview.
The whole interview that caused this problem is in the EFSP integration, but I copied the blocks in question below. I worked around this issue by just replacing the [ and ] with _, but I didn't see a specific reason that keys to DADicts shouldn't look like variable names. Without that special definition of newName below, you get the same error as in the above example.
---
objects:
- fill_me: DADict
---
code: |
fill_me.there_are_any = True
for var in remaining_to_check["required_vars"]:
newName = var["name"].replace('.', '_').replace('[', '_').replace(']', '_')
fill_me.initializeObject(newName, DAObject)
fill_me[newName].name = var["name"]
fill_me[newName].description = var["description"]
fill_me.there_is_another = False
fill_me.gathered = True
fill_me_done = True
---
code: |
court_id = convert_court_to_id(trial_court)
remaining_to_check = proxy_conn.CheckFiling(court_id, al_court_bundle)
fill_me_done
for key in fill_me.keys():
define(fill_me[key].name, fill_me[key].value)
resp = proxy_conn.FileForReview(court_id, al_court_bundle)
e_file = True
It has to do with the regular expressions that parse strings that represent variable names. The presence of [0] in the string causes confusion.
That makes sense, didn't realize there was a regex at play. Don't think it's possible for a regex to handle situations like that. I'm curious if a stack parser could work and how much effort it might be, but I think for now the workaround above key.replace('[', '_').replace(']', '_'). is best. Just wanted to document it somewhere, because it did stump me for a while.
Closing this because of lack of activity.