Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

feat: adapt package to convert TableSchema to ProtoDescriptor#326

Merged
alvarowolfx merged 18 commits intogoogleapis:mainfrom
alvarowolfx:proto-adapt-module
Mar 22, 2023
Merged

feat: adapt package to convert TableSchema to ProtoDescriptor#326
alvarowolfx merged 18 commits intogoogleapis:mainfrom
alvarowolfx:proto-adapt-module

Conversation

@alvarowolfx
Copy link
Copy Markdown
Contributor

@alvarowolfx alvarowolfx commented Feb 16, 2023

This sub package goal is to allow users to convert BigQuery Schema/Table Schema to protobuf Descriptors and then convert plain JSON objects to protobuf, making it easier to use with the Storage Write API and not depending on protofiles for sending data.

Example:

import {adapt} from '@google-cloud/bigquery-storage')';
const {Type} = require('protobufjs');
require('protobufjs/ext/descriptor');

const protoDescriptor =
adapt.convertStorageSchemaToProto2Descriptor(
  {
    fields: [
      {
        name: 'foo',
        type: TableFieldSchema.Type.STRING,
        mode: TableFieldSchema.Mode.NULLABLE,
      },
      {
        name: 'bar',
        type: TableFieldSchema.Type.DOUBLE,
        mode: TableFieldSchema.Mode.REQUIRED,
      },
      {
        name: 'baz',
        type: TableFieldSchema.Type.STRING,
        mode: TableFieldSchema.Mode.REPEATED,
      },
      {
        name: 'bat',
        type: TableFieldSchema.Type.BOOL,
        mode: TableFieldSchema.Mode.REPEATED,
      },
    ],
  },
  'Test'
);
const TestProto = Type.fromDescriptor(protoDescriptor);
const raw = {
  foo: 'name',
  bar: 42,
  baz: ['A', 'B'],
  bat: [true, false],
};
const serialized = TestProto.encode(raw).finish();
const decoded = TestProto.decode(serialized).toJSON();
});

@product-auto-label product-auto-label Bot added size: l Pull request size is large. api: bigquerystorage Issues related to the googleapis/nodejs-bigquery-storage API. labels Feb 16, 2023
@alvarowolfx alvarowolfx changed the title feat: initial iteration on converting TableSchema to ProtoDescriptor feat: adapt package to convert TableSchema to ProtoDescriptor Feb 17, 2023
@product-auto-label product-auto-label Bot added size: xl Pull request size is extra large. and removed size: l Pull request size is large. labels Feb 17, 2023
@alvarowolfx alvarowolfx marked this pull request as ready for review February 22, 2023 18:19
@alvarowolfx alvarowolfx requested review from a team, chalmerlowe, feywind and shollyman and removed request for chalmerlowe February 22, 2023 18:19
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented Feb 22, 2023

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Comment thread package.json Outdated
Comment thread src/adapt/proto.ts Outdated
Comment thread src/adapt/proto.ts Outdated
Copy link
Copy Markdown

@feywind feywind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, sorry for leaving so many comments :D but hopefully there's something useful in there. I don't see anything worth holding it back, but I'd definitely check out the behaviour on the circular package dependencies that Seth mentioned.

Comment thread package.json Outdated
Comment thread samples/append_rows_adapt_proto2.js Outdated
const root = Root.fromJSON(namespace);
const SampleData = root.lookupType('SampleData');

/**
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these go closer to the snippet open tag so it's more visible to users copying and pasting?

Comment thread samples/append_rows_adapt_proto2.js Outdated
}
// [END bigquerystorage_write_pending_complexschema]
appendRowsProto2();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user I kind of love the comprehensive nature of this sample, but I also worry that it's really long. I wonder if we want to have more than one for smaller aspects of the process. (Not really a comment on your stuff so much as thinking out loud in regards to snippet tags...)

Comment thread src/adapt/proto.ts Outdated
Comment thread src/adapt/proto.ts Outdated
if (field.typeName) {
type = field.typeName;
} else {
type = fieldTypeLabelMap[field.type].replace('TYPE_', '').toLowerCase();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was reminded the other day, in case it matters, that replace(string, string) will only replace the first occurrence. You'd need to use /TYPE_/g to get all of them. (not sure if that applies here)

Comment thread src/adapt/proto.ts Outdated
return useProto3
? bqModeToFieldLabelMapProto3[mode]
: bqModeToFieldLabelMapProto2[mode];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall comments, it might be good to go back and put in some jsdoc/tsdoc comments describing what the functions do. Or at least some basic non-doc comments. I'd also consider thinking about breaking this up into multiple files if you can, because it's sort of long.

Comment thread src/adapt/schema.ts Outdated
Comment thread test/adapt/proto.ts

const {Root} = protobuf;

describe('Adapt Protos', () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're interested, I recently discovered a package snap-shot-it that lets you just record the output of a test, and then it will compare against the saved one. Not super essential though.

@alvarowolfx alvarowolfx added the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 8, 2023
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 8, 2023
Comment thread samples/customer_record.json
Comment thread samples/customer_record.json
Comment thread samples/append_rows_table_to_proto2.js Outdated
Comment thread src/adapt/proto_mappings.ts
@generated-files-bot
Copy link
Copy Markdown

Warning: This pull request is touching the following templated files:

Comment thread samples/append_rows_proto2.js Outdated
@alvarowolfx alvarowolfx added the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 21, 2023
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 21, 2023
@alvarowolfx alvarowolfx added the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 22, 2023
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 22, 2023
@alvarowolfx alvarowolfx merged commit 2d189e9 into googleapis:main Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: bigquerystorage Issues related to the googleapis/nodejs-bigquery-storage API. size: xl Pull request size is extra large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants