Skip to content

Commit

Permalink
fix: force import protobufjs/ext/descriptor package (#407)
Browse files Browse the repository at this point in the history
* fix: lint issue

* fix: protobuf import

* fix: patch protobuf module declaration

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix: add more comments around protobufjs fix

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
alvarowolfx and gcf-owl-bot[bot] authored Jan 22, 2024
1 parent 70ca2d4 commit a970824
Show file tree
Hide file tree
Showing 9 changed files with 1,349 additions and 193 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"dependencies": {
"google-gax": "^4.0.3"
},
"peerDependencies": {
"protobufjs": "^7.2.4"
},
"devDependencies": {
"@google-cloud/bigquery": "^7.0.0",
"@types/uuid": "^9.0.1",
Expand Down
282 changes: 253 additions & 29 deletions protos/protos.d.ts

Large diffs are not rendered by default.

1,085 changes: 965 additions & 120 deletions protos/protos.js

Large diffs are not rendered by default.

118 changes: 83 additions & 35 deletions protos/protos.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ import * as protos from '../protos/protos';
export {protos};
import * as adapt from './adapt';
export {adapt};

// Add extra protobufjs definitions.
// When importing protobufjs/ext/descriptor package, it monkey patches some methods
// that we use in this package. We need to manually declare some of those
// methods that we use to make the Typescript compiler happy.
// There are some open issues around that. After they are fixed, we can remove this:
// * https://github.com/protobufjs/protobuf.js/issues/1499
// * https://github.com/protobufjs/protobuf.js/issues/1149
import './protobuf';
5 changes: 2 additions & 3 deletions src/managedwriter/json_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {protobuf} from 'google-gax';
import * as protobuf from 'protobufjs';
import * as protos from '../../protos/protos';
import {PendingWrite} from './pending_write';
import {StreamConnection, RemoveListener} from './stream_connection';
Expand Down Expand Up @@ -90,8 +90,7 @@ export class JSONWriter {
const normalized = adapt.normalizeDescriptor(
new DescriptorProto(protoDescriptor)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._type = (Type as any).fromDescriptor(normalized);
this._type = Type.fromDescriptor(normalized);
this._writer.setProtoDescriptor(protoDescriptor);
}

Expand Down
27 changes: 27 additions & 0 deletions src/protobuf/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 'protobufjs';
import 'protobufjs/ext/descriptor';
import * as protos from '../../protos/protos';

type IDescriptorProto = protos.google.protobuf.IDescriptorProto;

declare module 'protobufjs' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Type {
let toDescriptor: (protoVersion: string) => IDescriptorProto;
let fromDescriptor: (descriptor: IDescriptorProto) => Type;
}
}
7 changes: 4 additions & 3 deletions system-test/managed_writer_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import * as gax from 'google-gax';
import {BigQuery, TableSchema} from '@google-cloud/bigquery';
import * as protos from '../protos/protos';
import * as bigquerywriter from '../src';
import {ClientOptions, protobuf} from 'google-gax';
import * as protobuf from 'protobufjs';
import {ClientOptions} from 'google-gax';
import * as customerRecordProtoJson from '../samples/customer_record.json';

const {managedwriter, adapt} = bigquerywriter;
Expand Down Expand Up @@ -655,8 +656,8 @@ describe('managedwriter.WriterClient', () => {
});

protoDescriptor.field = protoDescriptor.field?.slice(0, 1); // leave just first field
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const invalidProto = (Type as any).fromDescriptor(

const invalidProto = Type.fromDescriptor(
protoDescriptor
) as protobuf.Type;
const row = {
Expand Down
6 changes: 3 additions & 3 deletions test/adapt/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as assert from 'assert';
import {describe, it} from 'mocha';
import {protobuf} from 'google-gax';
import * as protobuf from 'protobufjs';
import * as adapt from '../../src/adapt';
import * as messagesJSON from '../../samples/testdata/messages.json';
import * as protos from '../../protos/protos';
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Adapt Protos', () => {
if (!protoDescriptor) {
throw Error('null proto descriptor set');
}
const TestProto = (Type as any).fromDescriptor(protoDescriptor);
const TestProto = Type.fromDescriptor(protoDescriptor);
const raw = {
foo: 'name',
bar: 42,
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Adapt Protos', () => {
},
],
});
const NestedProto = (Type as any).fromDescriptor(protoDescriptor);
const NestedProto = Type.fromDescriptor(protoDescriptor);
const raw = {
record_id: '12345',
recordDetails: [
Expand Down

0 comments on commit a970824

Please sign in to comment.