Skip to content

Patch(<record1>, <record2>) should allow coercions from the second item to the first #3062

@CarlosFigueiraMSFT

Description

@CarlosFigueiraMSFT

The expression Patch({a:1,b:"hello"}, {a:3,b:4}) currently fails - the types of the fields on the subsequent records must match exactly the types of the fields in the initial records. We should be able to relax this restriction.

Some tests that could be added to the file at https://github.com/microsoft/Power-Fx/blob/main/src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs that would illustrate this scenario - and can be used to validate the fix.

[Theory]
[InlineData("Patch({a:1}, {b:2})", "![a:n, b:n]")]
[InlineData("Patch({a:1}, {b:\"test\"})", "![a:n, b:s]")]
[InlineData("Patch({a:1, b:2}, {b:\"3\", c:4})", "![a:n, b:s, c:n]")] // currently failing, would pass after this fix
[InlineData("Patch({x:true}, {y:Date(2020,1,1)})", "![x:b, y:D]")]
public void TestPatchRecordFunction(string script, string expectedType)
{
    Assert.True(DType.TryParse(expectedType, out var type), script);
    Assert.True(type.IsValid, script);

    TestSimpleBindingSuccess(script, type);
}

[Theory]
[InlineData("Patch(rec1, {b:2})", "![a:n, b:n]", "![a:n]")]
[InlineData("Patch(rec2, {c:\"new\"})", "![a:n, b:s, c:s]", "![a:n, b:s]")]
[InlineData("Patch(rec3, {b:3, c:4})", "![a:n, b:n, c:n]", "![a:n, b:n]")]
[InlineData("Patch(rec4, {y:Date(2020,1,1)})", "![x:b, y:D]", "![x:b]")]
[InlineData("Patch(rec5, {x:123})", "![x:$]", "![x:$]")] // currently failing, would pass after this fix
public void TestPatchRecordFunctionWithVariable(string script, string expectedType, string variableType)
{
    Assert.True(DType.TryParse(expectedType, out var type), script);
    Assert.True(type.IsValid, script);

    Assert.True(DType.TryParse(variableType, out var varType), script);
    Assert.True(varType.IsValid, script);

    var symbol = new SymbolTable();
    var variableName = script.Split('(')[1].Split(',')[0].Trim();
    symbol.AddVariable(variableName, FormulaType.Build(varType));

    TestSimpleBindingSuccess(script, type, symbol);
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions