Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Long committed Oct 28, 2017
1 parent dc6ef14 commit b5f10f9
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 5 deletions.
159 changes: 154 additions & 5 deletions lib/localFunction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var is = require('is');
var request = require('request');

var LocalFunction = function(trigger, urls, controller) {
Expand Down Expand Up @@ -44,6 +45,154 @@ LocalFunction.prototype._requestCallBack = function(err, response, body) {
return console.log('\nRESPONSE RECEIVED FROM FUNCTION: ' + status + body);
};

LocalFunction.prototype.encodeFirestoreValue = function(data) {

var isPlainObject= function(input) {
return (
typeof input === 'object' &&
input !== null &&
Object.getPrototypeOf(input) === Object.prototype
);
};

var encodeHelper = function(val) {

if (is.string(val)) {
return {
stringValue: val
};
}

if (is.boolean(val)) {
return {
booleanValue: val
};
}

if (is.integer(val)) {
return {
integerValue: val
};
}

// Integers are handled above, the remaining numbers are treated as doubles
if (is.number(val)) {
return {
doubleValue: val
};
}

// if (is.date(val)) {
// let epochSeconds = Math.floor(val.getTime() / 1000);
// let timestamp = {
// seconds: epochSeconds,
// nanos: (val.getTime() - epochSeconds * 1000) * 1000000,
// };
// return {
// timestampValue: timestamp
// };
// }

// if (is.array(val)) {
// let encodedElements = [];
// for (let i = 0; i < val.length; ++i) {
// let enc = this.encodeValue(val[i]);
// if (enc) {
// encodedElements.push(enc);
// }
// }
// return {
// arrayValue: {
// values: encodedElements
// },
// };
// }

if (is.nil(val)) {
return {
nullValue: 'NULL_VALUE'
};
}

// if (is.instance(val, DocumentReference) || is.instance(val, ResourcePath)) {
// return {
// referenceValue: val.formattedName,
// };
// }

// if (is.instance(val, GeoPoint)) {
// return {
// valueType: 'geoPointValue',
// geoPointValue: val.toProto(),
// };
// }

if (is.instanceof(val, Buffer) || is.instanceof(val, Uint8Array)) {
return {
bytesValue: val,
};
}

// if (isPlainObject(val)) {
// console.log(val + 'is a plain object')
// return {
// mapValue: {
// fields: this.encodeFields(val)
// },
// };
// }

throw new Error(
'Cannot encode ' + val + 'to a Firestore Value'
);
};

return _.mapValues(data, encodeHelper);
};


// LocalFunction.prototype._convertToFirestoreWireFormat = function(fields) {
// if (!fields) {
// return {};
// }
// function convertHelper(data) {
// let result;
// _.forEach(data, (value, key) => {
// let dataPart;
// // var valueType =
// if (valueType === 'arrayValue') {
// let array = _.get(value, 'values', []);
// dataPart = {
// arrayValue: {
// values: _.map(array, (elem) => {
// return convertHelper(elem);
// }),
// },
// };
// } else if (valueType === 'mapValue') {
// let map = _.get(value, 'fields', {});
// dataPart = {
// mapValue: {
// fields: _.mapValues(map, (val) => {
// return convertHelper(val);
// }),
// },
// };
// } else if (valueType === 'timestampValue') {
// dataPart = {timestampValue: dateToTimestampProto(value)};
// } else {
// dataPart = data;
// }
// result = _.merge({}, dataPart, {valueType: valueType});
// });
// return result;
// }

// return _.mapValues(fields, (data) => {
// return convertHelper(data);
// });
// };

LocalFunction.prototype._call = function(data, opts) {
opts = opts || {};
if (this.httpsTrigger) {
Expand Down Expand Up @@ -71,14 +220,14 @@ LocalFunction.prototype._call = function(data, opts) {
opts.resource = this._substituteParams(this.eventTrigger.resource, opts.params);
this.controller.call(this.name, dataPayload, opts);
} else if (this._isFirestoreFunc(this.eventTrigger)){
console.log('its a firestore functions')
var dataPayload = {
exists: true, // TODO, update
data: function() {
return data;
value: {
"createTime": "2017-06-02T18:48:58.920638Z",
fields: this.encodeFirestoreValue(data),
"updateTime": "2017-06-16T04:50:48.293439Z"
}
}

this.controller.call(this.name, dataPayload, opts);
} else {
this.controller.call(this.name, data || {}, opts);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"glob": "^7.1.2",
"google-auto-auth": "^0.7.2",
"inquirer": "^0.12.0",
"is": "^3.2.1",
"jsonschema": "^1.0.2",
"jsonwebtoken": "^7.4.1",
"lodash": "^4.6.1",
Expand Down

0 comments on commit b5f10f9

Please sign in to comment.