Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for dynamic defaults. #385

Merged
merged 8 commits into from
Dec 19, 2019
Merged

Conversation

nribeka
Copy link
Contributor

@nribeka nribeka commented Oct 4, 2019

Fix #338.

This will add dynamic default handler for the form. This failed validation on both enketo and ODKValidator I think.

@codecov-io
Copy link

codecov-io commented Oct 4, 2019

Codecov Report

Merging #385 into master will increase coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #385      +/-   ##
=========================================
+ Coverage   82.42%   82.5%   +0.08%     
=========================================
  Files          23      23              
  Lines        3260    3281      +21     
  Branches      763     770       +7     
=========================================
+ Hits         2687    2707      +20     
  Misses        433     433              
- Partials      140     141       +1
Impacted Files Coverage Δ
pyxform/xls2json.py 78.05% <100%> (+0.17%) ⬆️
pyxform/section.py 97.8% <100%> (+0.1%) ⬆️
pyxform/survey_element.py 90.86% <100%> (+0.5%) ⬆️
pyxform/question.py 92.47% <100%> (ø) ⬆️
pyxform/xform2json.py 79.66% <0%> (-0.21%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7fc5e90...5b3ca54. Read the comment docs.

@nribeka nribeka marked this pull request as ready for review October 7, 2019 20:19
if not self.default or not self.dynamic_default():
return

if self.parent.__class__.__name__ == "Survey" and in_binding:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone have comment on how to better do this, reply to this please.

@lognaturel
Copy link
Contributor

The tests look good.

For someone who wants to think about this at a high level, the proposed approach is to treat a default as dynamic if it contains one or more balanced pair of brackets ([]), parens (()) or curly braces ({}).

The only odd case I can think of that this would miss is an expression on numeric literals like 365/7. Of course, someone could just do that math and put in the result instead but it can be nice to use expressions to add meaning.

I also am not certain that it's so important for brackets/parens/braces to be balanced. I'd tend to treat anything that contains any of [](){}*/- as a dynamic expression.

@nribeka
Copy link
Contributor Author

nribeka commented Oct 11, 2019

@lognaturel you are thinking of all of the arithmetic ops there, right?

Adding arithmetic ops will make it think abc-data a dynamic default. I could add checking for text around the operator, but that will not work for func_a() - func_b(). I hope my question make sense.

@nribeka
Copy link
Contributor Author

nribeka commented Oct 11, 2019

@lognaturel and the second question, does code-cov update the coverage or just checking it once?

Copy link
Contributor

@lognaturel lognaturel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some comments inline about the strategies for identifying dynamic defaults. I think @MartijnR should comment on this because Enketo users will be affected by overly aggressive identification of dynamic defaults until Enketo adds support for them. @tiritea may also have thoughts.

ODK Collect users on older versions will also be affected in that the form will fail to open. This is not great because the error they get will be cryptic. However, the form would not have worked as indented when converted with prior versions of pyxform, either. For this reason, I don't believe dynamic defaults are very common. The likeliest source of problem will be someone who designs a form with dynamic defaults, tries it on a version of Collect v1.24+, and then pushes it out to users who have older Collect versions. This is painful but hopefully not fatal because folks generally do a test run when working with new forms.

  • @nribeka add a warning if any dynamic default is identified with text "This form definition contains dynamic defaults. Not all form filling software and versions support dynamic defaults so you should test the form with the software version you plan to use."
  • @MartijnR review the strategy for identifying dynamic defaults

expression = []
contains_dynamic = False
arithmetic_construct = {"*", "/", "+", "-"}
if self.type == "date":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all clients supported dynamic defaults, I would say that this case should be removed because it could be useful to have math in a date default. Given that not all clients support them, this does seem like the safest thing to do.

pyxform/survey_element.py Outdated Show resolved Hide resolved
@nribeka
Copy link
Contributor Author

nribeka commented Oct 24, 2019

@lognaturel added the warning. I moved the checking for dynamic to utils instead.

pyxform/utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lognaturel lognaturel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much!

I think it'd be great to get final 👍 from some or all of @ukanga @tiritea @MartijnR.

@yanokwa yanokwa added this to the v1.0.0 milestone Nov 18, 2019
Copy link
Contributor

@MartijnR MartijnR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, seems I forgot to press the submit button.

pyxform/tests_v1/test_dynamic_default.py Show resolved Hide resolved
@yanokwa yanokwa merged commit 7332103 into XLSForm:master Dec 19, 2019
lognaturel added a commit to lognaturel/pyxform that referenced this pull request Feb 3, 2020
It's not necessary and doesn't work as expected. See discussion at XLSForm#385 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for dynamic defaults
6 participants