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

Use reshape to simplify deserialization #2180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"@azure/core-paging": "^1.5.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
17,423 changes: 5,015 additions & 12,408 deletions packages/typespec-test/test/batch_modular/generated/typespec-ts/src/api/operations.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@azure/core-rest-pipeline": "^1.12.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
operationOptionsToRequestParameters,
createRestError,
} from "@azure-rest/core-client";
import { reshape } from "@azure/core-util";
import { CreateStreamingOptions, CreateOptions } from "../models/options.js";

export function _createStreamingSend(
Expand Down Expand Up @@ -44,19 +45,23 @@ export async function _createStreamingDeserialize(
throw createRestError(result);
}

return {
choices: result.body["choices"].map((p) => ({
index: p["index"],
delta: {
content: p.delta["content"],
role: p.delta["role"],
sessionState: p.delta["session_state"],
},
sessionState: p["session_state"],
context: p["context"],
finishReason: p["finish_reason"],
})),
};
let deserializedResponse: unknown = result.body;
deserializedResponse = reshape(
deserializedResponse,
"choices[].delta.session_state",
"sessionState"
);
deserializedResponse = reshape(
deserializedResponse,
"choices[].session_state",
Comment on lines -59 to +56
Copy link
Member

Choose a reason for hiding this comment

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

@xirzec If we could support jsonpath, I think we could somehow use $..session_state to be more simpler ?

"sessionState"
);
deserializedResponse = reshape(
deserializedResponse,
"choices[].finish_reason",
"finishReason"
);
return deserializedResponse as ChatCompletionChunk;
}

/** Creates a new streaming chat completion. */
Expand Down Expand Up @@ -94,15 +99,23 @@ export async function _createDeserialize(
throw createRestError(result);
}

return {
choices: result.body["choices"].map((p) => ({
index: p["index"],
message: p.message as any,
sessionState: p["session_state"],
context: p["context"],
finishReason: p["finish_reason"],
})),
};
let deserializedResponse: unknown = result.body;
deserializedResponse = reshape(
deserializedResponse,
"choices[].message.session_state",
"sessionState"
);
deserializedResponse = reshape(
deserializedResponse,
"choices[].session_state",
"sessionState"
);
deserializedResponse = reshape(
deserializedResponse,
"choices[].finish_reason",
"finishReason"
);
return deserializedResponse as ChatCompletion;
}

/** Creates a new chat completion. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"@azure/core-paging": "^1.5.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,8 @@ export async function _analyzeTextDeserialize(
throw createRestError(result);
}

return {
blocklistsMatchResults: !result.body["blocklistsMatchResults"]
? result.body["blocklistsMatchResults"]
: result.body["blocklistsMatchResults"].map((p) => ({
blocklistName: p["blocklistName"],
blockItemId: p["blockItemId"],
blockItemText: p["blockItemText"],
})),
analyzeResults: result.body["analyzeResults"].map((p) => ({
category: p["category"],
severity: p["severity"],
})),
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as AnalyzeTextResult;
Copy link
Member

Choose a reason for hiding this comment

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

why not return result.body directly ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup I'm pushing that change next.

}

/** A sync API for harmful content analysis for text. Currently, we support four categories: Hate, SelfHarm, Sexual, Violence. */
Expand Down Expand Up @@ -141,12 +130,8 @@ export async function _analyzeImageDeserialize(
throw createRestError(result);
}

return {
analyzeResults: result.body["analyzeResults"].map((p) => ({
category: p["category"],
severity: p["severity"],
})),
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as AnalyzeImageResult;
}

/** A sync API for harmful content analysis for image. Currently, we support four categories: Hate, SelfHarm, Sexual, Violence. */
Expand Down Expand Up @@ -178,10 +163,8 @@ export async function _getTextBlocklistDeserialize(
throw createRestError(result);
}

