Skip to content

Commit

Permalink
samples: add schema samples, typescript sample support (#1262)
Browse files Browse the repository at this point in the history
fixes: #1220

The remainder of this PR is about adding schema sample snippets, as well as some preliminary TypeScript snippet support. There are still some linting issues with the generated samples, and I wanted to talk about the method used here before committing to it, so it's still marked as a work in progress.
  • Loading branch information
feywind authored Jun 7, 2021
1 parent 7af6bc5 commit f9dc4c6
Show file tree
Hide file tree
Showing 39 changed files with 2,412 additions and 22 deletions.
69 changes: 69 additions & 0 deletions samples/createAvroSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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
* schemas 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'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>

// [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(schemaName, avscFile) {
const definition = fs.readFileSync(avscFile).toString();
const schema = await pubSubClient.createSchema(
schemaName,
SchemaTypes.Avro,
definition
);

const name = await schema.getName();
console.log(`Schema ${name} created.`);
}
// [END pubsub_create_avro_schema]

function main(
schemaName = 'YOUR_SCHEMA_NAME',
avscFile = 'path/to/an/avro/schema/file/(.avsc)/formatted/in/json'
) {
createAvroSchema(schemaName, avscFile).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions samples/createProtoSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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
* schemas 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'use strict';

// sample-metadata:
// title: Create a Proto based Schema
// description: Creates a new schema definition on a project, using Protos
// usage: node createProtoSchema.js <schema-name> <proto-filename>

// [START pubsub_create_proto_schema]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const schemaName = 'YOUR_SCHEMA_NAME';
// const protoFile = 'path/to/a/proto/schema/file/(.proto)/formatted/in/protcol/buffers';

// 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 createProtoSchema(schemaName, protoFile) {
const definition = fs.readFileSync(protoFile).toString();
const schema = await pubSubClient.createSchema(
schemaName,
SchemaTypes.ProtocolBuffer,
definition
);

const fullName = await schema.getName();
console.log(`Schema ${fullName} created.`);
}
// [END pubsub_create_proto_schema]

function main(
schemaName = 'YOUR_SCHEMA_NAME',
protoFile = 'path/to/a/proto/schema/file/(.proto)/formatted/in/protcol/buffers'
) {
createProtoSchema(schemaName, protoFile).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
75 changes: 75 additions & 0 deletions samples/createTopicWithSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'use strict';

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

// [START pubsub_create_topic_with_schema]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicName = 'YOUR_TOPIC_NAME';
// const schemaName = 'YOUR_SCHEMA_NAME';
// const encodingType = 'BINARY';

// 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 createTopicWithSchema(topicName, schemaName, encodingType) {
// Get the fully qualified schema name.
const schema = pubSubClient.schema(schemaName);
const fullName = await schema.getName();

// Creates a new topic with a schema. Note that you might also
// pass Encodings.Json or Encodings.Binary here.
await pubSubClient.createTopic({
name: topicName,
schemaSettings: {
schema: fullName,
encoding: encodingType,
},
});
console.log(`Topic ${topicName} created with schema ${fullName}.`);
}
// [END pubsub_create_topic_with_schema]

function main(
topicName = 'YOUR_TOPIC_NAME',
schemaName = 'YOUR_SCHEMA_NAME',
encodingType = 'BINARY'
) {
createTopicWithSchema(topicName, schemaName, encodingType).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
59 changes: 59 additions & 0 deletions samples/deleteSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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
* schemas 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'use strict';

// sample-metadata:
// title: Delete a previously created schema
// description: Deletes a schema which was previously created in the project.
// usage: node deleteSchema.js <schema-name>

// [START pubsub_delete_schema]
/**
* TODO(developer): Uncomment this variable before running the sample.
*/
// const schemaName = 'YOUR_SCHEMA_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 deleteSchema(schemaName) {
const schema = pubSubClient.schema(schemaName);
const name = await schema.getName();
await schema.delete();
console.log(`Schema ${name} deleted.`);
}
// [END pubsub_delete_schema]

function main(schemaName = 'YOUR_SCHEMA_NAME') {
deleteSchema(schemaName).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
59 changes: 59 additions & 0 deletions samples/getSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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
* schemas 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'use strict';

// sample-metadata:
// title: Get a previously created schema
// description: Gets information about a schema which was previously created in the project.
// usage: node getSchema.js <schema-name>

// [START pubsub_get_schema]
/**
* TODO(developer): Uncomment this variable before running the sample.
*/
// const schemaName = 'YOUR_SCHEMA_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 getSchema(schemaName) {
const schema = pubSubClient.schema(schemaName);
const info = await schema.get();
const fullName = await schema.getName();
console.log(`Schema ${fullName} info: ${JSON.stringify(info, null, 4)}.`);
}
// [END pubsub_get_schema]

function main(schemaName = 'YOUR_SCHEMA_NAME') {
getSchema(schemaName).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
55 changes: 55 additions & 0 deletions samples/listSchemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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
* schemas 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.
*/

// This is a generated sample. Please see typescript/README.md for more info.

'use strict';

// sample-metadata:
// title: List schemas on a project
// description: Gets a list of schemas which were previously created in the project.
// usage: node listSchemas.js

// [START pubsub_list_schemas]

// 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 listSchemas() {
for await (const s of pubSubClient.listSchemas()) {
console.log(await s.name);
}
console.log('Listed schemas.');
}
// [END pubsub_list_schemas]

function main() {
listSchemas().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main();
Loading

0 comments on commit f9dc4c6

Please sign in to comment.