-
Notifications
You must be signed in to change notification settings - Fork 75
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not return result.body directly ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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 */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
There was a problem hiding this comment.
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 ?