Skip to content

Commit

Permalink
fix(boolean,integer): Support integer and boolean values (#26)
Browse files Browse the repository at this point in the history
* fix(integer): Support integer values

* fix(boolean): also fix booleans
  • Loading branch information
Ben Ross authored Jun 8, 2018
1 parent 42fa44e commit c1c87db
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
3 changes: 3 additions & 0 deletions example/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ default_env: &default_env
value: ${cred:IDSFJKLJ}
optional: true
TEST: ${poop:hello}
INTEGER_VALUE: 3000
BOOLEAN_TRUE_VALUE: true
BOOLEAN_FALSE_VALUE: false

sandbox:
<<: *default_env
Expand Down
2 changes: 1 addition & 1 deletion src/inputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function parse(contents: string, stage?: string): InputDocument {
const keys = Object.keys(document);
for (const key of keys) {
const value = document[key];
if (typeof value === 'string') {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
result[key] = { value: value };
} else if (value.optional && value.value) {
result[key] = {
Expand Down
26 changes: 15 additions & 11 deletions src/rewriter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document, InputDocument, Config } from './types';
import { Document, InputDocument, Config, Primitive } from './types';

export class Rewriter {
constructor(private config: Config) { }
Expand All @@ -13,17 +13,21 @@ export class Rewriter {
return result;
}

private async rewriteValue(value: string): Promise<string> {
const regex = new RegExp('\\${([a-z]+):(.*)}');
const results = value.match(regex);
const resolverName = results && results[1];
const innerValue = results ? results[2] : value;
const resolver = this.getResolver(resolverName);
if (!resolver) {
throw new Error(`Could not locate resolver for value ${value}`);
private async rewriteValue(value: Primitive): Promise<Primitive> {
if (typeof value === 'string') {
const regex = new RegExp('\\${([a-z]+):(.*)}');
const results = value.match(regex);
const resolverName = results && results[1];
const innerValue = results ? results[2] : value;
const resolver = this.getResolver(resolverName);
if (!resolver) {
throw new Error(`Could not locate resolver for value ${value}`);
}
const result = await resolver(innerValue, this.config);
return result;
} else {
return value;
}
const result = await resolver(innerValue, this.config);
return result;
}

private getResolver(name: string) {
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ export type ResolverFunction = (arg: string, config: Config) => Promise<string>;
export type ResolverMap = {
[name: string]: ResolverFunction
};
export type Document = { [name: string]: string }

export type Primitive = string | number | boolean;
export type Document = { [name: string]: Primitive }

export class InputDocument {
[name: string]: {
value: string;
value: Primitive;
optional?: boolean
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as fs from 'fs';
import { Document, InputDocument, Config } from './types';
import { resolvers } from './resolvers';

export function writeFile(document: { [name: string]: string }) {
export function writeFile(document: Document) {
let output = '';
const keys = Object.keys(document);
for (const key of keys) {
if (document[key]) {
if (document[key] !== undefined) {
output += `${key}=${document[key]}\n`;
}
}
Expand All @@ -20,7 +20,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])) {
if (!(key in output) || output[key] === undefined) {
errors.push(`${key} is a required variable but is not specified in result`);
}
}
Expand Down

0 comments on commit c1c87db

Please sign in to comment.