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

[Mgmt]Build enums from code model converter #4576

Draft
wants to merge 3 commits into
base: feature/v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(CodeModel codeModel)
}
else
{
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(codeModel, sourceInputModel, schemaUsageProvider));
CodeModelTransformer.TransformForMgmt(codeModel);
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(codeModel, sourceInputModel, schemaUsageProvider));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think before we go any further, we should remove this MgmtContext first to make everything explicit.
Also we should remove BuildContext.

await MgmtTarget.ExecuteAsync(project, codeModel, sourceInputModel, schemaUsageProvider);
if (Configuration.MgmtTestConfiguration is not null && !Configuration.MgmtConfiguration.MgmtDebug.ReportOnly)
await MgmtTestTarget.ExecuteAsync(project, codeModel, sourceInputModel, schemaUsageProvider);
Expand Down
3 changes: 3 additions & 0 deletions src/AutoRest.CSharp/Common/Input/CodeModelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ internal class CodeModelConverter
private readonly Dictionary<ObjectSchema, List<InputModelProperty>> _modelPropertiesCache;
private readonly Dictionary<ObjectSchema, List<InputModelType>> _derivedModelsCache;

// TO-REMOVE: A temporary solution
public Dictionary<Schema, InputEnumType> EnumsCache => _enumsCache;

public CodeModelConverter(CodeModel codeModel, SchemaUsageProvider schemaUsages)
{
_codeModel = codeModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ private T EnsureLibrary()
public BuildContext(CodeModel codeModel, SourceInputModel? sourceInputModel, SchemaUsageProvider schemaUsageProvider)
: base(codeModel, sourceInputModel, schemaUsageProvider)
{
// TO-REMOVE: A temporary solution
if (Configuration.AzureArm)
{
MgmtCodeModelConverter = new CodeModelConverter(codeModel, schemaUsageProvider);
MgmtCodeModelConverter.CreateNamespace();
}
}

public override TypeFactory TypeFactory => _typeFactory ??= new TypeFactory(Library, typeof(BinaryData));

// TO-REMOVE: A temporary solution
public CodeModelConverter? MgmtCodeModelConverter { get; private set; }
}
}
15 changes: 14 additions & 1 deletion src/AutoRest.CSharp/Mgmt/AutoRest/MgmtOutputLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public MgmtOutputLibrary()
// these dictionaries are initialized right now and they would not change later
RawRequestPathToOperationSets = CategorizeOperationGroups();
ResourceDataSchemaNameToOperationSets = DecorateOperationSets();
_schemaToInputEnumMap = new CodeModelConverter(MgmtContext.CodeModel, MgmtContext.Context.SchemaUsageProvider).CreateEnums();
_schemaToInputEnumMap = MgmtContext.Context.MgmtCodeModelConverter!.EnumsCache;

// others are populated later
OperationsToOperationGroups = new Lazy<IReadOnlyDictionary<Operation, OperationGroup>>(PopulateOperationsToOperationGroups);
Expand Down Expand Up @@ -813,6 +813,19 @@ public IEnumerable<Resource> FindResources(ResourceData resourceData)
return ArmResources.Where(resource => resource.ResourceData == resourceData);
}

// TO-DO: After we do below, we could remove _schemaToInputEnumMap, same as other exposed dictionaries introduced by ObjectSchema later
// Expected as pesuade code:
// private TypeProvider BuildModel(InputType inputType) => inputType switch
// {
// InputEnumType enumType => new EnumType(enumType, MgmtContext.Context),
// InputModelType modelType => switch (modelType.[some condition])
// {
// case [some condition]:
// new MgmtObjectType(modelType, MgmtContext.Context),
// case [some condition]:
// new MgmtReferenceType(modelType, MgmtContext.Context),
// };
// };
private TypeProvider BuildModel(Schema schema) => schema switch
{
SealedChoiceSchema or ChoiceSchema => new EnumType(_schemaToInputEnumMap[schema], schema, MgmtContext.Context),
Expand Down