Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add schema samples, typescript sample support #1262

Merged
merged 59 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
7cac93f
feat: begin support for server-side schema objects
feywind Mar 1, 2021
e5cb28f
samples: first sample, for creating an avro schema
feywind Mar 1, 2021
385ed99
samples: update avro create sample with the most recent version
feywind Mar 1, 2021
1b69b24
docs: fix a small typo
feywind Mar 4, 2021
05f20d0
feat: more implementation for schema, and also pubsub unit tests
feywind Mar 30, 2021
3d0f447
cleanup: commit bot provided updates for protos and package.json
feywind Apr 1, 2021
e77fe41
tests: added tests for Schema, tweaked a few resulting issues
feywind Apr 7, 2021
ad9f124
fix: convert getSchema() to an async iterator and update test
feywind Apr 7, 2021
6915111
cleanup: remove unneeded(?) IAM client in Schema
feywind Apr 7, 2021
ae6f4bd
fix: tweak getSchemas to listSchemas, to match the wording in GAPIC
feywind Apr 7, 2021
a1e4dcf
docs: fix name of listSchemas() in jsdoc, and fix the type for gaxOpts
feywind Apr 7, 2021
c5359d2
docs: add missing jsdoc from PubSub for schema
feywind Apr 7, 2021
8ceaeef
fix: update callbackify exception list for renamed listSchemas
feywind Apr 8, 2021
482fc67
docs: update jsdoc for Schema class
feywind Apr 8, 2021
2d174b2
build: reverting the major from the OpenTelemetry change (it was alre…
feywind Apr 15, 2021
bd41455
fix: swap promisify() to opt-in in PubSub, to fix accidental promisif…
feywind Apr 16, 2021
2f77737
fix: fix some issues with PubSub promisified methods, and get the AVR…
feywind Apr 16, 2021
4c05ddd
docs: add jsdoc comment for PubSub.getClientConfig()
feywind Apr 16, 2021
c3829e5
chore: remove accidentally added files
feywind Apr 16, 2021
4d690b6
Merge remote-tracking branch 'remotes/origin/master' into schema-support
feywind May 7, 2021
e86ad31
feat: allow full topic metadata to be passed, when creating a topic
feywind May 7, 2021
5634b68
feat: add schema message metadata decoding
feywind May 7, 2021
d4d6687
samples: add most of the rest of the schema samples
feywind May 7, 2021
52d1813
samples: add the rest of the listen/publish samples
feywind May 10, 2021
19873d8
Merge remote-tracking branch 'remotes/origin/master' into schema-support
feywind May 10, 2021
22dd857
feat: allow for deferred name resolution for schema objects
feywind May 11, 2021
c3a945c
tests: update PubSub unit tests to take promisifySome() into account
feywind May 11, 2021
1ba4b7e
fix: metadata translation was incorrectly using the proto IDs
feywind May 11, 2021
d98d6c8
samples: update avro samples for latest library changes
feywind May 11, 2021
1a7670d
samples: finish cleaning up avro/protobuf samples for schema
feywind May 11, 2021
044aafa
Merge remote-tracking branch 'remotes/origin/master' into schema-support
feywind May 11, 2021
5e06816
tests: update system test for schema samples
feywind May 11, 2021
f6c531e
feat: add support for typescript based samples, and move the listenFo…
feywind May 12, 2021
d0892e7
samples: update listenForProtobufMessages and convert two more to Typ…
feywind May 12, 2021
0d31b3f
samples: move the rest of the schema samples to TypeScript
feywind May 12, 2021
f0b4264
samples: fix extra spaces in generated samples
feywind May 12, 2021
bebadfa
Merge remote-tracking branch 'remotes/origin/master' into schema-support
feywind May 12, 2021
461ed9f
chore: add missing header
feywind May 12, 2021
ac6b597
tests: disable currently not working (very minor) test
feywind May 12, 2021
afb928f
chore: merge from main after merging the first set of schema support
feywind May 17, 2021
4971bea
tests: fix dangling promise in schema close() test
feywind May 17, 2021
bffce57
feat: expose list/get options for full/basic on schema methods (corre…
feywind May 18, 2021
b8fab1b
chore: merge branch 'schema-listget' into schema-support
feywind May 18, 2021
6260f26
chore: merge remote-tracking branch 'remotes/origin/master' into sche…
feywind May 21, 2021
ca86cfa
Merge branch 'master' into schema-support
feywind May 31, 2021
071e016
samples: several updates to fix problems with the sample build and te…
feywind Jun 1, 2021
74066b4
samples: fix simplifier for node 12
feywind Jun 1, 2021
ec4bdc3
samples: add back a few compile steps that got removed
feywind Jun 1, 2021
3974e06
tests: update sample test for create avro schema
feywind Jun 1, 2021
bd36bd3
chore: make linter happy
feywind Jun 2, 2021
cc79803
samples: CR suggestions from anguillanneuf
feywind Jun 4, 2021
4a4b191
samples: also update js files
feywind Jun 4, 2021
5c99f35
fix: some typing missteps in the schema support, and...
feywind Jun 4, 2021
cbf874d
tests: get the rest of the schema sample system-tests working
feywind Jun 7, 2021
36a1fa2
tests: add tests for the simplifier script
feywind Jun 7, 2021
fd0342d
tests: close subscriptions to avoid long timeout
feywind Jun 7, 2021
043ecf8
chore: merge remote-tracking branch 'remotes/origin/master' into sche…
feywind Jun 7, 2021
816be44
tests: update tests after "fix: some typing..."
feywind Jun 7, 2021
598b84b
tests: system tests run as node 12, so we have to pull from the 'prom…
feywind Jun 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions samples/createAvroSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021 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
//
// http://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.

/**
* This application demonstrates how to perform basic operations on
* subscriptions with the Google Cloud Pub/Sub API.
*
* For more information, see the README.md under /pubsub and the documentation
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create an Avro based Schema
// description: Creates a new schema definition on a project, using Avro
// usage: node createAvroSchema.js <schema-name> <avsc-filename>

function main(
schemaName = 'YOUR_SCHEMA_NAME',
avscFile = 'path/to/an/avro/schema/file/(.avsc)/formatted/in/json'
) {
// [START pubsub_create_avro_schema]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const schemaName = 'YOUR_SCHEMA_NAME';
// const avscFile = 'path/to/an/avro/schema/file/(.avsc)/formatted/in/json';

// Imports the Google Cloud client library
const {PubSub, SchemaTypes} = require('@google-cloud/pubsub');

const fs = require('fs');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createAvroSchema() {
const definition = fs.readFileSync(avscFile).toString();
const schema = await pubSubClient.createSchema(
schemaName,
SchemaTypes.Avro,
definition
);
console.log(`Schema ${schema.name} created.`);
}

createAvroSchema();
// [END pubsub_create_avro_schema]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
56 changes: 56 additions & 0 deletions samples/createTopicWithSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019-2021 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.

/**
* This sample demonstrates how to perform basic operations on topics with
* the Google Cloud Pub/Sub API.
*
* For more information, see the README.md under /pubsub and the documentation
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create Topic With Schema
// description: Creates a new topic, with a schema definition.
// usage: node createTopicWithSchema.js <topic-name>

async function main(topicName = 'YOUR_TOPIC_NAME') {
// [START pubsub_create_topic]
feywind marked this conversation as resolved.
Show resolved Hide resolved
/**
* TODO(developer): Uncomment this variable before running the sample.
*/
// const topicName = 'YOUR_TOPIC_NAME';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createTopic() {
// Creates a new topic
await pubSubClient.createTopic(topicName);
console.log(`Topic ${topicName} created.`);
}

createTopic();
// [END pubsub_create_topic]
feywind marked this conversation as resolved.
Show resolved Hide resolved
}

