-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat-default-value-interpretation
- Loading branch information
Showing
8 changed files
with
230 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as protobuf from 'protobufjs'; | ||
import * as protos from '../../protos/protos'; | ||
import {normalizeDescriptor} from '../adapt/proto'; | ||
import * as extend from 'extend'; | ||
|
||
type IDescriptorProto = protos.google.protobuf.IDescriptorProto; | ||
type DescriptorProto = protos.google.protobuf.DescriptorProto; | ||
|
||
const DescriptorProto = protos.google.protobuf.DescriptorProto; | ||
const {Type} = protobuf; | ||
|
||
/** | ||
* Internal class used by the JSONWriter to convert JSON data to protobuf messages. | ||
* It can be configure to do some data conversion to match what BigQuery expects. | ||
* | ||
* @class | ||
* @memberof managedwriter | ||
*/ | ||
export class JSONEncoder { | ||
private _type: protobuf.Type = Type.fromJSON('root', { | ||
fields: {}, | ||
}); | ||
|
||
/** | ||
* Creates a new JSONEncoder instance. | ||
* | ||
* @param {Object} params - The parameters for the JSONEncoder. | ||
* @param {IDescriptorProto} params.protoDescriptor - The proto descriptor | ||
* for the JSON rows. | ||
*/ | ||
constructor(params: {protoDescriptor: IDescriptorProto}) { | ||
const {protoDescriptor} = params; | ||
this.setProtoDescriptor(protoDescriptor); | ||
} | ||
|
||
/** | ||
* Update the proto descriptor for the Encoder. | ||
* | ||
* @param {IDescriptorProto} protoDescriptor - The proto descriptor. | ||
*/ | ||
setProtoDescriptor(protoDescriptor: IDescriptorProto): void { | ||
const normalized = normalizeDescriptor( | ||
new DescriptorProto(protoDescriptor) | ||
); | ||
this._type = Type.fromDescriptor(normalized); | ||
} | ||
|
||
/** | ||
* Writes a JSONList that contains objects to be written to the BigQuery table by first converting | ||
* the JSON data to protobuf messages, then using Writer's appendRows() to write the data at current end | ||
* of stream. If there is a schema update, the current Writer is closed and reopened with the updated schema. | ||
* | ||
* @param {JSONList} rows - The list of JSON rows. | ||
* @returns {Uint8Array[]} The encoded rows. | ||
*/ | ||
encodeRows(rows: any[]): Uint8Array[] { | ||
const serializedRows = rows | ||
.map(r => { | ||
return this.convertRow(r); | ||
}) | ||
.map(r => { | ||
return this.encodeRow(r); | ||
}); | ||
return serializedRows; | ||
} | ||
|
||
private isPlainObject(value: any): boolean { | ||
return value && [undefined, Object].includes(value.constructor); | ||
} | ||
|
||
private encodeRow(row: any): Uint8Array { | ||
const msg = this._type.create(row); | ||
return this._type.encode(msg).finish(); | ||
} | ||
|
||
private convertRow(source: any): Object { | ||
const row = extend(true, {}, source); | ||
for (const key in row) { | ||
const value = row[key]; | ||
if (value === null) { | ||
continue; | ||
} | ||
if (value instanceof Date) { | ||
const pfield = this._type.fields[key]; | ||
if (!pfield) { | ||
continue; | ||
} | ||
switch (pfield.type) { | ||
case 'int32': // DATE | ||
// The value is the number of days since the Unix epoch (1970-01-01) | ||
row[key] = value.getTime() / (1000 * 60 * 60 * 24); | ||
break; | ||
case 'int64': // TIMESTAMP | ||
// The value is given in microseconds since the Unix epoch (1970-01-01) | ||
row[key] = value.getTime() * 1000; | ||
break; | ||
case 'string': // DATETIME | ||
row[key] = value.toJSON().replace(/^(.*)T(.*)Z$/, '$1 $2'); | ||
break; | ||
} | ||
continue; | ||
} | ||
if (Array.isArray(value)) { | ||
row[key] = value.map(v => { | ||
if (!this.isPlainObject(v)) { | ||
return v; | ||
} | ||
return this.convertRow(v); | ||
}); | ||
continue; | ||
} | ||
if (this.isPlainObject(value)) { | ||
row[key] = this.convertRow(value); | ||
continue; | ||
} | ||
} | ||
return row; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.