OBPIH-7001 Add “Active” column to Product export/import#5556
OBPIH-7001 Add “Active” column to Product export/import#5556alannadolny merged 4 commits intodevelopfrom
Conversation
0fdc308 to
bff08c2
Compare
bff08c2 to
55160bc
Compare
| Boolean parseCsvBooleanField(String value, int rowCount) { | ||
| // If the value is empty we treat it as true | ||
| if (!value) { | ||
| return true | ||
| } | ||
| String parsedValue = value.toLowerCase() | ||
| Set<String> validValues = ['true', 'false', '1', '0'] | ||
| if (!(parsedValue in validValues)) { | ||
| throw new RuntimeException("Active field has to be either empty or a boolean value (true/false/1/0) at row " + rowCount) | ||
| } | ||
| return parsedValue in ['true', '1'] |
There was a problem hiding this comment.
I'm wondering if we could do this in a better way 🤔
There was a problem hiding this comment.
why not use String.toBoolean()?
This returns:
assert "true".toBoolean() == true
assert "TRUE".toBoolean() == true
assert " TrUe ".toBoolean() == true
assert "1".toBoolean() == true
assert "y".toBoolean() == true
assert "yes".toBoolean() == true
and is false otherwise. Then all you need to do is:
return value == null ? true : value.toBolean()
But if you're not using it because you want to throw an error in case of invalid input, then a few suggestions:
- make the method static and put it in a util class that we can re-use
- do
String parsedValue = value.trim().toLowerCase()(whitespace shouldn't matter) - add the following static constants:
Set<String> validBooleanValues = ['t', 'f', 'true', 'false', '1', '0', 'y', 'n', 'yes', 'no'] Set<String> trueBooleanValues = ['t, 'true', '1', 'y', 'yes']
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #5556 +/- ##
============================================
- Coverage 9.12% 8.54% -0.59%
+ Complexity 1170 1113 -57
============================================
Files 701 702 +1
Lines 45281 45346 +65
Branches 10851 10869 +18
============================================
- Hits 4131 3873 -258
- Misses 40497 40895 +398
+ Partials 653 578 -75 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| // FIXME make relation to Constants.EXPORT_PRODUCT_COLUMNS explicit | ||
| def row = [ | ||
| Id : product?.id, | ||
| Active : product.active ?: Boolean.FALSE, |
There was a problem hiding this comment.
parseCsvBooleanField defaults to true. Why is this default different?
| Boolean parseCsvBooleanField(String value, int rowCount) { | ||
| // If the value is empty we treat it as true | ||
| if (!value) { | ||
| return true | ||
| } | ||
| String parsedValue = value.toLowerCase() | ||
| Set<String> validValues = ['true', 'false', '1', '0'] | ||
| if (!(parsedValue in validValues)) { | ||
| throw new RuntimeException("Active field has to be either empty or a boolean value (true/false/1/0) at row " + rowCount) | ||
| } | ||
| return parsedValue in ['true', '1'] |
There was a problem hiding this comment.
why not use String.toBoolean()?
This returns:
assert "true".toBoolean() == true
assert "TRUE".toBoolean() == true
assert " TrUe ".toBoolean() == true
assert "1".toBoolean() == true
assert "y".toBoolean() == true
assert "yes".toBoolean() == true
and is false otherwise. Then all you need to do is:
return value == null ? true : value.toBolean()
But if you're not using it because you want to throw an error in case of invalid input, then a few suggestions:
- make the method static and put it in a util class that we can re-use
- do
String parsedValue = value.trim().toLowerCase()(whitespace shouldn't matter) - add the following static constants:
Set<String> validBooleanValues = ['t', 'f', 'true', 'false', '1', '0', 'y', 'n', 'yes', 'no'] Set<String> trueBooleanValues = ['t, 'true', '1', 'y', 'yes']
7619b36 to
63dabd1
Compare
| */ | ||
| static Boolean parseCsvBooleanField(String value, int rowCount) { | ||
| if (!value) { | ||
| return true |
There was a problem hiding this comment.
suggestion: it would probably be best for this to be a param so that it's configurable. You also might want to use StringUtils so that " " is also considered falsey:
static Boolean parseCsvBooleanField(String value, int rowCount, Boolean defaultValue) {
if (StringUtils.isBlank(value)) {
return defaultValue
}
....
| String parsedValue = value.trim().toLowerCase() | ||
|
|
||
| if (!(parsedValue in validBooleanValues)) { | ||
| throw new RuntimeException("Active field has to be either empty or a boolean value (${validBooleanValues.join(', ')}) at row ${rowCount}") |
There was a problem hiding this comment.
nitpick: I think you can just do ${validBooleanValues} because the Set.toString() method will output properly.
There was a problem hiding this comment.
Oh, I didn’t know that 😄 I thought it worked like in JavaScript and that you had to add .join().
There was a problem hiding this comment.
What's wrong with the square brackets? Your error message adds round brackets so you could just remove those and let it use the square ones instead:
"Active field has to be either empty or a boolean value ${validBooleanValues} at row ${rowCount}"
|
|
||
| class CsvUtil { | ||
| static final Set<String> validBooleanValues = ['t', 'f', 'true', 'false', '1', '0', 'y', 'n', 'yes', 'no'] | ||
| static final Set<String> trueBooleanValues = ['t', 'true', '1', 'y', 'yes'] |
There was a problem hiding this comment.
nitpick: please use all caps for constants: VALID_BOOLEAN_VALUES, TRUE_BOOLEAN_VALUES
There was a problem hiding this comment.
Initially, that’s what I wanted to do, but when I noticed that you wrote it in camelCase in previous comment, I decided to do the same. I will change it.

✨ Description of Change
Link to GitHub issue or Jira ticket:
https://pihemr.atlassian.net/browse/OBPIH-7001
Description:
📷 Screenshots & Recordings (optional)