csv: strip outer whitespace when interpolating CSV values (#1051)

This removes a potential snag in amount field assignments, and
hopefully is harmless and acceptable otherwise.
This commit is contained in:
Simon Michael 2019-06-14 16:43:12 -07:00
parent d2b3185062
commit 0a3af8d9ee
2 changed files with 7 additions and 1 deletions

View File

@ -850,10 +850,12 @@ getEffectiveAssignment rules record f = lastMay $ assignmentsFor f
where
csvline = intercalate "," record
-- | Render a field assigment's template, possibly interpolating referenced
-- CSV field values. Outer whitespace is removed from interpolated values.
renderTemplate :: CsvRules -> CsvRecord -> FieldTemplate -> String
renderTemplate rules record t = regexReplaceBy "%[A-z0-9]+" replace t
where
replace ('%':pat) = maybe pat (\i -> atDef "" record (i-1)) mindex
replace ('%':pat) = maybe pat (\i -> strip $ atDef "" record (i-1)) mindex
where
mindex | all isDigit pat = readMay pat
| otherwise = lookup (map toLower pat) $ rcsvfieldindexes rules

View File

@ -161,8 +161,12 @@ amount USD %4
# combine three fields to make a comment (containing two tags)
comment note: %somefield - %anotherfield, date: %1
```
Field assignments can be used instead of or in addition to a field list.
Note, interpolation strips any outer whitespace, so a CSV value like
`" 1 "` becomes `1` when interpolated.
## conditional block
`if` *`PATTERN`*\