Skip to content

Commit 72fa1f2

Browse files
committed
Merge pull request #233 from ParsePlatform/nlutsenko.transform.cleaner
Split basic type conversions from other logic in transform.js.
2 parents 27560e1 + 21aef05 commit 72fa1f2

File tree

1 file changed

+104
-31
lines changed

1 file changed

+104
-31
lines changed

transform.js

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
363363
objectId: atom.objectId
364364
};
365365
}
366-
if (atom.__type == 'Date') {
367-
return new Date(atom.iso);
366+
if (DateCoder.isValidJSON(atom)) {
367+
return DateCoder.JSONToDatabase(atom);
368368
}
369-
if (atom.__type == 'GeoPoint') {
370-
if (!inArray && !inObject) {
371-
return [atom.longitude, atom.latitude];
372-
}
373-
return atom;
369+
if (BytesCoder.isValidJSON(atom)) {
370+
return BytesCoder.JSONToDatabase(atom);
374371
}
375-
if (atom.__type == 'Bytes') {
376-
return new mongodb.Binary(new Buffer(atom.base64, 'base64'));
372+
if (GeoPointCoder.isValidJSON(atom)) {
373+
return (inArray || inObject ? atom : GeoPointCoder.JSONToDatabase(atom));
377374
}
378-
if (atom.__type == 'File') {
379-
if (!inArray && !inObject) {
380-
return atom.name;
381-
}
382-
return atom;
375+
if (FileCoder.isValidJSON(atom)) {
376+
return (inArray || inObject ? atom : FileCoder.JSONToDatabase(atom));
383377
}
384378

385379
if (force) {
@@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
620614
return Parse._encode(mongoObject);
621615
}
622616

623-
if (mongoObject instanceof mongodb.Binary) {
624-
return {
625-
__type: 'Bytes',
626-
base64: mongoObject.buffer.toString('base64')
627-
};
617+
if (BytesCoder.isValidDatabaseObject(mongoObject)) {
618+
return BytesCoder.databaseToJSON(mongoObject);
628619
}
629620

630621
var restObject = untransformACL(mongoObject);
@@ -699,20 +690,14 @@ function untransformObject(schema, className, mongoObject) {
699690
//} else if (mongoObject[key] === null) {
700691
//break;
701692
} else {
702-
var expected = schema.getExpectedType(className, key);
703-
if (expected == 'file' && mongoObject[key]) {
704-
restObject[key] = {
705-
__type: 'File',
706-
name: mongoObject[key]
707-
};
693+
var expectedType = schema.getExpectedType(className, key);
694+
var value = mongoObject[key];
695+
if (expectedType === 'file' && FileCoder.isValidDatabaseObject(value)) {
696+
restObject[key] = FileCoder.databaseToJSON(value);
708697
break;
709698
}
710-
if (expected == 'geopoint') {
711-
restObject[key] = {
712-
__type: 'GeoPoint',
713-
latitude: mongoObject[key][1],
714-
longitude: mongoObject[key][0]
715-
};
699+
if (expectedType === 'geopoint' && GeoPointCoder.isValidDatabaseObject(value)) {
700+
restObject[key] = GeoPointCoder.databaseToJSON(value);
716701
break;
717702
}
718703
}
@@ -726,6 +711,94 @@ function untransformObject(schema, className, mongoObject) {
726711
}
727712
}
728713

714+
var DateCoder = {
715+
JSONToDatabase(json) {
716+
return new Date(json.iso);
717+
},
718+
719+
isValidJSON(value) {
720+
return (typeof value === 'object' &&
721+
value !== null &&
722+
value.__type === 'Date'
723+
);
724+
}
725+
};
726+
727+
var BytesCoder = {
728+
databaseToJSON(object) {
729+
return {
730+
__type: 'Bytes',
731+
base64: object.buffer.toString('base64')
732+
};
733+
},
734+
735+
isValidDatabaseObject(object) {
736+
return (object instanceof mongodb.Binary);
737+
},
738+
739+
JSONToDatabase(json) {
740+
return new mongodb.Binary(new Buffer(json.base64, 'base64'));
741+
},
742+
743+
isValidJSON(value) {
744+
return (typeof value === 'object' &&
745+
value !== null &&
746+
value.__type === 'Bytes'
747+
);
748+
}
749+
};
750+
751+
var GeoPointCoder = {
752+
databaseToJSON(object) {
753+
return {
754+
__type: 'GeoPoint',
755+
latitude: object[1],
756+
longitude: object[0]
757+
}
758+
},
759+
760+
isValidDatabaseObject(object) {
761+
return (object instanceof Array &&
762+
object.length == 2
763+
);
764+
},
765+
766+
JSONToDatabase(json) {
767+
return [ json.longitude, json.latitude ];
768+
},
769+
770+
isValidJSON(value) {
771+
return (typeof value === 'object' &&
772+
value !== null &&
773+
value.__type === 'GeoPoint'
774+
);
775+
}
776+
};
777+
778+
var FileCoder = {
779+
databaseToJSON(object) {
780+
return {
781+
__type: 'File',
782+
name: object
783+
}
784+
},
785+
786+
isValidDatabaseObject(object) {
787+
return (typeof object === 'string');
788+
},
789+
790+
JSONToDatabase(json) {
791+
return json.name;
792+
},
793+
794+
isValidJSON(value) {
795+
return (typeof value === 'object' &&
796+
value !== null &&
797+
value.__type === 'File'
798+
);
799+
}
800+
};
801+
729802
module.exports = {
730803
transformKey: transformKey,
731804
transformCreate: transformCreate,

0 commit comments

Comments
 (0)