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

Modular: Encapsulate serializers into functions #2613

Merged
merged 34 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f89ebba
Track meta tree contexts
joheredi Jun 20, 2024
525f56f
Generate and use model serializers
joheredi Jun 20, 2024
faae113
Address issue when RLC doesn't have a corresponding model
joheredi Jun 20, 2024
d597805
Support record
joheredi Jun 21, 2024
0d52a63
support discriminated unions
joheredi Jun 21, 2024
9d38be7
Support discriminator and additional properties
joheredi Jun 21, 2024
2ed817e
Fix empty serializers and missing imports
joheredi Jun 21, 2024
d9a7711
fix key issue
joheredi Jun 21, 2024
f010a87
fix nested additionalprops
joheredi Jun 22, 2024
56d3b4b
update tests
joheredi Jun 22, 2024
afc0fca
Add dictionary tests
joheredi Jun 22, 2024
9c25bc5
support optional
joheredi Jun 22, 2024
22e2722
Fix test
joheredi Jun 23, 2024
80b766b
Fix issue with direct assignment of serialized
joheredi Jun 23, 2024
44d7e2c
Fix duplicate models and unecessary serializers
joheredi Jun 24, 2024
fcbd139
Re generate Smoke test
joheredi Jun 24, 2024
34dd138
Update Unit Tests
joheredi Jun 24, 2024
63c4d55
Fix tests
joheredi Jun 24, 2024
44c1b74
Re generate tests
joheredi Jun 25, 2024
9a3eea8
regenerate smoke tests
joheredi Jun 25, 2024
3f3bcf4
skip no-longer reelevant tests
joheredi Jun 25, 2024
ef68d29
Update generated files
joheredi Jun 25, 2024
3c749a3
update loadtesting
joheredi Jun 25, 2024
53f645f
Address feedback
joheredi Jun 25, 2024
f789363
skip exporting serializers
joheredi Jun 25, 2024
e9b0f50
Handle polymorphic parents
joheredi Jun 26, 2024
5d2fcd8
re-generate smoke
joheredi Jun 26, 2024
cf6d586
Merge remote-tracking branch 'upstream/main' into serialize-short-term
joheredi Jun 26, 2024
103fcd8
Update integration tests
joheredi Jun 26, 2024
8d37a98
Update smoke tests
joheredi Jun 26, 2024
89e56a1
Update test
joheredi Jun 26, 2024
9e7d1c8
Merge remote-tracking branch 'upstream/main' into serialize-short-term
joheredi Jun 26, 2024
a98d09e
Update tests
joheredi Jun 26, 2024
797bfde
Fix merge conflicts with new nullability from TCGC and regenerate assets
joheredi Jun 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions packages/rlc-common/src/helpers/importsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export function initInternalImports(): Imports {
rlcClientDefinition: {
type: "rlcClientDefinition",
importsSet: new Set<string>()
},
serializerHelpers: {
type: "serializerHelpers",
importsSet: new Set<string>()
}
} as Imports;
}
Expand Down Expand Up @@ -155,6 +159,9 @@ export function addImportsToFiles(
const specifier =
internalSpecifierMap?.[importType.type] ?? importType.specifier!;
let hasModifier = false;
if (!specifier) {
return;
}
file
.getImportDeclarations()
.filter((importDeclaration) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/rlc-common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export type ImportType =
| "azureDevTool"
| "azureAbortController"
| "azureCoreLro"
| "azureCorePaging";
| "azureCorePaging"
/**Internal helper imports */
| "serializerHelpers";

export interface ImportMetadata {
type: ImportType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { getLongRunningPoller } from "../pollingHelpers.js";
import { PollerLike, OperationState } from "@azure/core-lro";
import {
dataProductPropertiesSerializer,
managedServiceIdentitySerializer,
dataProductUpdatePropertiesSerializer,
DataProduct,
DataProductUpdate,
AccountSas,
Expand Down Expand Up @@ -53,6 +56,7 @@ import {
operationOptionsToRequestParameters,
createRestError,
} from "@azure-rest/core-client";
import { serializeRecord } from "../../helpers/serializerHelpers.js";
import {
DataProductsCreateOptionalParams,
DataProductsGetOptionalParams,
Expand Down Expand Up @@ -90,76 +94,16 @@ export function _createSend(
.put({
...operationOptionsToRequestParameters(options),
body: {
tags: resource["tags"],
tags: !resource.tags
? resource.tags
: (serializeRecord(resource.tags as any) as any),
location: resource["location"],
properties: !resource.properties
? undefined
: {
publisher: resource.properties?.["publisher"],
product: resource.properties?.["product"],
majorVersion: resource.properties?.["majorVersion"],
owners: resource.properties?.["owners"],
redundancy: resource.properties?.["redundancy"],
purviewAccount: resource.properties?.["purviewAccount"],
purviewCollection: resource.properties?.["purviewCollection"],
privateLinksEnabled: resource.properties?.["privateLinksEnabled"],
publicNetworkAccess: resource.properties?.["publicNetworkAccess"],
customerManagedKeyEncryptionEnabled:
resource.properties?.["customerManagedKeyEncryptionEnabled"],
customerEncryptionKey: !resource.properties?.customerEncryptionKey
? undefined
: {
keyVaultUri:
resource.properties?.customerEncryptionKey?.[
"keyVaultUri"
],
keyName:
resource.properties?.customerEncryptionKey?.["keyName"],
keyVersion:
resource.properties?.customerEncryptionKey?.[
"keyVersion"
],
},
networkacls: !resource.properties?.networkacls
? undefined
: {
virtualNetworkRule: resource.properties?.networkacls?.[
"virtualNetworkRule"
].map((p) => ({
id: p["id"],
action: p["action"],
state: p["state"],
})),
ipRules: resource.properties?.networkacls?.["ipRules"].map(
(p) => ({ value: p["value"], action: p["action"] }),
),
allowedQueryIpRangeList:
resource.properties?.networkacls?.[
"allowedQueryIpRangeList"
],
defaultAction:
resource.properties?.networkacls?.["defaultAction"],
},
managedResourceGroupConfiguration: !resource.properties
?.managedResourceGroupConfiguration
? undefined
: {
name: resource.properties
?.managedResourceGroupConfiguration?.["name"],
location:
resource.properties?.managedResourceGroupConfiguration?.[
"location"
],
},
currentMinorVersion: resource.properties?.["currentMinorVersion"],
},
? resource.properties
: dataProductPropertiesSerializer(resource.properties),
identity: !resource.identity
? undefined
: {
type: resource.identity?.["type"],
userAssignedIdentities:
resource.identity?.["userAssignedIdentities"],
},
? resource.identity
: managedServiceIdentitySerializer(resource.identity),
},
});
}
Expand Down Expand Up @@ -518,24 +462,14 @@ export function _updateSend(
...operationOptionsToRequestParameters(options),
body: {
identity: !properties.identity
? undefined
: {
type: properties.identity?.["type"],
userAssignedIdentities:
properties.identity?.["userAssignedIdentities"],
},
tags: properties["tags"],
? properties.identity
: managedServiceIdentitySerializer(properties.identity),
tags: !properties.tags
? properties.tags
: (serializeRecord(properties.tags as any) as any),
properties: !properties.properties
? undefined
: {
owners: properties.properties?.["owners"],
purviewAccount: properties.properties?.["purviewAccount"],
purviewCollection: properties.properties?.["purviewCollection"],
privateLinksEnabled:
properties.properties?.["privateLinksEnabled"],
currentMinorVersion:
properties.properties?.["currentMinorVersion"],
},
? properties.properties
: dataProductUpdatePropertiesSerializer(properties.properties),
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { getLongRunningPoller } from "../pollingHelpers.js";
import { PollerLike, OperationState } from "@azure/core-lro";
import {
dataTypePropertiesSerializer,
dataTypeUpdatePropertiesSerializer,
DataType,
DataTypeUpdate,
ContainerSaS,
Expand Down Expand Up @@ -79,15 +81,8 @@ export function _createSend(
...operationOptionsToRequestParameters(options),
body: {
properties: !resource.properties
? undefined
: {
state: resource.properties?.["state"],
storageOutputRetention:
resource.properties?.["storageOutputRetention"],
databaseCacheRetention:
resource.properties?.["databaseCacheRetention"],
databaseRetention: resource.properties?.["databaseRetention"],
},
? resource.properties
: dataTypePropertiesSerializer(resource.properties),
},
});
}
Expand Down Expand Up @@ -274,15 +269,8 @@ export function _updateSend(
...operationOptionsToRequestParameters(options),
body: {
properties: !properties.properties
? undefined
: {
state: properties.properties?.["state"],
storageOutputRetention:
properties.properties?.["storageOutputRetention"],
databaseCacheRetention:
properties.properties?.["databaseCacheRetention"],
databaseRetention: properties.properties?.["databaseRetention"],
},
? properties.properties
: dataTypeUpdatePropertiesSerializer(properties.properties),
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function serializeRecord<
T extends string | number | boolean | Date | null,
R,
>(item: Record<string, T>): Record<string, R>;
export function serializeRecord<T, R>(
item: Record<string, T>,
serializer: (item: T) => R,
): Record<string, R>;
export function serializeRecord<T, R>(
item: Record<string, T>,
serializer?: (item: T) => R,
): Record<string, R> {
return Object.keys(item).reduce(
(acc, key) => {
if (isSupportedRecordType(item[key])) {
acc[key] = item[key] as any;
} else if (serializer) {
const value = item[key];
if (value !== undefined) {
acc[key] = serializer(value);
}
} else {
console.warn(`Don't know how to serialize ${item[key]}`);
acc[key] = item[key] as any;
}
return acc;
},
{} as Record<string, R>,
);
}

function isSupportedRecordType(t: any) {
return (
["number", "string", "boolean", "null"].includes(typeof t) ||
t instanceof Date
);
}
Loading
Loading