Skip to content

Commit

Permalink
fix duration default value problem
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Jan 25, 2024
1 parent ac54e4a commit 8585eda
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TypespecParameter } from "../interfaces";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { transformDefaultValue } from "../utils/values";

export function generateParameter(parameter: TypespecParameter): string {
const definitions: string[] = [];
Expand All @@ -12,7 +11,7 @@ export function generateParameter(parameter: TypespecParameter): string {
decorators && definitions.push(decorators);
let defaultValue = "";
if (parameter.defaultValue) {
defaultValue = ` = ${transformDefaultValue(parameter.type, parameter.defaultValue)}`;
defaultValue = ` = ${parameter.defaultValue}`;
}
definitions.push(`"${parameter.name}"${parameter.isOptional ? "?" : ""}: ${parameter.type}${defaultValue}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function transformTspArmResource(schema: ArmResourceSchema): TspArmResour
}

const decorators = buildResourceDecorators(schema);
if(!getArmCommonTypeVersion() && schema.resourceMetadata.IsExtensionResource) {
if (!getArmCommonTypeVersion() && schema.resourceMetadata.IsExtensionResource) {
decorators.push({ name: "extensionResource" });
}

Expand Down Expand Up @@ -855,7 +855,7 @@ function buildKeyProperty(schema: ArmResourceSchema): TypespecObjectProperty {
{
name: "visibility",
arguments: ["read"],
}
},
);

// remove @path decorator for key parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,24 @@ export function transformObjectProperty(propertySchema: Property, codeModel: Cod
isOptional: propertySchema.required !== true,
type: visited.name,
decorators: getPropertyDecorators(propertySchema),
defaultValue: getDefaultValue(propertySchema.schema),
defaultValue: getDefaultValue(visited.name, propertySchema.schema),
};
}

const logger = getLogger("getDiscriminatorProperty");

logger.info(`Transforming property ${propertySchema.language.default.name} of type ${propertySchema.schema.type}`);

const type = getTypespecType(propertySchema.schema, codeModel);
return {
kind: "property",
doc,
name,
isOptional: propertySchema.required !== true,
type: getTypespecType(propertySchema.schema, codeModel),
type,
decorators: getPropertyDecorators(propertySchema),
fixMe: getFixme(propertySchema, codeModel),
defaultValue: getDefaultValue(propertySchema.schema),
defaultValue: getDefaultValue(type, propertySchema.schema),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function transformParameter(parameter: Parameter, codeModel: CodeModel):
location: transformParameterLocation(parameter),
decorators: getPropertyDecorators(parameter),
serializedName: parameter.language.default.serializedName ?? parameter.language.default.name,
defaultValue: getDefaultValue(parameter.schema),
defaultValue: getDefaultValue(visited.name, parameter.schema),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TypespecObjectProperty } from "../interfaces";
import { generateDecorators } from "./decorators";
import { generateDocs } from "./docs";
import { transformDefaultValue } from "./values";

export function getModelPropertiesDeclarations(properties: TypespecObjectProperty[]): string[] {
const definitions: string[] = [];
Expand All @@ -13,7 +12,7 @@ export function getModelPropertiesDeclarations(properties: TypespecObjectPropert
property.fixMe && property.fixMe.length && definitions.push(property.fixMe.join("\n"));
let defaultValue = "";
if (property.defaultValue) {
defaultValue = ` = ${transformDefaultValue(property.type, property.defaultValue)}`;
defaultValue = ` = ${property.defaultValue}`;
}
definitions.push(`"${property.name}"${getOptionalOperator(property)}: ${property.type}${defaultValue};`);
}
Expand Down
18 changes: 5 additions & 13 deletions packages/extensions/openapi-to-typespec/src/utils/values.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema } from "@autorest/codemodel";
import { isChoiceSchema } from "./schemas";
import { isChoiceSchema, isSealedChoiceSchema } from "./schemas";

export function transformValue(value: string | number | boolean) {
if (typeof value === "string") {
Expand All @@ -9,25 +9,17 @@ export function transformValue(value: string | number | boolean) {
return value;
}

export function transformDefaultValue(type: string, value: string | number | boolean) {
if (["string", "int32", "int64", "float32", "float64", "boolean"].includes(type)) {
return transformValue(value);
} else {
return `${type}.\`${value}\``;
}
}

export function getDefaultValue(schema: Schema) {
export function getDefaultValue(type: string, schema: Schema) {
if (schema.defaultValue === undefined) {
return undefined;
}
if (isChoiceSchema(schema)) {
if (isChoiceSchema(schema) || isSealedChoiceSchema(schema)) {
for (const choice of schema.choices) {
if (schema.defaultValue === choice.value.toString()) {
return choice.language.default.name;
return `${type}.\`${choice.language.default.name}\``;
}
}
} else {
return schema.defaultValue;
return transformValue(schema.defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4203,7 +4203,7 @@ model BatchRetrySettings {
/**
* Invocation timeout for a mini-batch, in ISO 8601 format.
*/
timeout?: duration = duration.PT30S;
timeout?: duration = "PT30S";
}

/**
Expand Down Expand Up @@ -5249,7 +5249,7 @@ model ProbeSettings {
/**
* The length of time between probes in ISO 8601 format.
*/
period?: duration = duration.PT10S;
period?: duration = "PT10S";

/**
* The number of successful probes before returning a healthy status.
Expand All @@ -5259,7 +5259,7 @@ model ProbeSettings {
/**
* The probe timeout in ISO 8601 format.
*/
timeout?: duration = duration.PT2S;
timeout?: duration = "PT2S";
}

/**
Expand All @@ -5275,13 +5275,13 @@ model OnlineRequestSettings {
* The maximum amount of time a request will stay in the queue in ISO 8601 format.
* Defaults to 500ms.
*/
maxQueueWait?: duration = duration.`PT0.5S`;
maxQueueWait?: duration = "PT0.5S";

/**
* The scoring timeout in ISO 8601 format.
* Defaults to 5000ms.
*/
requestTimeout?: duration = duration.PT5S;
requestTimeout?: duration = "PT5S";
}

/**
Expand Down Expand Up @@ -8988,7 +8988,7 @@ model TrainingSettings {
* During VotingEnsemble and StackEnsemble model generation, multiple fitted models from the previous child runs are downloaded.
* Configure this parameter with a higher value than 300 secs, if more time is needed.
*/
ensembleModelDownloadTimeout?: duration = duration.PT5M;
ensembleModelDownloadTimeout?: duration = "PT5M";

/**
* Stack ensemble settings for stack ensemble run.
Expand Down Expand Up @@ -9303,12 +9303,12 @@ model TableVerticalLimitSettings {
/**
* AutoML job timeout.
*/
timeout?: duration = duration.PT6H;
timeout?: duration = "PT6H";

/**
* Iteration timeout.
*/
trialTimeout?: duration = duration.PT30M;
trialTimeout?: duration = "PT30M";
}

model TableParameterSubspace {
Expand Down Expand Up @@ -11003,7 +11003,7 @@ model ImageLimitSettings {
/**
* AutoML job timeout.
*/
timeout?: duration = duration.P7D;
timeout?: duration = "P7D";
}

/**
Expand Down Expand Up @@ -11867,7 +11867,7 @@ model NlpVerticalLimitSettings {
/**
* AutoML job timeout.
*/
timeout?: duration = duration.P7D;
timeout?: duration = "P7D";

/**
* Timeout for individual HD trials.
Expand Down Expand Up @@ -12656,7 +12656,7 @@ model TargetUtilizationScaleSettings extends OnlineScaleSettings {
/**
* The polling interval in ISO 8691 format. Only supports duration with precision as low as Seconds.
*/
pollingInterval?: duration = duration.PT1S;
pollingInterval?: duration = "PT1S";

/**
* Target CPU usage for the autoscaler.
Expand Down

0 comments on commit 8585eda

Please sign in to comment.