UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

179 lines (152 loc) 3.28 kB
'use strict'; var xmlValueToODataJson = exports.xmlValueToODataJson = {}; var Type_Error = require('./../errors/typeError'); var dateTimeParser = require('./../../parsers/dateTimeParser'); /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Binary'] = function xmlEdmBinaryToODataJson(jsonValue) { /** * in: base64 encoded string * out: base64 encoded string */ return jsonValue; }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Boolean'] = function xmlEdmBooleanToODataJson(jsonValue) { /** * in: true / false or 1 / 0 * out: true / false */ if (jsonValue === 'true' || jsonValue === true || jsonValue === '1' || jsonValue === 1) { return true; } else { return false; } }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Byte'] = function xmlEdmByteToODataJson(jsonValue) { /** * in: "0" - "255" * out: 0 - 255 */ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.DateTime'] = function xmlEdmDateTimeToODataJson(jsonValue) { /** * in: "YYYY-[M]M-[D]DTHH:mm[:ss.zzzzzzz]" --> z = DIGIT * See https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC * --> Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]]) * out: /Date(<millis since 1970-01-01T00:00:00.0000000>)/ */ try { var result = dateTimeParser.parse(jsonValue); var month = result.month-1; var date = new Date(Date.UTC(result.year, month, result.day, result.hour, result.minute, result.second, result.millisecond)); return "/Date(" + date.getTime() + ")/"; } catch (err) { throw new Type_Error('Provided Edm.DateTime property is not valid'); } }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Decimal'] = function xmlEdmDecimalToDbJson(jsonValue) { return jsonValue; }; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Double'] = noop; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Guid'] = noop; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Single'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int16'] = function(jsonValue){ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int32'] = function(jsonValue){ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int64'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.String'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Byte'] = function xmlEdmDecimalToDbJson(jsonValue){ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.SByte'] = function(jsonValue){ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Time'] = noop; /** * * @param jsonValue */ function noop(jsonValue) { return jsonValue; }