Skip to content

Commit

Permalink
Feature/schema registry avro stable (Azure#21164)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure/schema-registry-avro

### Issues associated with this PR
Fixes Azure#20814

### Describe the problem that is addressed by this PR
`serializeMessageData` is too long and can be simplified to `serialize`. This change has been already discussed and approved in the feature branch and this PR just merges it to main in order to get ready for release next week.

### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen?
N/A

### Are there test cases added in this PR? _(If not, why?)_
N/A

### Provide a list of related PRs _(if any)_
Azure#20815

### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_
- [x] Added a changelog (if necessary)
  • Loading branch information
deyaaeldeen authored Apr 2, 2022
1 parent 69448ed commit 01e0ade
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 184 deletions.
3 changes: 3 additions & 0 deletions sdk/eventhub/event-hubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Features Added

### Breaking Changes
- `MessageWithMetadata` has been renamed to `MessageContent`.
- `MessageContent`'s `body` has been renamed to `data`.
- `MessageAdapter`'s `consumeMessage` and `produceMessage` have been renamed to `consume` and `produce`.

### Bugs Fixed

Expand Down
8 changes: 4 additions & 4 deletions sdk/eventhub/event-hubs/review/event-hubs.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ export const logger: AzureLogger;

// @public
export interface MessageAdapter<MessageT> {
consumeMessage: (message: MessageT) => MessageWithMetadata;
produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT;
consume: (message: MessageT) => MessageContent;
produce: (MessageContent: MessageContent) => MessageT;
}

// @public
export interface MessageWithMetadata {
body: Uint8Array;
export interface MessageContent {
contentType: string;
data: Uint8Array;
}

export { MessagingError }
Expand Down
14 changes: 7 additions & 7 deletions sdk/eventhub/event-hubs/src/eventDataAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { EventData } from "./eventData";
*
* @hidden
*/
export interface MessageWithMetadata {
export interface MessageContent {
/**
* The message's binary data
*/
body: Uint8Array;
data: Uint8Array;
/**
* The message's content type
*/
Expand All @@ -33,11 +33,11 @@ export interface MessageAdapter<MessageT> {
/**
* defines how to create a message from a payload and a content type
*/
produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT;
produce: (MessageContent: MessageContent) => MessageT;
/**
* defines how to access the payload and the content type of a message
*/
consumeMessage: (message: MessageT) => MessageWithMetadata;
consume: (message: MessageT) => MessageContent;
}

// This type should always be equivalent to Omit<Omit<EventData, "body">, "contentType">
Expand Down Expand Up @@ -79,14 +79,14 @@ export function createEventDataAdapter(
params: EventDataAdapterParameters = {}
): MessageAdapter<EventData> {
return {
produceMessage: ({ body, contentType }: MessageWithMetadata) => {
produce: ({ data: body, contentType }: MessageContent) => {
return {
...params,
body,
contentType,
};
},
consumeMessage: (message: EventData): MessageWithMetadata => {
consume: (message: EventData): MessageContent => {
const { body, contentType } = message;
if (body === undefined) {
throw new Error("Expected the body field to be defined");
Expand All @@ -99,7 +99,7 @@ export function createEventDataAdapter(
* If the raw response was parsed as JSON, we need to convert it to a Uint8Array,
* otherwise, leave the payload as is.
*/
body: typeof body === "object" ? Uint8Array.from(Object.values(body)) : body,
data: typeof body === "object" ? Uint8Array.from(Object.values(body)) : body,
contentType,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SerializeTest extends AvroSerializerTest<SerializePerfTestOptions>
}

async run(): Promise<void> {
await this.serializer.serializeMessageData(
await this.serializer.serialize(
{
name: "test",
favoriteNumbers: this.array,
Expand Down
5 changes: 5 additions & 0 deletions sdk/schemaregistry/schema-registry-avro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
- A new error type, `AvroError`, is added and is thrown in all error cases except for service calls.

### Breaking Changes
- The `encodeMessageData` method has been renamed to `serialize`.
- The `decodeMessageData` method has been renamed to `deserialize`.
- The `MessageWithMetadata` interface has been renamed to `MessageContent`.
- `MessageContent`'s `body` has been renamed to `data`.
- `MessageAdapter`'s `consumeMessage` and `produceMessage` have been renamed to `consume` and `produce`.

### Bugs Fixed

Expand Down
6 changes: 3 additions & 3 deletions sdk/schemaregistry/schema-registry-avro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ to get schema IDs from schema definition or vice versa. The provided API has int

By default, the serializer will create messages structured as follows:

- `body`: a byte array containing data in the Avro Binary Encoding. Note that it
- `data`: a byte array containing data in the Avro Binary Encoding. Note that it
is NOT Avro Object Container File. The latter includes the schema and creating
it defeats the purpose of using this serializer to move the schema out of the
message payload and into the schema registry.
Expand Down Expand Up @@ -99,10 +99,10 @@ const schema = JSON.stringify({
const value = { score: 42 };

// Serialize value to a message
const message = await serializer.serializeMessageData(value, schema);
const message = await serializer.serialize(value, schema);

// Deserialize a message to value
const deserializedValue = await serializer.deserializeMessageData(message);
const deserializedValue = await serializer.deserialize(message);
```

## Troubleshooting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export class AvroError extends Error {
}

// @public
export class AvroSerializer<MessageT = MessageWithMetadata> {
export class AvroSerializer<MessageT = MessageContent> {
constructor(client: SchemaRegistry, options?: AvroSerializerOptions<MessageT>);
deserializeMessageData(message: MessageT, options?: DeserializeMessageDataOptions): Promise<unknown>;
serializeMessageData(value: unknown, schema: string): Promise<MessageT>;
deserialize(message: MessageT, options?: DeserializeOptions): Promise<unknown>;
serialize(value: unknown, schema: string): Promise<MessageT>;
}

// @public
Expand All @@ -31,20 +31,20 @@ export interface AvroSerializerOptions<MessageT> {
}

// @public
export interface DeserializeMessageDataOptions {
export interface DeserializeOptions {
schema?: string;
}

// @public
export interface MessageAdapter<MessageT> {
consumeMessage: (message: MessageT) => MessageWithMetadata;
produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT;
consume: (message: MessageT) => MessageContent;
produce: (messageContent: MessageContent) => MessageT;
}

// @public
export interface MessageWithMetadata {
body: Uint8Array;
export interface MessageContent {
contentType: string;
data: Uint8Array;
}

// (No @packageDocumentation comment for this package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function main() {
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// You can also skip this step and let `serialize` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await client.registerSchema(schemaDescription);

Expand All @@ -70,12 +70,12 @@ export async function main() {

// serialize an object that matches the schema and put it in a message
const value: User = { firstName: "Jane", lastName: "Doe" };
const message = await serializer.serializeMessageData(value, schema);
const message = await serializer.serialize(value, schema);
console.log("Created message:");
console.log(JSON.stringify(message));

// deserialize the message back to an object
const deserializedObject = await serializer.deserializeMessageData(message);
const deserializedObject = await serializer.deserialize(message);
console.log("Deserialized object:");
console.log(JSON.stringify(deserializedObject as User));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function main() {
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// You can also skip this step and let `serialize` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await schemaRegistryClient.registerSchema(schemaDescription);

Expand All @@ -88,7 +88,7 @@ export async function main() {

// serialize an object that matches the schema
const value: User = { firstName: "Jane", lastName: "Doe" };
const message = await serializer.serializeMessageData(value, schema);
const message = await serializer.serialize(value, schema);
console.log("Created message:");
console.log(message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function main() {
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// You can also skip this step and let `serialize` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await schemaRegistryClient.registerSchema(schemaDescription);

Expand Down Expand Up @@ -104,7 +104,7 @@ export async function main() {
if (event.contentType !== undefined && event.body) {
const contentTypeParts = event.contentType.split("+");
if (contentTypeParts[0] === "avro/binary") {
const deserializedEvent = await serializer.deserializeMessageData(event);
const deserializedEvent = await serializer.deserialize(event);
console.log(`Deserialized message: '${JSON.stringify(deserializedEvent)}'`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function main() {
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// You can also skip this step and let `serialize` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await schemaRegistryClient.registerSchema(schemaDescription);

Expand All @@ -85,7 +85,7 @@ export async function main() {

// serialize an object that matches the schema
const value: User = { firstName: "Joe", lastName: "Doe" };
const message = await serializer.serializeMessageData(value, schema);
const message = await serializer.serialize(value, schema);
console.log("Created message:");
console.log(message);

Expand Down
Loading

0 comments on commit 01e0ade

Please sign in to comment.