Skip to content

Commit

Permalink
Tsp - Use enum for string literal and fixed a polymorphism related issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dolauli committed Dec 20, 2024
1 parent 213e8b4 commit dd82a6e
Show file tree
Hide file tree
Showing 41 changed files with 730 additions and 375 deletions.
23 changes: 18 additions & 5 deletions packages/typespec-powershell/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,9 @@ function getSchemaForModel(
if (!isDiscriminatorInChild) {
modelSchema.properties.push(property);
} else {
modelSchema.discriminatorValue = (<ConstantSchema>propSchema).value.value;
// If discriminator value is union variant, it will be constant type
// Otherwise, it will be sealed choice type
modelSchema.discriminatorValue = propSchema.type === 'constant' ? (<ConstantSchema>propSchema).value.value : (<SealedChoiceSchema>propSchema).choices[0].value.toString();
}
if (discriminator && propName === discriminator.propertyName) {
property.isDiscriminator = true;
Expand Down Expand Up @@ -960,25 +962,36 @@ function getSchemaForModel(
// modelSchema.properties = modelSchema.properties?.filter(p => p.language.default.name != name);
// modelSchema.properties.push(newPropSchema);
}

// Add discriminator property if it is not already present
if (discriminator && !modelSchema.discriminator) {
const discriminatorProperty = new Property(discriminator.propertyName, `Discriminator property for ${modelSchema.language.default.name}.`, new StringSchema("string", ""));
discriminatorProperty.isDiscriminator = true;
discriminatorProperty.required = true;
modelSchema.discriminator = new M4Discriminator(discriminatorProperty);
modelSchema.properties.push(discriminatorProperty);
}
return modelSchema;
}
// Map an typespec type to an OA schema. Returns undefined when the resulting
// OA schema is just a regular object schema.
function getSchemaForLiteral(type: Type): any {
// ToDo: by xiaogang, need to implement other kinds as String
if (type.kind) {
const schema = new ConstantSchema("", "");
let schema;
switch (type.kind) {
case "Number":
schema = new ConstantSchema("", "");
schema.valueType = new NumberSchema("Constant", "Constant number", SchemaType.Number, 64);
schema.value = new ConstantValue(type.value);
return schema;
case "String":
schema.valueType = new StringSchema("Constant", "Constant string");
schema.value = new ConstantValue(type.value);
// generate a sealed choice schema for string literals
schema = new SealedChoiceSchema("", "");
schema.choiceType = new StringSchema("Constant", "Constant string");
schema.choices = [new ChoiceValue(type.value, "", type.value)];
return schema;
case "Boolean":
schema = new ConstantSchema("", "");
schema.valueType = new BooleanSchema("Constant", "Constant boolean");
schema.value = new ConstantValue(type.value);
return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public partial interface IAdditionalUnattendContent :
Microsoft-Windows-Shell-Setup.",
SerializedName = @"componentName",
PossibleTypes = new [] { typeof(string) })]
[global::Microsoft.Azure.PowerShell.Cmdlets.ComputeFleet.PSArgumentCompleterAttribute("Microsoft-Windows-Shell-Setup")]
string ComponentName { get; set; }
/// <summary>
/// Specifies the XML formatted content that is added to the unattend.xml file for
Expand Down Expand Up @@ -107,6 +108,7 @@ the specified path and component. The XML must be less than 4KB and must
Description = @"The pass name. Currently, the only allowable value is OobeSystem.",
SerializedName = @"passName",
PossibleTypes = new [] { typeof(string) })]
[global::Microsoft.Azure.PowerShell.Cmdlets.ComputeFleet.PSArgumentCompleterAttribute("OobeSystem")]
string PassName { get; set; }
/// <summary>
/// Specifies the name of the setting to which the content applies. Possible values
Expand Down Expand Up @@ -136,6 +138,7 @@ internal partial interface IAdditionalUnattendContentInternal
/// The component name. Currently, the only allowable value is
/// Microsoft-Windows-Shell-Setup.
/// </summary>
[global::Microsoft.Azure.PowerShell.Cmdlets.ComputeFleet.PSArgumentCompleterAttribute("Microsoft-Windows-Shell-Setup")]
string ComponentName { get; set; }
/// <summary>
/// Specifies the XML formatted content that is added to the unattend.xml file for
Expand All @@ -144,6 +147,7 @@ internal partial interface IAdditionalUnattendContentInternal
/// </summary>
System.Security.SecureString Content { get; set; }
/// <summary>The pass name. Currently, the only allowable value is OobeSystem.</summary>
[global::Microsoft.Azure.PowerShell.Cmdlets.ComputeFleet.PSArgumentCompleterAttribute("OobeSystem")]
string PassName { get; set; }
/// <summary>
/// Specifies the name of the setting to which the content applies. Possible values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ internal AgentProfile(global::System.Collections.IDictionary content)
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePrediction = (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IResourcePredictions) content.GetValueForProperty("ResourcePrediction",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePrediction, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.ResourcePredictionsTypeConverter.ConvertFrom);
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("ResourcePredictionProfileKind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePredictionProfileKind = (string) content.GetValueForProperty("ResourcePredictionProfileKind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePredictionProfileKind, global::System.Convert.ToString);
Expand Down Expand Up @@ -111,6 +115,10 @@ internal AgentProfile(global::System.Management.Automation.PSObject content)
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePrediction = (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IResourcePredictions) content.GetValueForProperty("ResourcePrediction",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePrediction, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.ResourcePredictionsTypeConverter.ConvertFrom);
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("ResourcePredictionProfileKind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePredictionProfileKind = (string) content.GetValueForProperty("ResourcePredictionProfileKind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal)this).ResourcePredictionProfileKind, global::System.Convert.ToString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public partial class AgentProfile :
Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal
{

/// <summary>Backing field for <see cref="Kind" /> property.</summary>
private string _kind;

/// <summary>Discriminator property for AgentProfile.</summary>
[Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Origin(Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.PropertyOrigin.Owned)]
public string Kind { get => this._kind; set => this._kind = value; }

/// <summary>Internal Acessors for ResourcePredictionsProfile</summary>
Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IResourcePredictionsProfile Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfileInternal.ResourcePredictionsProfile { get => (this._resourcePredictionsProfile = this._resourcePredictionsProfile ?? new Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.ResourcePredictionsProfile()); set { {_resourcePredictionsProfile = value;} } }

Expand Down Expand Up @@ -42,6 +49,17 @@ public AgentProfile()
public partial interface IAgentProfile :
Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.IJsonSerializable
{
/// <summary>Discriminator property for AgentProfile.</summary>
[Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Info(
Required = true,
ReadOnly = false,
Read = true,
Create = true,
Update = true,
Description = @"Discriminator property for AgentProfile.",
SerializedName = @"kind",
PossibleTypes = new [] { typeof(string) })]
string Kind { get; set; }
/// <summary>Defines pool buffer/stand-by agents.</summary>
[Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Info(
Required = false,
Expand Down Expand Up @@ -71,6 +89,8 @@ public partial interface IAgentProfile :
internal partial interface IAgentProfileInternal

{
/// <summary>Discriminator property for AgentProfile.</summary>
string Kind { get; set; }
/// <summary>Defines pool buffer/stand-by agents.</summary>
Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IResourcePredictions ResourcePrediction { get; set; }
/// <summary>Determines how the stand-by scheme should be provided.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,39 @@ internal AgentProfile(Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Ru
}
{_resourcePredictionsProfile = If( json?.PropertyT<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonObject>("resourcePredictionsProfile"), out var __jsonResourcePredictionsProfile) ? Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.ResourcePredictionsProfile.FromJson(__jsonResourcePredictionsProfile) : _resourcePredictionsProfile;}
{_resourcePrediction = If( json?.PropertyT<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonObject>("resourcePredictions"), out var __jsonResourcePredictions) ? Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.ResourcePredictions.FromJson(__jsonResourcePredictions) : _resourcePrediction;}
{_kind = If( json?.PropertyT<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonString>("kind"), out var __jsonKind) ? (string)__jsonKind : (string)_kind;}
AfterFromJson(json);
}

/// <summary>
/// Deserializes a <see cref="Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode"/> into an instance of Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfile.
/// Note: the Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfile interface is polymorphic, and the
/// precise model class that will get deserialized is determined at runtime based on the payload.
/// </summary>
/// <param name="node">a <see cref="Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode" /> to deserialize from.</param>
/// <returns>
/// an instance of Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfile.
/// </returns>
public static Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAgentProfile FromJson(Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode node)
{
return node is Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonObject json ? new AgentProfile(json) : null;
if (!(node is Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonObject json))
{
return null;
}
// Polymorphic type -- select the appropriate constructor using the discriminator

switch ( json.StringProperty("kind") )
{
case "Stateless":
{
return new StatelessAgentProfile(json);
}
case "Stateful":
{
return new Stateful(json);
}
}
return new AgentProfile(json);
}

/// <summary>
Expand All @@ -101,6 +121,7 @@ public Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.Json
}
AddIf( null != this._resourcePredictionsProfile ? (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode) this._resourcePredictionsProfile.ToJson(null,serializationMode) : null, "resourcePredictionsProfile" ,container.Add );
AddIf( null != this._resourcePrediction ? (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode) this._resourcePrediction.ToJson(null,serializationMode) : null, "resourcePredictions" ,container.Add );
AddIf( null != (((object)this._kind)?.ToString()) ? (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonNode) new Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Runtime.Json.JsonString(this._kind.ToString()) : null, "kind" ,container.Add );
AfterToJson(ref container);
return container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ internal AzureDevOpsOrganizationProfile(global::System.Collections.IDictionary c
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfile = (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsPermissionProfile) content.GetValueForProperty("PermissionProfile",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfile, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.AzureDevOpsPermissionProfileTypeConverter.ConvertFrom);
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("Organization"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Organization = (System.Collections.Generic.List<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganization>) content.GetValueForProperty("Organization",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Organization, __y => TypeConverterExtensions.SelectToList<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganization>(__y, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.OrganizationTypeConverter.ConvertFrom));
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganizationProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganizationProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("PermissionProfileKind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfileKind = (string) content.GetValueForProperty("PermissionProfileKind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfileKind, global::System.Convert.ToString);
Expand Down Expand Up @@ -119,14 +119,14 @@ internal AzureDevOpsOrganizationProfile(global::System.Management.Automation.PSO
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfile = (Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsPermissionProfile) content.GetValueForProperty("PermissionProfile",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfile, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.AzureDevOpsPermissionProfileTypeConverter.ConvertFrom);
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("Organization"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Organization = (System.Collections.Generic.List<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganization>) content.GetValueForProperty("Organization",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).Organization, __y => TypeConverterExtensions.SelectToList<Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganization>(__y, Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.OrganizationTypeConverter.ConvertFrom));
}
if (content.Contains("Kind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganizationProfileInternal)this).Kind = (string) content.GetValueForProperty("Kind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IOrganizationProfileInternal)this).Kind, global::System.Convert.ToString);
}
if (content.Contains("PermissionProfileKind"))
{
((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfileKind = (string) content.GetValueForProperty("PermissionProfileKind",((Microsoft.Azure.PowerShell.Cmdlets.DevOpsInfrastructure.Models.IAzureDevOpsOrganizationProfileInternal)this).PermissionProfileKind, global::System.Convert.ToString);
Expand Down
Loading

0 comments on commit dd82a6e

Please sign in to comment.