return {
blocklistName: result.body["blocklistName"],
description: result.body["description"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as TextBlocklist;
}

/** Returns text blocklist details. */
Expand Down Expand Up @@ -224,10 +207,8 @@ export async function _createOrUpdateTextBlocklistDeserialize(
throw createRestError(result);
}

return {
blocklistName: result.body["blocklistName"],
description: result.body["description"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as TextBlocklist;
}

/** Updates a text blocklist, if blocklistName does not exist, create a new blocklist. */
Expand Down Expand Up @@ -300,13 +281,8 @@ export async function _listTextBlocklistsDeserialize(
throw createRestError(result);
}

return {
value: result.body["value"].map((p) => ({
blocklistName: p["blocklistName"],
description: p["description"],
})),
nextLink: result.body["nextLink"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as PagedTextBlocklist;
}

/** Get all text blocklists details. */
Expand Down Expand Up @@ -355,15 +331,8 @@ export async function _addOrUpdateBlockItemsDeserialize(
throw createRestError(result);
}

return {
value: !result.body["value"]
? result.body["value"]
: result.body["value"].map((p) => ({
blockItemId: p["blockItemId"],
description: p["description"],
text: p["text"],
})),
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as AddOrUpdateBlockItemsResult;
}

/** Add or update blockItems to a text blocklist. You can add or update at most 100 BlockItems in one request. */
Expand Down Expand Up @@ -448,11 +417,8 @@ export async function _getTextBlocklistItemDeserialize(
throw createRestError(result);
}

return {
blockItemId: result.body["blockItemId"],
description: result.body["description"],
text: result.body["text"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as TextBlockItem;
}

/** Get blockItem By blockItemId from a text blocklist. */
Expand Down Expand Up @@ -499,14 +465,8 @@ export async function _listTextBlocklistItemsDeserialize(
throw createRestError(result);
}

return {
value: result.body["value"].map((p) => ({
blockItemId: p["blockItemId"],
description: p["description"],
text: p["text"],
})),
nextLink: result.body["nextLink"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as PagedTextBlockItem;
}

/** Get all blockItems in a text blocklist */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@azure/core-rest-pipeline": "^1.12.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
operationOptionsToRequestParameters,
createRestError,
} from "@azure-rest/core-client";
import { uint8ArrayToString, stringToUint8Array } from "@azure/core-util";
import { reshape, uint8ArrayToString } from "@azure/core-util";
import {
PublishCloudEventOptions,
PublishCloudEventsOptions,
Expand Down Expand Up @@ -196,30 +196,18 @@ export async function _receiveCloudEventsDeserialize(
throw createRestError(result);
}

return {
value: result.body["value"].map((p) => ({
brokerProperties: {
lockToken: p.brokerProperties["lockToken"],
deliveryCount: p.brokerProperties["deliveryCount"],
},
event: {
id: p.event["id"],
source: p.event["source"],
data: p.event["data"],
dataBase64:
typeof p.event["data_base64"] === "string"
? stringToUint8Array(p.event["data_base64"], "base64")
: p.event["data_base64"],
type: p.event["type"],
time:
p.event["time"] !== undefined ? new Date(p.event["time"]) : undefined,
specversion: p.event["specversion"],
dataschema: p.event["dataschema"],
datacontenttype: p.event["datacontenttype"],
subject: p.event["subject"],
},
})),
};
let deserializedResponse: unknown = result.body;
deserializedResponse = reshape(
deserializedResponse,
"value[].event.data_base64",
"dataBase64"
);
Comment on lines +199 to +204
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this is incorrect ? because we still need to convert it into uint8Array ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, still need to handle the conversion. I let it out temporarily b/c I wanted to double check if we want to keep the current approach or need to update

deserializedResponse = reshape(
deserializedResponse,
"value[].event.time",
(value) => new Date(value as string)
);
return deserializedResponse as ReceiveResult;
}

/** Receive Batch of Cloud Events from the Event Subscription. */
Expand Down Expand Up @@ -270,14 +258,8 @@ export async function _acknowledgeCloudEventsDeserialize(
throw createRestError(result);
}

return {
failedLockTokens: result.body["failedLockTokens"].map((p) => ({
lockToken: p["lockToken"],
errorCode: p["errorCode"],
errorDescription: p["errorDescription"],
})),
succeededLockTokens: result.body["succeededLockTokens"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as AcknowledgeResult;
}

/** Acknowledge batch of Cloud Events. The server responds with an HTTP 200 status code if at least one event is successfully acknowledged. The response body will include the set of successfully acknowledged lockTokens, along with other failed lockTokens with their corresponding error information. Successfully acknowledged events will no longer be available to any consumer. */
Expand Down Expand Up @@ -328,14 +310,8 @@ export async function _releaseCloudEventsDeserialize(
throw createRestError(result);
}

return {
failedLockTokens: result.body["failedLockTokens"].map((p) => ({
lockToken: p["lockToken"],
errorCode: p["errorCode"],
errorDescription: p["errorDescription"],
})),
succeededLockTokens: result.body["succeededLockTokens"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as ReleaseResult;
}

/** Release batch of Cloud Events. The server responds with an HTTP 200 status code if at least one event is successfully released. The response body will include the set of successfully released lockTokens, along with other failed lockTokens with their corresponding error information. */
Expand Down Expand Up @@ -386,14 +362,8 @@ export async function _rejectCloudEventsDeserialize(
throw createRestError(result);
}

return {
failedLockTokens: result.body["failedLockTokens"].map((p) => ({
lockToken: p["lockToken"],
errorCode: p["errorCode"],
errorDescription: p["errorDescription"],
})),
succeededLockTokens: result.body["succeededLockTokens"],
};
let deserializedResponse: unknown = result.body;
return deserializedResponse as RejectResult;
}

/** Reject batch of Cloud Events. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@azure/core-rest-pipeline": "^1.12.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"@azure/core-paging": "^1.5.0",
"@azure/core-lro": "^2.5.4",
"@azure/abort-controller": "^1.0.0",
"@azure/core-util": "^1.4.0"
"@azure/core-util": "~/microsoft/azure-sdk-for-js/sdk/core/core-util"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.31.1",
Expand Down
Loading
Loading