Skip to content

Commit

Permalink
feat(unmarshal): add rudimentary support for $transform
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Sep 13, 2017
1 parent c4dbd1b commit 8bdf8db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/unmarshal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ function visitArray(arr, schema, projection, path) {
}

try {
handleCast(arr, index, schema._paths[newPath].$type);
const pathOptions = schema._paths[newPath];
handleCast(arr, index, pathOptions.$type, pathOptions.$transform);
} catch(err) {
error.markError(join(path, index, true), err);
}
Expand Down Expand Up @@ -168,8 +169,9 @@ function visitObject(obj, schema, projection, path) {
// If type not specified, no type casting
return;
}
const pathOptions = schema._paths[newPath];

if (schema._paths[newPath].$type === Array ||
if (pathOptions.$type === Array ||
Array.isArray(schema._paths[newPath].$type)) {
let res = visitArray(value, schema, projection, newPath);
if (res.error) {
Expand All @@ -178,11 +180,14 @@ function visitObject(obj, schema, projection, path) {
}
obj[key] = res.value;
return;
} else if (schema._paths[newPath].$type === Object) {
} else if (pathOptions.$type === Object) {
if (value == null) {
delete obj[key];
return;
}
if (pathOptions.$transform != null) {
value = pathOptions.$transform(obj[key]);
}
let res = visitObject(value, schema, projection, newPath);
if (res.error) {
debug('merge', res.error.errors);
Expand All @@ -193,7 +198,7 @@ function visitObject(obj, schema, projection, path) {
}

try {
handleCast(obj, key, schema._paths[newPath].$type);
handleCast(obj, key, pathOptions.$type, pathOptions.$transform);
} catch(err) {
error.markError(join(path, key, true), err);
}
Expand Down
8 changes: 6 additions & 2 deletions src/unmarshal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

const to = require('../to');

exports.handleCast = function(obj, key, type) {
obj[key] = to(obj[key], type);
const noop = x => x;

exports.handleCast = function(obj, key, type, transform) {
transform = transform == null ? noop : transform;
console.log('F', transform)
obj[key] = to(transform(obj[key]), type);
};

exports.realPathToSchemaPath = function(path) {
Expand Down
16 changes: 14 additions & 2 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,26 @@ describe('unmarshal()', function() {

assert.deepEqual(Test.paths().filter(v => !v.$description), [
{ path: 'str', $type: 'string' }
])
]);
assert.deepEqual(Test.paths().filter(v => !!v.$description), [
{
path: 'num',
$type: 'number',
$description: 'this is a number'
}
])
]);
});

it('$transform', function() {
const Test = new Archetype({
str: {
$type: Object,
$transform: JSON.parse
}
}).compile();

const doc = new Test({ str: JSON.stringify({ hello: 'world' }) });
assert.deepEqual(doc.str, { hello: 'world' });
});

it('to()', function() {
Expand Down

0 comments on commit 8bdf8db

Please # to comment.