From 8cb65843a852867967eed21e25435282b48a7000 Mon Sep 17 00:00:00 2001 From: Anderson Silva Date: Thu, 3 Oct 2024 11:18:06 -0500 Subject: [PATCH] IsEmpty supports UO (#2672) Issue https://github.com/microsoft/Power-Fx/issues/2671. Still need to verify other functions. --- .../Texl/BuiltinFunctionsCore.cs | 5 +++-- .../Texl/Builtins/IsEmpty.cs | 15 +++++++++++++++ .../Functions/Library.cs | 11 +++++++++++ .../Functions/LibraryUntypedObject.cs | 13 +++++++++++++ .../ExpressionTestCases/IsEmpty.txt | 10 ++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs b/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs index a46e801686..240fd0d809 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs @@ -127,7 +127,8 @@ internal class BuiltinFunctionsCore public static readonly TexlFunction IsBlankOptionSetValue = _library.Add(new IsBlankOptionSetValueFunction()); public static readonly TexlFunction IsBlankOrError = _library.Add(new IsBlankOrErrorFunction()); public static readonly TexlFunction IsBlankOrErrorOptionSetValue = _library.Add(new IsBlankOrErrorOptionSetValueFunction()); - public static readonly TexlFunction IsEmpty = _library.Add(new IsEmptyFunction()); + public static readonly TexlFunction IsEmpty = _library.Add(new IsEmptyFunction()); + public static readonly TexlFunction IsEmpty_UO = _library.Add(new IsEmptyFunction_UO()); public static readonly TexlFunction IsError = _library.Add(new IsErrorFunction()); public static readonly TexlFunction IsNumeric = _library.Add(new IsNumericFunction()); public static readonly TexlFunction ISOWeekNum = _library.Add(new ISOWeekNumFunction()); @@ -253,7 +254,7 @@ internal class BuiltinFunctionsCore public static readonly TexlFunction Decimal = _featureGateFunctions.Add(new DecimalFunction()); public static readonly TexlFunction Decimal_UO = _featureGateFunctions.Add(new DecimalFunction_UO()); public static readonly TexlFunction Float = _featureGateFunctions.Add(new FloatFunction()); - public static readonly TexlFunction Float_UO = _featureGateFunctions.Add(new FloatFunction_UO()); + public static readonly TexlFunction Float_UO = _featureGateFunctions.Add(new FloatFunction_UO()); public static readonly TexlFunction IsUTCToday = _featureGateFunctions.Add(new IsUTCTodayFunction()); public static readonly TexlFunction UTCNow = _featureGateFunctions.Add(new UTCNowFunction()); public static readonly TexlFunction UTCToday = _featureGateFunctions.Add(new UTCTodayFunction()); diff --git a/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/IsEmpty.cs b/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/IsEmpty.cs index 6633acc9c5..3b6c9194ac 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/IsEmpty.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/IsEmpty.cs @@ -68,4 +68,19 @@ public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DTyp return fValid; } } + + internal sealed class IsEmptyFunction_UO : BuiltinFunction + { + public override bool IsSelfContained => true; + + public IsEmptyFunction_UO() + : base("IsEmpty", TexlStrings.AboutIsEmpty, FunctionCategories.Table | FunctionCategories.Information, DType.Boolean, 0, 1, 1, DType.UntypedObject) + { + } + + public override IEnumerable GetSignatures() + { + yield return new[] { TexlStrings.IsEmptyArg1 }; + } + } } diff --git a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs index 367e632f10..ab580f2dd0 100644 --- a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs +++ b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs @@ -982,6 +982,17 @@ static Library() returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult, targetFunction: IsEmpty) }, + { + BuiltinFunctionsCore.IsEmpty_UO, + StandardErrorHandling( + BuiltinFunctionsCore.IsEmpty_UO.Name, + expandArguments: NoArgExpansion, + replaceBlankValues: DoNotReplaceBlank, + checkRuntimeTypes: ExactValueTypeOrBlank, + checkRuntimeValues: UntypedObjectArrayChecker, + returnBehavior: ReturnBehavior.ReturnBlankIfAnyArgIsBlank, + targetFunction: IsEmpty_UO) + }, { BuiltinFunctionsCore.IsError, NoErrorHandling(IsError) diff --git a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs index 3c83962512..5cf252e46e 100644 --- a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs +++ b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs @@ -67,6 +67,19 @@ public static FormulaValue First_UO(IRContext irContext, UntypedObjectValue[] ar var result = element[0]; return new UntypedObjectValue(irContext, result); + } + + public static FormulaValue IsEmpty_UO(IRContext irContext, UntypedObjectValue[] args) + { + var element = args[0].Impl; + var len = element.GetArrayLength(); + + if (len == 0) + { + return FormulaValue.New(true); + } + + return FormulaValue.New(false); } public static FormulaValue Last_UO(IRContext irContext, UntypedObjectValue[] args) diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/IsEmpty.txt b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/IsEmpty.txt index 303594466f..f7ceb85dc6 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/IsEmpty.txt +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/IsEmpty.txt @@ -114,6 +114,16 @@ true ),a > 10)) true +// Untyped object +>> IsEmpty(ParseJSON("[{""a"":1}]")) +false + +>> IsEmpty(ParseJSON("[]")) +true + +>> IsEmpty(ParseJSON("1")) +Error({Kind:ErrorKind.InvalidArgument}) + // INVALID ARGUMENTS >> IsEmpty("") Errors: Error 8-10: Invalid argument type (Text). Expecting a Table value instead.