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

support additional properties in RLC #2054

Merged
merged 12 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion packages/rlc-common/src/buildIndexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ function generateRLCIndex(file: SourceFile, model: RLCModel) {
hasMultiCollection(model) ||
hasSsvCollection(model) ||
hasPipeCollection(model) ||
hasTsvCollection(model) ||
hasTsvCollection(model) ||
hasCsvCollection(model)
) {
file.addExportDeclarations([
Expand Down
24 changes: 16 additions & 8 deletions packages/rlc-common/src/buildObjectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,32 @@ function getImmediateParentsNames(

const extendFrom: string[] = [];

// If an immediate parent is a DictionarySchema, that means that the object has been marked
// If an immediate parent is an empty DictionarySchema, that means that the object has been marked
// with additional properties. We need to add Record<string, unknown> to the extend list and
if (objectSchema.parents.immediate.find(isDictionarySchema)) {
if (
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
objectSchema.parents.immediate.find((im) => isDictionarySchema(im, {filterEmpty: true}))
) {
extendFrom.push("Record<string, unknown>");
}

// Get the rest of the parents excluding any DictionarySchemas
const parents = objectSchema.parents.immediate
.filter((p) => !isDictionarySchema(p))
.filter((p) => !isDictionarySchema(p, {filterEmpty: true}))
.map((parent) => {
const nameSuffix = schemaUsage.includes(SchemaContext.Output)
? "Output"
: "";
const name = `${normalizeName(
parent.name,
NameType.Interface,
true /** shouldGuard */
)}${nameSuffix}`;
const name = isDictionarySchema(parent)
? `${
(schemaUsage.includes(SchemaContext.Output)
? parent.outputTypeName
: parent.typeName) ?? parent.name
}`
: `${normalizeName(
parent.name,
NameType.Interface,
true /** shouldGuard */
)}${nameSuffix}`;

return isObjectSchema(parent) && isPolymorphicParent(parent)
? `${name}Parent`
Expand Down
13 changes: 11 additions & 2 deletions packages/rlc-common/src/helpers/schemaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

import { Schema } from "../interfaces.js";

export function isDictionarySchema(schema: Schema) {
export interface IsDictionaryOptions {
filterEmpty?: boolean;
}

export function isDictionarySchema(
schema: Schema,
options: IsDictionaryOptions = {}
) {
if (schema.type === "dictionary") {
return true;
if (!options.filterEmpty || (options.filterEmpty && !schema.typeName)) {
return true;
}
}
return false;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/typespec-ts/src/modular/emitModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ export function buildModelsOptions(
client: Client
) {
const modelOptionsFile = codeModel.project.createSourceFile(
`${codeModel.modularOptions.sourceRoot}/${client.subfolder}/models/options.ts`,
path.join(
codeModel.modularOptions.sourceRoot,
client.subfolder ?? "",
`models/options.ts`
),
undefined,
{
overwrite: true
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/modular/helpers/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getType(type: Type, format?: string): TypeMetadata {
case "boolean":
return { name: getNullableType(type.type, type) };
case "constant": {
let typeName: string = type.value ?? "undefined";
let typeName: string = type.value?.toString() ?? "undefined";
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
if (type.valueType?.type === "string") {
typeName = type.value ? `"${type.value}"` : "undefined";
}
Expand Down
8 changes: 6 additions & 2 deletions packages/typespec-ts/src/transform/transformSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ export function transformSchemas(

setModelMap(model, context);
const indexer = (model as Model).indexer;
if (indexer?.value && !program.stateMap(modelKey).get(indexer?.value)) {
setModelMap(indexer.value, context);
if (
indexer?.value &&
(!program.stateMap(modelKey).get(indexer?.value) ||
!program.stateMap(modelKey).get(indexer?.value)?.includes(context))
) {
getGeneratedModels(indexer.value, context);
}
for (const prop of model.properties) {
if (
Expand Down
Loading
Loading