Skip to content

Commit d55d26c

Browse files
committed
Update
1 parent 59ccb74 commit d55d26c

File tree

2 files changed

+244
-243
lines changed

2 files changed

+244
-243
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/Import-TypeFunction.ps1

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,247 @@ function Get-ShortNounName
454454

455455
Write-Output $noun;
456456
}
457+
458+
459+
function Get-ParameterTypeShortName
460+
{
461+
param(
462+
[Parameter(Mandatory = $True)]
463+
$parameter_type_info,
464+
465+
[Parameter(Mandatory = $false)]
466+
$is_list_type = $false
467+
)
468+
469+
if (-not $is_list_type)
470+
{
471+
$param_type_full_name = $parameter_type_info.FullName;
472+
$param_type_full_name = $param_type_full_name.Replace('+', '.');
473+
474+
$param_type_short_name = $parameter_type_info.Name;
475+
$param_type_short_name = $param_type_short_name.Replace('+', '.');
476+
}
477+
else
478+
{
479+
$itemType = $parameter_type_info.GetGenericArguments()[0];
480+
$itemTypeShortName = $itemType.Name;
481+
$itemTypeFullName = $itemType.FullName;
482+
$itemTypeNormalizedShortName = Get-NormalizedTypeName $itemType;;
483+
484+
$param_type_full_name = "System.Collections.Generic.List<${itemTypeNormalizedShortName}>";
485+
$param_type_full_name = $param_type_full_name.Replace('+', '.');
486+
487+
$param_type_short_name = "${itemTypeShortName}List";
488+
$param_type_short_name = $param_type_short_name.Replace('+', '.');
489+
}
490+
491+
return $param_type_short_name;
492+
}
493+
494+
function Get-ParameterTypeFullName
495+
{
496+
param(
497+
[Parameter(Mandatory = $True)]
498+
$parameter_type_info,
499+
500+
[Parameter(Mandatory = $false)]
501+
$is_list_type = $false
502+
)
503+
504+
if (-not $is_list_type)
505+
{
506+
$param_type_full_name = $parameter_type_info.FullName;
507+
$param_type_full_name = $param_type_full_name.Replace('+', '.');
508+
}
509+
else
510+
{
511+
$itemType = $parameter_type_info.GetGenericArguments()[0];
512+
$itemTypeShortName = $itemType.Name;
513+
$itemTypeFullName = $itemType.FullName;
514+
$itemTypeNormalizedShortName = Get-NormalizedTypeName $itemType;
515+
516+
$param_type_full_name = "System.Collections.Generic.List<${itemTypeNormalizedShortName}>";
517+
$param_type_full_name = $param_type_full_name.Replace('+', '.');
518+
}
519+
520+
return $param_type_full_name;
521+
}
522+
523+
524+
# Sample: VirtualMachineCreateParameters
525+
function Is-ClientComplexType
526+
{
527+
param(
528+
[Parameter(Mandatory = $True)]
529+
$type_info
530+
)
531+
532+
return ($type_info.Namespace -like "${client_name_space}.Model?") -and (-not $type_info.IsEnum);
533+
}
534+
535+
# Sample: IList<ConfigurationSet>
536+
function Is-ListComplexType
537+
{
538+
param(
539+
[Parameter(Mandatory = $True)]
540+
$type_info
541+
)
542+
543+
if ($type_info.IsGenericType)
544+
{
545+
$args = $list_item_type = $type_info.GetGenericArguments();
546+
547+
if ($args.Count -eq 1)
548+
{
549+
$list_item_type = $type_info.GetGenericArguments()[0];
550+
551+
if (Is-ClientComplexType $list_item_type)
552+
{
553+
return $true;
554+
}
555+
}
556+
}
557+
558+
return $false;
559+
}
560+
561+
# Sample: IList<ConfigurationSet> => ConfigurationSet
562+
function Get-ListComplexItemType
563+
{
564+
param(
565+
[Parameter(Mandatory = $True)]
566+
$type_info
567+
)
568+
569+
if ($type_info.IsGenericType)
570+
{
571+
$args = $list_item_type = $type_info.GetGenericArguments();
572+
573+
if ($args.Count -eq 1)
574+
{
575+
$list_item_type = $type_info.GetGenericArguments()[0];
576+
577+
if (Is-ClientComplexType $list_item_type)
578+
{
579+
return $list_item_type;
580+
}
581+
}
582+
}
583+
584+
return $null;
585+
}
586+
587+
# Sample: VirtualMachines.Create(...) => VirtualMachineCreateParameters
588+
function Get-MethodComplexParameter
589+
{
590+
param(
591+
[Parameter(Mandatory = $True)]
592+
[System.Reflection.MethodInfo]$op_method_info,
593+
594+
[Parameter(Mandatory = $True)]
595+
[string]$client_name_space
596+
)
597+
598+
$method_param_list = $op_method_info.GetParameters();
599+
$paramsWithoutEnums = $method_param_list | where { -not $_.ParameterType.IsEnum };
600+
601+
# Assume that each operation method has only one complext parameter type
602+
$param_info = $paramsWithoutEnums | where { $_.ParameterType.Namespace -like "${client_name_space}.Model?" } | select -First 1;
603+
604+
return $param_info;
605+
}
606+
607+
# Sample: VirtualMachineCreateParameters => ConfigurationSet, VMImageInput, ...
608+
function Get-SubComplexParameterListFromType
609+
{
610+
param(
611+
[Parameter(Mandatory = $True)]
612+
$type_info,
613+
614+
[Parameter(Mandatory = $True)]
615+
[string]$client_name_space
616+
)
617+
618+
$subParamTypeList = @();
619+
620+
if (-not (Is-ClientComplexType $type_info))
621+
{
622+
return $subParamTypeList;
623+
}
624+
625+
$paramProps = $type_info.GetProperties();
626+
foreach ($pp in $paramProps)
627+
{
628+
$isClientType = $false;
629+
if (Is-ClientComplexType $pp.PropertyType)
630+
{
631+
$subParamTypeList += $pp.PropertyType;
632+
$isClientType = $true;
633+
}
634+
elseif (Is-ListComplexType $pp.PropertyType)
635+
{
636+
$subParamTypeList += $pp.PropertyType;
637+
$subParamTypeList += Get-ListComplexItemType $pp.PropertyType;
638+
$isClientType = $true;
639+
}
640+
641+
if ($isClientType)
642+
{
643+
$recursiveSubParamTypeList = Get-SubComplexParameterListFromType $pp.PropertyType $client_name_space;
644+
foreach ($rsType in $recursiveSubParamTypeList)
645+
{
646+
$subParamTypeList += $rsType;
647+
}
648+
}
649+
}
650+
651+
return $subParamTypeList;
652+
}
653+
654+
# Sample: VirtualMachineCreateParameters => ConfigurationSet, VMImageInput, ...
655+
function Get-SubComplexParameterList
656+
{
657+
param(
658+
[Parameter(Mandatory = $True)]
659+
[System.Reflection.ParameterInfo]$param_info,
660+
661+
[Parameter(Mandatory = $True)]
662+
[string]$client_name_space
663+
)
664+
665+
return Get-SubComplexParameterListFromType $param_info.ParameterType $client_name_space;
666+
}
667+
668+
# Get proper type name
669+
function Get-ProperTypeName
670+
{
671+
param([System.Type] $itemType)
672+
673+
if ($itemType.IsGenericType -and ($itemType.Name.StartsWith('IList') -or $itemType.Name.StartsWith('List')))
674+
{
675+
$typeStr = 'IList<' + $itemType.GenericTypeArguments[0].Name + '>';
676+
}
677+
elseif ($itemType.IsGenericType -and ($itemType.Name.StartsWith('IDictionary') -or $itemType.Name.StartsWith('Dictionary')))
678+
{
679+
$typeStr = 'IDictionary<' + $itemType.GenericTypeArguments[0].Name + ',' + $itemType.GenericTypeArguments[1].Name + '>';
680+
}
681+
elseif ($itemType.IsGenericType -and $itemType.Name.StartsWith('Nullable'))
682+
{
683+
$typeStr = $itemType.GenericTypeArguments[0].Name + '?';
684+
}
685+
else
686+
{
687+
$typeStr = $itemType.Name;
688+
}
689+
690+
$typeStr = $typeStr.Replace("System.String", "string");
691+
$typeStr = $typeStr.Replace("String", "string");
692+
$typeStr = $typeStr.Replace("System.Boolean", "bool");
693+
$typeStr = $typeStr.Replace("Boolean", "bool");
694+
$typeStr = $typeStr.Replace("System.UInt32", "uint");
695+
$typeStr = $typeStr.Replace("UInt32", "uint");
696+
$typeStr = $typeStr.Replace("System.Int32", "int");
697+
$typeStr = $typeStr.Replace("Int32", "int");
698+
699+
return $typeStr;
700+
}

0 commit comments

Comments
 (0)