Skip to content

Commit

Permalink
SNOW-1332640-structured-types-support - code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki committed Sep 30, 2024
1 parent 3e8e519 commit 9af44dc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
77 changes: 63 additions & 14 deletions lib/connection/result/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const dateTimeFormatConverter = require('./datetime_format_converter');
const bigInt = require('big-integer');
const moment = require('moment');
const momentTimezone = require('moment-timezone');
const util = require('../../util');

/**
* Creates a new Column.
Expand Down Expand Up @@ -296,7 +297,8 @@ function convertRawBoolean(rawColumnValue) {
/**
* Converts a raw column value of structured type object to javascript Object
*
* @param {String} rawColumnValue
* @param {Object} json
* @param {Object} context
*
* @returns {Object}
*/
Expand All @@ -317,6 +319,14 @@ function convertJsonObject(json, context) {
}
}

/**
* Converts a raw column value of structured type array to javascript Object
*
* @param {Object} json
* @param {Object} context
*
* @returns {Object}
*/
function convertJsonArray(json, context) {
let result = [];
if (context.fieldsMetadata) {
Expand All @@ -329,7 +339,14 @@ function convertJsonArray(json, context) {
return result;
}


/**
* Converts a raw column value of structured type map to javascript Object
*
* @param {Object} json
* @param {Object} context
*
* @returns {Object}
*/
function convertJsonMap(json, context) {
const result = new Map;
if (Array.isArray(context.fieldsMetadata) && context.fieldsMetadata.length === 2) {
Expand All @@ -346,37 +363,69 @@ function convertJsonMap(json, context) {
}

/**
* Converts a raw column value of structured type object to javascript Object
* Converts a raw column value of structured type OBJECT to javascript Object
*
* @param {String} parsedJsonValue
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object}
*/
function convertRawObject(rawColumnValue, column, context) {
const json = JSON.parse(rawColumnValue);
return convertJsonObject(json, context);
if (Util.string.isNotNullOrEmpty(rawColumnValue)) {
try {
const json = JSON.parse(rawColumnValue);
return convertJsonObject(json, context);
} catch (jsonParseError) {
Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message);
throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message));
}
} else {
throw new Error(util.format('Column %s raw value is null or empty ', column.name));
}
}
/**
* Converts a raw column value of structured type array to javascript Array
* Converts a raw column value of structured type ARRAY to javascript Array
*
* @param {String} parsedJsonValue
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object}
*/
function convertRawArray(rawColumnValue, column, context) {
const json = JSON.parse(rawColumnValue);
return convertJsonArray(json, context);
if (Util.string.isNotNullOrEmpty(rawColumnValue)) {
try {
const json = JSON.parse(rawColumnValue);
return convertJsonArray(json, context);
} catch (jsonParseError) {
Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message);
throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message));
}
} else {
throw new Error(util.format('Column %s raw value is null or empty ', column.name));
}
}
/**
* Converts a raw column value of structured type MAP to javascript Map
*
* @param {String} parsedJsonValue
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object}
*/
function convertRawMap(rawColumnValue, column, context) {
const json = JSON.parse(rawColumnValue);
return convertJsonMap(json, context);
if (Util.string.isNotNullOrEmpty(rawColumnValue)) {
try {
const json = JSON.parse(rawColumnValue);
return convertJsonMap(json, context);
} catch (jsonParseError) {
Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message);
throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message));
}
} else {
throw new Error(util.format('Column %s raw value is null or empty ', column.name));
}
}

function mapStructuredTypeValue(columnValue, context, metadataField) {
Expand Down
10 changes: 1 addition & 9 deletions lib/connection/result/datetime_format_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ function convertSnowflakeFormatToMomentFormat(formatSql, scale) {
} else {
// we found one of our special tags
if (out === '') {
if (tag === 'TZH') {
// format the moment to get the timezone string and extract the
// hours; for example, '-0700' will be converted to '-07'
out = moment.format('ZZ').substr(0, 3);
} else if (tag === 'TZM') {
// format the moment to get the timezone string and extract the
// minutes; for example, '-0700' will be converted to '00
out = moment.format('ZZ').substr(3);
} else if (tag === 'FF') {
if (tag === 'FF') {
// if 'FF' is followed by a digit, use the digit as the scale
let digit = null;
if (pos + tag.length < length) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/testStructuredType.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function normalize(source) {
}


describe.only('Test Structured types', function () {
describe('Test Structured types', function () {
let connection;

before(function (done) {
Expand Down

0 comments on commit 9af44dc

Please sign in to comment.