-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathencoder.ts
More file actions
196 lines (183 loc) · 5.92 KB
/
encoder.ts
File metadata and controls
196 lines (183 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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 {
generatePlaceholderFieldName,
isProtoCompatible,
normalizeDescriptor,
} from '../adapt/proto';
import {JSONObject, JSONValue} from './json_writer';
import {PreciseDate} from '@google-cloud/precise-date';
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: JSONObject[]): Uint8Array[] {
const serializedRows = rows
.map(r => {
return this.convertRow(r, this._type);
})
.map(r => {
return this.encodeRow(r);
});
return serializedRows;
}
private isPlainObject(value: any): boolean {
return value && [undefined, Object].includes(value.constructor);
}
private encodeRow(row: JSONObject): Uint8Array {
const msg = this._type.create(row);
return this._type.encode(msg).finish();
}
private convertRow(source: JSONObject, ptype: protobuf.Type): JSONObject {
const row = structuredClone(source);
const keys = Object.keys(row).map(key => {
if (!isProtoCompatible(key)) {
const newFieldName = generatePlaceholderFieldName(key);
// swap original key with placeholder field name
row[newFieldName] = row[key];
delete row[key];
return newFieldName;
}
return key;
});
for (const key of keys) {
const value = row[key];
if (value === null) {
continue;
}
const encodedValue = this.encodeRowValue(value, key, ptype);
if (encodedValue === undefined) {
continue;
}
row[key] = encodedValue;
}
return row;
}
private encodeRowValue(
value: JSONValue,
key: string,
ptype: protobuf.Type,
): JSONValue | undefined {
const pfield = ptype.fields[key];
if (!pfield) {
return undefined;
}
if (value instanceof Date || value instanceof PreciseDate) {
return this.encodeDateValue(pfield.type, value);
}
// NUMERIC and BIGNUMERIC integer
if (typeof value === 'number' || typeof value === 'bigint') {
switch (pfield.type) {
case 'string':
return value.toString(10);
}
return undefined;
}
if (Array.isArray(value)) {
const subType = this.getSubType(key, ptype);
return value.map(v => {
if (this.isPlainObject(v)) {
return this.convertRow(v as JSONObject, subType);
}
const encodedValue = this.encodeRowValue(v, key, subType);
if (encodedValue === undefined) {
return v;
}
return encodedValue;
});
}
if (this.isPlainObject(value)) {
const subType = this.getSubType(key, ptype);
return this.convertRow(value as JSONObject, subType);
}
return undefined;
}
private encodeDateValue(
fieldType: string,
value: Date | PreciseDate,
): JSONValue | undefined {
switch (fieldType) {
case 'int32': // DATE
// The value is the number of days since the Unix epoch (1970-01-01)
return value.getTime() / (1000 * 60 * 60 * 24);
case 'int64': {
// TIMESTAMP
let microseconds = 0;
if (value instanceof PreciseDate) {
microseconds = value.getMicroseconds();
}
// The value is given in microseconds since the Unix epoch (1970-01-01)
return value.getTime() * 1000 + microseconds;
}
case 'string': // DATETIME
return value.toJSON().replace(/^(.*)T(.*)Z$/, '$1 $2');
}
return undefined;
}
private getSubType(key: string, ptype: protobuf.Type): protobuf.Type {
const pfield = ptype.fields[key];
if (!pfield) {
return ptype;
}
try {
const subType = ptype.lookupTypeOrEnum(pfield.type);
return subType;
} catch (err) {
return ptype;
}
}
}