Skip to content

Commit

Permalink
Generate GetFormatFromOptions struct override (#3714)
Browse files Browse the repository at this point in the history
This PR adds support for generating the `GetFormatFromOptions` method
override for models that are structs.

Supports #3330.
  • Loading branch information
jorgerangel-msft authored Jul 1, 2024
1 parent 71ecc06 commit 72e26ac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ protected override MethodProvider[] BuildMethods()
{
methods.Add(BuildJsonModelWriteMethodObjectDeclaration());
methods.Add(BuildPersistableModelWriteMethodObjectDeclaration());
methods.Add(BuildPersistableModelGetFormatFromOptionsObjectDeclaration());
}

return [.. methods];
Expand Down Expand Up @@ -356,15 +357,32 @@ internal MethodProvider BuildPersistableModelCreateMethod()
internal MethodProvider BuildPersistableModelGetFormatFromOptionsMethod()
{
ValueExpression jsonWireFormat = SystemSnippet.JsonFormatSerialization;
// ModelReaderWriterFormat IPersistableModel<T>.GetFormatFromOptions(ModelReaderWriterOptions options)
// string IPersistableModel<T>.GetFormatFromOptions(ModelReaderWriterOptions options)
return new MethodProvider
(
new MethodSignature(nameof(IPersistableModel<object>.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, new[] { _serializationOptionsParameter }, ExplicitInterface: _persistableModelTInterface),
new MethodSignature(nameof(IPersistableModel<object>.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, [_serializationOptionsParameter], ExplicitInterface: _persistableModelTInterface),
jsonWireFormat,
this
);
}

/// <summary>
/// Builds the <see cref="IPersistableModel{object}"/> GetFormatFromOptions method for the model object.
/// </summary>
internal MethodProvider BuildPersistableModelGetFormatFromOptionsObjectDeclaration()
{
ValueExpression jsonWireFormat = SystemSnippet.JsonFormatSerialization;
var castToT = This.CastTo(_persistableModelTInterface);

// string IPersistableModel<object>.GetFormatFromOptions(ModelReaderWriterOptions options) => ((IPersistableModel<T>)this).GetFormatFromOptions(options);
return new MethodProvider
(
new MethodSignature(nameof(IPersistableModel<object>.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, [_serializationOptionsParameter], ExplicitInterface: _persistableModelObjectInterface),
castToT.Invoke(nameof(IPersistableModel<object>.GetFormatFromOptions), [_serializationOptionsParameter]),
this
);
}

/// <summary>
/// Builds the serialization constructor for the model.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void TestBuildJsonModelWriteCoreMethod()

// This test validates the json model serialization write method object declaration is built correctly
[Test]
public void BuildJsonModelWriteMethodObjectDeclaration()
public void TestBuildJsonModelWriteMethodObjectDeclaration()
{
var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty<InputModelProperty>(), null, new List<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, true);
var mockModelTypeProvider = new ModelProvider(inputModel);
Expand Down Expand Up @@ -217,7 +217,7 @@ public void TestBuildPersistableModelWriteCoreMethod()

// This test validates the PersistableModel serialization write method object declaration is built correctly
[Test]
public void BuildPersistableModelWriteMethodObjectDeclaration()
public void TestBuildPersistableModelWriteMethodObjectDeclaration()
{
var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty<InputModelProperty>(), null, new List<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, true);
var mockModelTypeProvider = new ModelProvider(inputModel);
Expand Down Expand Up @@ -271,7 +271,7 @@ public void TestBuildPersistableModelDeserializationMethod()
Assert.AreEqual(expectedReturnType, methodSignature?.ReturnType);
}

// This test validates the I model get format method is built correctly
// This test validates the persistable model get format method is built correctly
[Test]
public void TestBuildPersistableModelGetFormatMethod()
{
Expand All @@ -295,6 +295,41 @@ public void TestBuildPersistableModelGetFormatMethod()
Assert.IsNotNull(methodBody);
}

// This test validates the persistable model get format method object declaration is built correctly
[Test]
public void TestBuildPersistableModelGetFormatMethodObjectDeclaration()
{
var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty<InputModelProperty>(), null, new List<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, true);
var mockModelTypeProvider = new ModelProvider(inputModel);
var jsonMrwSerializationTypeProvider = new MrwSerializationTypeProvider(mockModelTypeProvider, inputModel);
var method = jsonMrwSerializationTypeProvider.BuildPersistableModelGetFormatFromOptionsObjectDeclaration();

Assert.IsNotNull(method);

var expectedInterface = new CSharpType(typeof(IPersistableModel<object>));
var methodSignature = method?.Signature as MethodSignature;
Assert.IsNotNull(methodSignature);
Assert.AreEqual("GetFormatFromOptions", methodSignature?.Name);
Assert.AreEqual(expectedInterface, methodSignature?.ExplicitInterface);
Assert.AreEqual(1, methodSignature?.Parameters.Count);
var expectedReturnType = new CSharpType(typeof(string));
Assert.AreEqual(expectedReturnType, methodSignature?.ReturnType);

// Check method modifiers
var expectedModifiers = MethodSignatureModifiers.None;
Assert.AreEqual(expectedModifiers, methodSignature?.Modifiers, "Method modifiers do not match the expected value.");


// Validate body
var methodBody = method?.BodyStatements;
Assert.IsNull(methodBody);
var bodyExpression = method?.BodyExpression as InvokeInstanceMethodExpression;
Assert.IsNotNull(bodyExpression);
Assert.AreEqual("GetFormatFromOptions", bodyExpression?.MethodName);
Assert.IsNotNull(bodyExpression?.InstanceReference);
Assert.AreEqual(1, bodyExpression?.Arguments.Count);
}

[Test]
public void TestBuildSerializationConstructor()
{
Expand Down

0 comments on commit 72e26ac

Please sign in to comment.