Skip to content

Commit ee50306

Browse files
committed
template: Rename base64 property for dataFile to toBase64 and add a fromBase64
1 parent f6f6fa7 commit ee50306

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,20 @@ Parameters with a `dataFile` property:
320320
* have their `default` set to the contents of the file
321321
* given a default `format` of `hidden`
322322
323-
Additionally, the contents of the data file can be base64 encoded before being used as for `default` by setting the `base64` property to `true`:
323+
Additionally, the contents of the data file can be base64-encoded before being used as for `default` by setting the `toBase64` property to `true`:
324324
325325
```yaml
326326
definitions:
327327
var:
328328
dataFile: example
329-
base64: true
329+
toBase64: true
330330
template: |
331331
{{var}}
332332
```
333333
334+
Similarly, if the data file is base64-encoded, it can be decoded using `fromBase64`.
335+
If both `toBase64` and `fromBase64` are set, then `toBase64` takes precedence.
336+
334337
## Development
335338
336339
* To check for lint errors run `npm run lint`

lib/template.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,10 @@ class Template {
412412
}
413413

414414
const dataFile = dataFiles[propDef.dataFile];
415-
if (propDef.base64) {
415+
if (propDef.toBase64) {
416416
propDef.default = Buffer.from(dataFile, 'utf8').toString('base64');
417+
} else if (propDef.fromBase64) {
418+
propDef.default = Buffer.from(dataFile, 'base64').toString('utf8');
417419
} else {
418420
propDef.default = dataFile;
419421
}

test/template.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,21 @@ describe('Template class tests', function () {
12591259
dataFile: textData.txt
12601260
base64:
12611261
dataFile: textData.txt
1262-
base64: true
1262+
toBase64: true
1263+
decoded:
1264+
dataFile: textData.txt
1265+
fromBase64: true
1266+
toAndFrom:
1267+
dataFile: textData.txt
1268+
toBase64: true
1269+
fromBase64: true
12631270
template: |-
12641271
{{fromFile}}
12651272
{{base64}}
1273+
{{decoded}}
1274+
{{toAndFrom}}
12661275
`;
1267-
const reference = 'Lorem ipsum\n\nTG9yZW0gaXBzdW0K';
1276+
const reference = 'Lorem ipsum\n\nTG9yZW0gaXBzdW0K\n.�ޚ*l�\nTG9yZW0gaXBzdW0K';
12681277
return Template.loadYaml(yamldata, { dataProvider })
12691278
.then((tmpl) => {
12701279
const schema = tmpl.getParametersSchema();
@@ -1277,6 +1286,8 @@ describe('Template class tests', function () {
12771286
assert.strictEqual(fileProp.default, 'Lorem ipsum\n');
12781287

12791288
assert.strictEqual(schema.properties.base64.default, 'TG9yZW0gaXBzdW0K');
1289+
assert.strictEqual(schema.properties.decoded.default, '.�ޚ*l�');
1290+
assert.strictEqual(schema.properties.toAndFrom.default, 'TG9yZW0gaXBzdW0K');
12801291

12811292
const rendered = tmpl.render();
12821293
console.log(rendered);

0 commit comments

Comments
 (0)