Skip to content

Commit

Permalink
allow repeated fields for any type
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Oct 3, 2016
1 parent 6301b95 commit 91a9a79
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
70 changes: 38 additions & 32 deletions packages/bigquery/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Table.mergeSchemaWithRows_ = function(schema, rows) {
return row.f.map(function(field, index) {
var schemaField = schema.fields[index];
var value = field.v;
var convertedValue;

var fieldObject = {};

Expand All @@ -202,45 +203,50 @@ Table.mergeSchemaWithRows_ = function(schema, rows) {
return fieldObject;
}

switch (schemaField.type) {
case 'BOOLEAN': {
value = value === 'true';
break;
}
case 'FLOAT': {
if (!is.nil(value)) {
value = parseFloat(value);
}
break;
}
case 'INTEGER': {
if (!is.nil(value)) {
value = parseInt(value, 10);
}
break;
}
case 'RECORD': {
if (schemaField.mode === 'REPEATED') {
value = value.map(function(val) {
return Table.mergeSchemaWithRows_(schemaField, val.v).pop();
});
} else {
value = Table.mergeSchemaWithRows_(schemaField, value).pop();
}

break;
}
case 'TIMESTAMP': {
value = new Date(value * 1000);
break;
}
if (schemaField.mode === 'REPEATED') {
value = value.map(function(val) {
return convert(schemaField, val.v);
});
} else {
value = convert(schemaField, value);
}

fieldObject[schemaField.name] = value;
return fieldObject;
});
}

function convert(schemaField, value) {
switch (schemaField.type) {
case 'BOOLEAN': {
value = value === 'true';
break;
}
case 'FLOAT': {
if (!is.nil(value)) {
value = parseFloat(value);
}
break;
}
case 'INTEGER': {
if (!is.nil(value)) {
value = parseInt(value, 10);
}
break;
}
case 'RECORD': {
value = Table.mergeSchemaWithRows_(schemaField, value).pop();
break;
}
case 'TIMESTAMP': {
value = new Date(value * 1000);
break;
}
}

return value;
}

function flattenRows(rows) {
return rows.reduce(function(acc, row) {
var key = Object.keys(row)[0];
Expand Down
16 changes: 15 additions & 1 deletion packages/bigquery/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('BigQuery/Table', function() {
});

describe('mergeSchemaWithRows_', function() {
it('should merge the schema and flatten the rows', function() {
it.only('should merge the schema and flatten the rows', function() {
var now = new Date();

var rows = [
Expand All @@ -181,6 +181,13 @@ describe('BigQuery/Table', function() {
{ v: String(now.valueOf() / 1000) },
{ v: 'false' },
{ v: '5.222330009847' },
{
v: [
{
v: '10'
}
]
},
{ v: null },
{
v: [
Expand Down Expand Up @@ -209,6 +216,7 @@ describe('BigQuery/Table', function() {
dob: now,
has_claws: false,
hair_count: 5.222330009847,
arr: [10],
nullable: null,
objects: [
{
Expand All @@ -223,6 +231,12 @@ describe('BigQuery/Table', function() {

var schemaObject = extend(true, SCHEMA_OBJECT, {});

schemaObject.fields.push({
name: 'arr',
type: 'INTEGER',
mode: 'REPEATED'
});

schemaObject.fields.push({
name: 'nullable',
type: 'STRING',
Expand Down

0 comments on commit 91a9a79

Please sign in to comment.