зеркало из https://github.com/microsoft/Power-Fx.git
Handle Entity types in UDFs (#2427)
As we plan to support complex types from Datasource in UDFs, we want to handle recursively defined Entity types in UDFs. We do not need to check for restricted types in DataEntity and metadata types that have expandinfo.
This commit is contained in:
Родитель
10346aeb53
Коммит
c9c5a866f6
|
@ -142,7 +142,7 @@ namespace Microsoft.PowerFx.Core.Functions
|
|||
}
|
||||
else
|
||||
{
|
||||
binding.ErrorContainer.EnsureError(DocumentErrorSeverity.Severe, UdfBody, TexlStrings.ErrUDF_ReturnTypeDoesNotMatch);
|
||||
binding.ErrorContainer.EnsureError(DocumentErrorSeverity.Severe, UdfBody, TexlStrings.ErrUDF_ReturnTypeDoesNotMatch, ReturnType.GetKindString(), actualBodyReturnType.GetKindString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -310,7 +310,9 @@ namespace Microsoft.PowerFx.Core.Functions
|
|||
{
|
||||
Contracts.AssertValue(ft);
|
||||
|
||||
if (ft is AggregateType aggType)
|
||||
// Datasource types may contain fields that may expand to other datasource types or refernce themselves.
|
||||
// We can avoid calling this method on these types containing expand info.
|
||||
if (!ft._type.HasExpandInfo && ft is AggregateType aggType)
|
||||
{
|
||||
if (aggType.GetFieldTypes().Any(ct => IsRestrictedType(ct.Type)))
|
||||
{
|
||||
|
|
|
@ -4075,8 +4075,8 @@
|
|||
<comment>This error message shows up when an invalid type is used</comment>
|
||||
</data>
|
||||
<data name="ErrUDF_ReturnTypeDoesNotMatch" xml:space="preserve">
|
||||
<value>The stated function return type does not match the return type of the function body.</value>
|
||||
<comment>This error message shows up when expected return type does not match with actual return type</comment>
|
||||
<value>The stated function return type '{0}' does not match the return type of the function body '{1}'.</value>
|
||||
<comment>This error message shows up when expected return type does not match with actual return type. The arguments '{0}' and '{1}' will be replaced with data types. For example, "The stated function return type 'Number' does not match the return type of the function body 'Table'"</comment>
|
||||
</data>
|
||||
<data name="ErrUDF_MissingReturnType" xml:space="preserve">
|
||||
<value>Missing function return type, for example the ":Number" in "FindMonth( d:Text ):Number = Month( DateParse( d ));"</value>
|
||||
|
|
|
@ -30,9 +30,11 @@ namespace Microsoft.PowerFx.Core.Tests.Helpers
|
|||
entityMetadata = new DataEntityMetadata();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Getting Metadata isn't allowed for performance reasons only
|
||||
throw new GettingMetadataNotAllowedException();
|
||||
else
|
||||
{
|
||||
entityMetadata = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,6 +336,11 @@ namespace Microsoft.PowerFx.Core.Tests.Helpers
|
|||
DataSource = new TestDataSource("test", DType.EmptyTable);
|
||||
}
|
||||
|
||||
internal TestExpandInfo(DType tableType)
|
||||
{
|
||||
DataSource = new TestDataSource("test", tableType);
|
||||
}
|
||||
|
||||
public string Identity => "Some Identity";
|
||||
|
||||
public bool IsTable => true;
|
||||
|
|
|
@ -268,6 +268,11 @@ namespace Microsoft.PowerFx.Interpreter.Tests
|
|||
{
|
||||
}
|
||||
|
||||
internal TestEntityType(DType tableType)
|
||||
: base(DType.CreateExpandType(new TestExpandInfo(tableType)))
|
||||
{
|
||||
}
|
||||
|
||||
public override void Visit(ITypeVisitor vistor)
|
||||
{
|
||||
throw new NotImplementedException("TestEntityType.Visit");
|
||||
|
|
|
@ -9,6 +9,7 @@ using System.Text;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.PowerFx.Core;
|
||||
using Microsoft.PowerFx.Core.IR;
|
||||
using Microsoft.PowerFx.Core.Localization;
|
||||
using Microsoft.PowerFx.Core.Tests;
|
||||
using Microsoft.PowerFx.Core.Tests.Helpers;
|
||||
|
@ -1662,6 +1663,14 @@ namespace Microsoft.PowerFx.Tests
|
|||
"",
|
||||
false)]
|
||||
|
||||
// UDFs with Enitity Types should work in parameter and return types
|
||||
|
||||
[InlineData(
|
||||
"f():TestEntity = Entity; g(e: TestEntity):Number = 1;",
|
||||
"g(f())",
|
||||
true,
|
||||
1.0)]
|
||||
|
||||
public void UserDefinedTypeTest(string userDefinitions, string evalExpression, bool isValid, double expectedResult = 0)
|
||||
{
|
||||
var config = new PowerFxConfig();
|
||||
|
@ -1674,6 +1683,10 @@ namespace Microsoft.PowerFx.Tests
|
|||
|
||||
if (isValid)
|
||||
{
|
||||
var entityType = new Interpreter.Tests.DatabaseSimulationTests.TestEntityType(new Tests.BindingEngineTests.LazyRecursiveRecordType().ToTable()._type);
|
||||
var entityValue = new Interpreter.Tests.DatabaseSimulationTests.TestEntityValue(IRContext.NotInSource(entityType));
|
||||
recalcEngine._symbolTable.AddType(new DName("TestEntity"), entityType);
|
||||
recalcEngine._symbolValues.Add("Entity", entityValue);
|
||||
recalcEngine.AddUserDefinitions(userDefinitions, CultureInfo.InvariantCulture);
|
||||
Assert.Equal(expectedResult, recalcEngine.Eval(evalExpression, options: parserOptions).ToObject());
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче