Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from docker/v1.1.1
Browse files Browse the repository at this point in the history
made sure to include transform and casting after looking up value
  • Loading branch information
Patrick Camacho authored Sep 14, 2016
2 parents 55d4d99 + e061361 commit 2e4ec57
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bonito/pulpo",
"version": "1.1.0",
"version": "1.1.1",
"description": "Configuration mechanism",
"author": "Patrick Camacho <[email protected]>",
"license": "MIT",
Expand Down
79 changes: 47 additions & 32 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,44 @@ interface HydratedConfig {
[optName: string]: any;
}

function stringLookup(str: string, config: HydratedConfig): string {
return str.replace(/(\$\{(.*)\})/gi, (match, group1, key) => {
return dotty.get(config, key);
});
}

function getter(config: HydratedConfig, value: any, path: string, validate: boolean): any {
let resolvedValue: any;

switch (typeof value) {
function getter(
config: HydratedConfig,
property: Property,
path: string,
rawValue: any,
flags: HydrateOptionsDefinition
): any {
let value: any;

switch (typeof rawValue) {
case 'function':
resolvedValue = value(config, path);
value = rawValue(config, path);
break;
case 'string':
resolvedValue = stringLookup(value, config);
value = rawValue.replace(/(\$\{(.*)\})/gi, (
match: string,
group1: string,
key: string
) => dotty.get(config, key));
break;
default:
resolvedValue = value;
value = rawValue;
}

if (validate) this.definition[path].validate(resolvedValue);
return resolvedValue;
if (flags.transform) value = property.transform(value, config);
if (flags.cast) value = property.cast(value);
if (flags.validate) property.validate(value);

return value;
};

export default class Schema {
definition: ParsedSchemaDefinition;

static parse(rawDefinition: SchemaDefinition, startingPath?: String): ParsedSchemaDefinition {
static parse(
rawDefinition: SchemaDefinition,
startingPath?: String
): ParsedSchemaDefinition {
// 1. Loop through keys on object
// 2. Determine if value assigned to key is a Property or a nested object
// 3. Convert properties to Property objects
Expand Down Expand Up @@ -94,25 +104,30 @@ export default class Schema {
validate: !Reflect.has(options, 'validate') || options.validate,
}

// Loop over and hydrate the object with getters

const hydratedConfig: HydratedConfig = Object.keys(this.definition).reduce((obj, key) => {
const property = this.definition[key];

let value = property.resolve(rawConfig);

if (flags.transform) value = property.transform(value, rawConfig);
if (flags.cast) value = property.cast(value);
// Find all the schema paths we need to trace
const paths = Object.keys(this.definition);

Object.defineProperty(obj, key, {get: getter.bind(this, obj, value, key, flags.validate) });
return obj
// Loop over and hydrate the object with getters
const hydratedConfig: HydratedConfig = paths.reduce((config, path) => {
const property = this.definition[path];

const curriedGetter = getter.bind(
this,
config,
property,
path,
property.resolve(rawConfig),
flags
);

Object.defineProperty(config, path, {get: curriedGetter });
return config;
}, {});

return Object.keys(this.definition).reduce((obj: HydratedConfig, key: string) => {
const value: any = hydratedConfig[key];

if (value) dotty.put(obj, key, value);
return obj;
// Evaluate each of the paths provided in the schema
return paths.reduce((config, key) => {
dotty.put(config, key, hydratedConfig[key]);
return config;
}, {});
}
}
Expand Down

0 comments on commit 2e4ec57

Please sign in to comment.