main(...process.argv.slice(2)).catch(e => {
console.error(e);
process.exitCode = -1;
});
10 changes: 8 additions & 2 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
"node": ">=10"
},
"scripts": {
"test": "mocha system-test --timeout 600000"
"test": "mocha build/system-test --timeout 600000",
"pretest": "npm run compile",
"compile": "tsc -p . && cp *.js build/ && cp -p system-test/*.js build/system-test/",
"precompile": "gts clean"
},
"dependencies": {
"@google-cloud/pubsub": "^2.11.0",
"@opentelemetry/api": "^0.18.1",
"@opentelemetry/tracing": "^0.18.2"
"@opentelemetry/tracing": "^0.18.2",
"avro-js": "^1.10.1"
},
"devDependencies": {
"@types/chai": "^4.2.16",
"chai": "^4.2.0",
"gts": "^3.1.0",
"mocha": "^8.0.0",
"uuid": "^8.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion samples/synchronousPull.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function main(
}

if (ackIds.length !== 0) {
// Acknowledge all of the messages. You could also ackknowledge
// Acknowledge all of the messages. You could also acknowledge
// these individually, but this is more efficient.
const ackRequest = {
subscription: formattedSubscription,
Expand Down
1 change: 1 addition & 0 deletions samples/system-test/fixtures/avro-avsc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Pet","type":"record","fields":[{"name":"kind","type":{"name":"Kind","type":"enum","symbols":["CAT","DOG"]}},{"name":"name","type":"string"}]}
92 changes: 92 additions & 0 deletions samples/system-test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2021 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 {PubSub} from '@google-cloud/pubsub';
import {assert} from 'chai';
import {describe, it, before, after} from 'mocha';
import * as cp from 'child_process';
import * as uuid from 'uuid';

const execSync = (cmd: string) => cp.execSync(cmd, {encoding: 'utf-8'});

describe('schema', () => {
const projectId = process.env.GCLOUD_PROJECT;
const pubsub = new PubSub({projectId});
const runId = uuid.v4();
console.log(`Topics runId: ${runId}`);
const topicNameOne = `schema-top1-${runId}`;
const subscriptionNameOne = `schema-sub1-${runId}`;
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
const schemaNameOne = `schema1-${runId}`;

const commandFor = (action: string) => `node ${action}.js`;

before(async () => {
const [topic] = await pubsub.createTopic(topicNameOne);
console.log('topic', topic);
console.log(
'sub',
await pubsub.createSubscription(topic, subscriptionNameOne)
);
});

async function cleanSchemas() {
const schemas = [];
for await (const s of pubsub.listSchemas()) {
schemas.push(s.delete());
}
await Promise.all(schemas);
}

async function cleanTopics() {
const [topics] = await pubsub.getTopics();
await Promise.all(
topics.filter(x => x.name.endsWith(runId)).map(x => x.delete())
);
}

async function cleanSubs() {
const [subscriptions] = await pubsub.getSubscriptions();
await Promise.all(
subscriptions.filter(x => x.name.endsWith(runId)).map(x => x.delete())
);
}

after(async () => {
await cleanSubs();
await cleanTopics();
await cleanSchemas();
});

it('should create an avro schema', async () => {
const output = execSync(
`${commandFor(
'createAvroSchema'
)} ${schemaNameOne} system-test/fixtures/avro-avsc.json`
);
assert.include(output, schemaNameOne);
assert.include(output, 'created.');

let found = false;
for await (const s of pubsub.listSchemas()) {
if (s.name.endsWith(schemaNameOne)) {
found = true;
break;
}
}

await cleanSchemas();
assert.ok(found, 'created schema was not found');
});
});
16 changes: 16 additions & 0 deletions samples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build",
"resolveJsonModule": true,
"lib": [
"es2018",
"dom"
]
},
"include": [
"./*.ts",
"system-test/*.ts"
]
}
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ export {
Snapshot,
} from './snapshot';
export {Message} from './subscriber';
export {
Schema,
CreateSchemaResponse,
ISchema,
SchemaType,
SchemaTypes,
ICreateSchemaRequest,
SchemaEncoding,
SchemaView,
SchemaViews,
Encodings,
} from './schema';
export {
PushConfig,
SubscriptionMetadata,
Expand Down
Loading