Skip to content

Commit

Permalink
fix(optionals): Fix optionals (#50)
Browse files Browse the repository at this point in the history
* Fix optionals

* Add "acceptance test"

* More just an "example" at this point

* Minor nit
  • Loading branch information
b3ross authored Mar 28, 2020
1 parent 317eb79 commit 51e7ddf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
10 changes: 4 additions & 6 deletions example/env.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
default_env: &default_env
IA_ENV: ${env:IA_ENV}
FOO: ${cft:forms-api-fargate-stack.ServiceURL}
BAZ: BAR
CRED: ${cred:IA_VPC_PRIVATE_SUBNET1_ID}
CRED_OPTIONAL:
value: ${cred:IDSFJKLJ}
optional: true
TEST: ${poop:hello}
INTEGER_VALUE: 3000
BOOLEAN_TRUE_VALUE: true
BOOLEAN_FALSE_VALUE: false
VALUE_WITH_SURROUNDING_STRINGS: test-${IA_ENV}-test
VALUE_WITH_SURROUNDING_STRINGS: test-${env:IA_ENV}-test
OPTIONAL_VALUE:
value: ${env:IA_OPTIONAL}
optional: true

sandbox:
<<: *default_env
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"build": "yarn tsc",
"test": "yarn jest",
"precommit": "lint-staged",
"release": "semantic-release"
"release": "semantic-release",
"example": "pushd example && IA_ENV=test node ../dist/index.js -f env.yml -s development && popd"
},
"bin": {
"dotenvi": "dist/index.js"
Expand Down
14 changes: 14 additions & 0 deletions src/rewriter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@ describe('Rewriter', () => {
expect(output['test']).toBe('footestbartest-test');
});
});

it('Supports optionals', () => {
const document = {
test: {
value: '${env:OPTIONAL}',
optional: true
}
};

const rewriter = new Rewriter({ resolvers: resolvers });
return rewriter.rewrite(document).then(output => {
expect(output['test']).toBe('');
});
});
});
11 changes: 6 additions & 5 deletions src/rewriter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Document, InputDocument, Config, Primitive } from './types';
import { isNullOrUndefined } from 'util';

export class Rewriter {
constructor(private config: Config) {}
Expand Down Expand Up @@ -31,12 +32,12 @@ export class Rewriter {
if (!resolver) {
throw new Error(`Could not locate resolver for value ${value}`);
}
const innerValue = matchResults ? matchResults[2] : value;
let innerResult = await resolver(innerValue, this.config);
if (!innerResult) {
throw new Error(`Resolver ${resolverName} didn't return any value`);
const argument = matchResults ? matchResults[2] : value;
let resolved = await resolver(argument, this.config);
const rewrittenValue = await this.rewriteValue(resolved);
if (!isNullOrUndefined(rewrittenValue)) {
result += rewrittenValue;
}
result += await this.rewriteValue(innerResult);
capture = '';
} else if (capture) {
capture += c;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function validateOutput(input: InputDocument, output: Document): string[]
const keys = Object.keys(input);
for (const key of keys) {
if (!input[key].optional) {
if (!(key in output) || output[key] === undefined) {
if (!(key in output) || output[key] === undefined || output[key] === '') {
errors.push(`${key} is a required variable but is not specified in result`);
}
}
Expand Down

0 comments on commit 51e7ddf

Please sign in to comment.