Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(boolean,integer): Support integer and boolean values #26

Merged
merged 2 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]+):(.*)}');
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test where we pull an integer from credstash

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