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

Refactor sample generator in mgmt #5107

Merged
merged 61 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
2ca81d9
move mgmt test classes into mgmt
ArcturusZhang Oct 10, 2024
2c3d949
some in progress changes
ArcturusZhang Oct 11, 2024
30fe506
wip, now the new provider could generate those methods
ArcturusZhang Oct 11, 2024
1553bc9
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 11, 2024
066cd2d
follow up
ArcturusZhang Oct 11, 2024
3a03f71
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 14, 2024
6f25ddb
a few progress on creating id for a resource
ArcturusZhang Oct 14, 2024
d0d5df2
a few update
ArcturusZhang Oct 14, 2024
d209e5a
add get resource statement
ArcturusZhang Oct 14, 2024
1ac3fe2
add implementation for normal operations
ArcturusZhang Oct 16, 2024
36685a3
implmeent some bodies
ArcturusZhang Oct 16, 2024
834618d
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 16, 2024
ad43b46
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 17, 2024
403e9ab
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 17, 2024
77d3ebf
some more progress
ArcturusZhang Oct 17, 2024
ce40019
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 21, 2024
64824ce
fix the scope issue
ArcturusZhang Oct 21, 2024
33bfeca
fix another scope path issue
ArcturusZhang Oct 21, 2024
f88e7c1
enable generation for collections
ArcturusZhang Oct 21, 2024
e0efd6e
implement part of the resource collection methods
ArcturusZhang Oct 21, 2024
fdeb1b0
fix inline parameter issue
ArcturusZhang Oct 21, 2024
ee9c3d8
some updates with issues
ArcturusZhang Oct 21, 2024
123c66c
fix the scope issue
ArcturusZhang Oct 22, 2024
12efc8e
fix isuses in mgmttestconfiguration
ArcturusZhang Oct 23, 2024
f1273f0
fix casing and empty lines
ArcturusZhang Oct 23, 2024
4576226
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 31, 2024
9cfffd1
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 31, 2024
48f7f2f
honor the skipped operation config
ArcturusZhang Oct 31, 2024
3894a9b
regen
ArcturusZhang Nov 4, 2024
2cf792d
regen
ArcturusZhang Nov 4, 2024
2931a53
implement the extension operation samples
ArcturusZhang Nov 4, 2024
010f4cf
implement the property bag part 1
ArcturusZhang Nov 5, 2024
8468e13
implement the property bag part 2
ArcturusZhang Nov 5, 2024
600b4d0
remove the configureawait part
ArcturusZhang Nov 5, 2024
d1d9e0d
fix the null value issue in code model converter
ArcturusZhang Nov 5, 2024
e65f290
fix a case in resourcemanager
ArcturusZhang Nov 5, 2024
f5c2441
fix some issues found in real rps
ArcturusZhang Nov 5, 2024
e17b225
find another case that has configureawait
ArcturusZhang Nov 5, 2024
7936159
remove useless code
ArcturusZhang Nov 5, 2024
7461fff
more removals
ArcturusZhang Nov 5, 2024
c032d67
adjust namespaces
ArcturusZhang Nov 5, 2024
441dde0
clean up
ArcturusZhang Nov 5, 2024
c323542
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 6, 2024
c5704f9
refactor
ArcturusZhang Nov 6, 2024
fbf2968
more refactor
ArcturusZhang Nov 6, 2024
bf49918
regen
ArcturusZhang Nov 6, 2024
ad7549e
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Nov 11, 2024
9d7e62c
fix some more issues
ArcturusZhang Nov 11, 2024
97762f3
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Nov 12, 2024
855d575
fix userassignedidentity
ArcturusZhang Nov 12, 2024
8180473
add some notes
ArcturusZhang Nov 12, 2024
5027003
fix the optional parameter issue and regen
ArcturusZhang Nov 13, 2024
f1210ae
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 13, 2024
9ce9211
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 15, 2024
75098c2
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 21, 2024
c36dc84
fix the armclient issue
ArcturusZhang Nov 21, 2024
642a3bd
fix some minor issues in code
ArcturusZhang Nov 21, 2024
0754dde
regen
ArcturusZhang Nov 21, 2024
0e8aac7
resolve comments
ArcturusZhang Nov 22, 2024
f861682
refine
ArcturusZhang Nov 22, 2024
1f13f5b
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 25, 2024
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
4 changes: 0 additions & 4 deletions src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(CodeModel codeModel)
var inputNamespace = new CodeModelConverter(codeModel, schemaUsageProvider).CreateNamespace();
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(inputNamespace, sourceInputModel));
await MgmtTarget.ExecuteAsync(project);
if (Configuration.MgmtTestConfiguration is not null && !Configuration.MgmtConfiguration.MgmtDebug.ReportOnly)
await MgmtTestTarget.ExecuteAsync(project, inputNamespace, sourceInputModel);
GenerateMgmtReport(project);
}
else
Expand Down Expand Up @@ -88,8 +86,6 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(InputNamespace rootNamesp
InputNamespaceTransformer.Transform(rootNamespace);
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(rootNamespace, sourceInputModel));
await MgmtTarget.ExecuteAsync(project);
if (Configuration.GenerateSampleProject)
await MgmtTestTarget.ExecuteAsync(project, rootNamespace, sourceInputModel);
}
else
{
Expand Down
40 changes: 38 additions & 2 deletions src/AutoRest.CSharp/Common/Input/CodeModelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,49 @@ private InputOperation CreateOperation(ServiceRequest serviceRequest, Operation
return result;
}

private static InputExampleValue HandleRawExampleValue(InputType inputType, object? rawValue)
{
if (rawValue == null)
{
return InputExampleValue.Null(inputType);
}
var type = rawValue.GetType();
if (type.IsGenericType)
{
// handle dictionary
if (type == typeof(Dictionary<object, object>))
{
var values = new Dictionary<string, InputExampleValue>();
foreach (var (key, value) in (Dictionary<object, object>)rawValue)
{
// key is always a string
// since we do not have a schema for the value type here, we use unknown
values.Add((string)key, HandleRawExampleValue(InputPrimitiveType.Unknown, value));
}
return new InputExampleObjectValue(inputType, values);
}
// handle list
if (type == typeof(List<object>))
{
var values = new List<InputExampleValue>();
foreach (var item in (List<object>)rawValue)
{
values.Add(HandleRawExampleValue(InputPrimitiveType.Unknown, item));
}
return new InputExampleListValue(inputType, values);
}
}

// the deserialization part of the test modeler output has a bug that all the primitive types are deserialized as strings, here we should convert them according to its real type
return new InputExampleRawValue(inputType, ConvertRawValue(inputType, rawValue));
}

private InputExampleValue CreateExampleValue(ExampleValue exampleValue)
{
var inputType = CreateType(exampleValue.Schema, exampleValue.Schema.Extensions?.Format, false);
if (exampleValue.RawValue != null)
{
// test modeler has a bug that all the primitive types are deserialized as strings, here we should convert them according to its real type
return new InputExampleRawValue(inputType, ConvertRawValue(inputType, exampleValue.RawValue));
return HandleRawExampleValue(inputType, exampleValue.RawValue);
}
if (exampleValue.Elements != null && exampleValue.Elements.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Output.Builders;
using AutoRest.CSharp.Output.Models.Serialization;
using AutoRest.CSharp.Output.Models.Types;
using AutoRest.CSharp.Output.Samples.Models;
using AutoRest.CSharp.Utilities;
using Azure;
using Azure.Core;
using Azure.ResourceManager.Models;
using Microsoft.CodeAnalysis.CSharp;
using static AutoRest.CSharp.Common.Output.Models.Snippets;

namespace AutoRest.CSharp.LowLevel.Extensions
namespace AutoRest.CSharp.Common.Output.Models.Samples
{
internal static partial class ExampleValueSnippets
{
Expand Down Expand Up @@ -188,13 +190,35 @@ private static ValueExpression GetExpressionForFrameworkType(Type frameworkType,
return Null.CastTo(frameworkType);
}

return frameworkType.IsValueType ? Default.CastTo(frameworkType) : Null.CastTo(frameworkType);
// TODO -- this might be an issue. We have so many shared common types in resoucemanager which replace the generated types in a library.
live1206 marked this conversation as resolved.
Show resolved Hide resolved
// This way is only temporary, we need a universal way to handle those replaced types.
if (frameworkType == typeof(ManagedServiceIdentityType))
{
if (exampleValue is InputExampleRawValue rawValue && rawValue.RawValue is string str)
{
return Literal(str);
}

return Default.CastTo(frameworkType);
}

if (frameworkType == typeof(UserAssignedIdentity))
{
return New.Instance(frameworkType);
}

return DefaultOf(frameworkType);
}

public static ValueExpression GetExpression(InputExampleParameterValue exampleParameterValue, SerializationFormat serializationFormat)
public static ValueExpression GetExpression(CSharpType type, InputExampleValue? value)
=> value != null ?
GetExpression(type, value, SerializationBuilder.GetDefaultSerializationFormat(type)) :
DefaultOf(type);

public static ValueExpression GetExpression(ExampleParameterValue exampleParameterValue, SerializationFormat serializationFormat)
{
if (exampleParameterValue.Value != null)
return GetExpression(exampleParameterValue.Type, exampleParameterValue.Value, serializationFormat);
return GetExpression(exampleParameterValue.Type, exampleParameterValue.Value, serializationFormat == SerializationFormat.Default ? SerializationBuilder.GetDefaultSerializationFormat(exampleParameterValue.Type) : serializationFormat);
else if (exampleParameterValue.Expression != null)
return exampleParameterValue.Expression;
else
Expand Down Expand Up @@ -373,12 +397,12 @@ private static ValueExpression GetExpressionForObjectType(ObjectType objectType,
if (valueDict.TryGetValue(property.InputModelProperty!.SerializedName, out var exampleValue))
{
properties.Remove(property);
argument = GetExpression(propertyType, exampleValue, property.SerializationFormat, includeCollectionInitialization: true);
argument = GetExpression(propertyType, exampleValue, GetSerializationFormat(property, propertyType), includeCollectionInitialization: true);
}
else
{
// if no match, we put default here
argument = propertyType.IsValueType && !propertyType.IsNullable ? Default : Null;
argument = DefaultOf(propertyType);
}
arguments.Add(argument);
}
Expand All @@ -390,13 +414,24 @@ private static ValueExpression GetExpressionForObjectType(ObjectType objectType,
foreach (var (property, exampleValue) in propertiesToWrite)
{
// we need to pass in the current type of this property to make sure its initialization is correct
var propertyExpression = GetExpression(property.Declaration.Type, exampleValue, property.SerializationFormat, includeCollectionInitialization: false);
var propertyExpression = GetExpression(property.Declaration.Type, exampleValue, GetSerializationFormat(property, property.Declaration.Type), includeCollectionInitialization: false);
initializerDict.Add(property.Declaration.Name, propertyExpression);
}
objectPropertyInitializer = new(initializerDict, false);
}

return new NewInstanceExpression(objectType.Type, arguments, objectPropertyInitializer);

static SerializationFormat GetSerializationFormat(ObjectTypeProperty property, CSharpType propertyType)
{
var serializationFormat = property.SerializationFormat; // this works for typespec input models
// TODO - wait for fix. For swagger input models, the serialization format is not properly added onto the property, therefore here we have to find the serialization format again
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
if (serializationFormat == SerializationFormat.Default)
{
serializationFormat = SerializationBuilder.GetDefaultSerializationFormat(propertyType);
}
return serializationFormat;
}
}

private static IReadOnlyDictionary<ObjectTypeProperty, InputExampleValue> GetPropertiesToWrite(IEnumerable<ObjectTypeProperty> properties, IReadOnlyDictionary<string, InputExampleValue> valueDict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions;
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions.Azure;
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions.System;
using AutoRest.CSharp.Common.Output.Expressions.Statements;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Generation.Types;
Expand Down
3 changes: 0 additions & 3 deletions src/AutoRest.CSharp/Common/Output/Expressions/Snippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Expressions;
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions;
Expand All @@ -14,7 +12,6 @@
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Generation.Writers;
using AutoRest.CSharp.Output.Models.Types;
using static AutoRest.CSharp.Common.Input.Configuration;

namespace AutoRest.CSharp.Common.Output.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

namespace AutoRest.CSharp.Common.Output.Expressions.Statements
{
internal record InvokeInstanceMethodStatement(ValueExpression? InstanceReference, string MethodName, IReadOnlyList<ValueExpression> Arguments, bool CallAsAsync) : MethodBodyStatement
internal record InvokeInstanceMethodStatement(ValueExpression? InstanceReference, string MethodName, IReadOnlyList<ValueExpression> Arguments, bool CallAsAsync, bool AddConfigureAwaitFalse = true) : MethodBodyStatement
{
public InvokeInstanceMethodStatement(ValueExpression? instance, string methodName) : this(instance, methodName, Array.Empty<ValueExpression>(), false) { }
public InvokeInstanceMethodStatement(ValueExpression? instance, string methodName, ValueExpression arg) : this(instance, methodName, new[] { arg }, false) { }
public InvokeInstanceMethodStatement(ValueExpression? instance, string methodName, ValueExpression arg1, ValueExpression arg2) : this(instance, methodName, new[] { arg1, arg2 }, false) { }
public InvokeInstanceMethodStatement(ValueExpression? instance, string methodName, ValueExpression arg1, ValueExpression arg2, ValueExpression arg3) : this(instance, methodName, new[] { arg1, arg2, arg3 }, false) { }

public InvokeInstanceMethodStatement(InvokeInstanceMethodExpression expression) : this(expression.InstanceReference, expression.MethodName, expression.Arguments, expression.CallAsAsync, expression.AddConfigureAwaitFalse) { }

public sealed override void Write(CodeWriter writer)
{
new InvokeInstanceMethodExpression(InstanceReference, MethodName, Arguments, null, CallAsAsync).Write(writer);
new InvokeInstanceMethodExpression(InstanceReference, MethodName, Arguments, null, CallAsAsync, AddConfigureAwaitFalse).Write(writer);
writer.LineRaw(";");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ internal record SingleLineCommentStatement(FormattableString Message) : MethodBo
{
public SingleLineCommentStatement(string message) : this(FormattableStringHelpers.FromString(message))
{ }

public override void Write(CodeWriter writer)
{
writer.Line($"// {Message}");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using AutoRest.CSharp.Common.Input.Examples;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Output.Models.Requests;
using AutoRest.CSharp.Output.Models.Shared;

namespace AutoRest.CSharp.MgmtTest.Models
namespace AutoRest.CSharp.Output.Samples.Models
live1206 marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// A <see cref="ExampleParameterValue"/> represents a value for a parameter, which could either be a <see cref="ExampleValue"/>, or a <see cref="FormattableString"/> as a literal
/// A <see cref="ExampleParameterValue"/> represents a value for a parameter, which could either be a <see cref="InputExampleValue"/>, or a <see cref="ValueExpression"/> as an expression
/// </summary>
/// <param name="Parameter"></param>
/// <param name="Value"></param>
/// <param name="RawValue"></param>
internal record ExampleParameterValue
{
public string Name { get; }
Expand All @@ -24,7 +20,7 @@ internal record ExampleParameterValue

public InputExampleValue? Value { get; }

public FormattableString? Expression { get; }
public ValueExpression? Expression { get; }

private ExampleParameterValue(string name, CSharpType type)
{
Expand All @@ -37,19 +33,19 @@ public ExampleParameterValue(Reference reference, InputExampleValue value) : thi
Value = value;
}

public ExampleParameterValue(Reference reference, FormattableString rawValue) : this(reference.Name, reference.Type)
public ExampleParameterValue(Reference reference, ValueExpression expression) : this(reference.Name, reference.Type)
{
Expression = rawValue;
Expression = expression;
}

public ExampleParameterValue(Parameter parameter, InputExampleValue value) : this(parameter.Name, parameter.Type)
{
Value = value;
}

public ExampleParameterValue(Parameter parameter, FormattableString rawValue) : this(parameter.Name, parameter.Type)
public ExampleParameterValue(Parameter parameter, ValueExpression expression) : this(parameter.Name, parameter.Type)
{
Expression = rawValue;
Expression = expression;
}
}
}
Loading