From 930ca694c5017eb410987510710246e49b84244e Mon Sep 17 00:00:00 2001 From: Anderson Silva Date: Mon, 19 Aug 2024 19:03:32 -0500 Subject: [PATCH] ColumnName to work with array untyped object (#2601) Reply to https://github.com/microsoft/Power-Fx/pull/2588. --- .../Functions/LibraryUntypedObject.cs | 2 +- .../PadUntypedObjectTests.cs | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs index 28dc9a4e20..771d35ef8a 100644 --- a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs +++ b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryUntypedObject.cs @@ -437,7 +437,7 @@ public static FormulaValue ColumnNames_UO(IRContext irContext, UntypedObjectValu { var impl = args[0].Impl; - if (impl.Type is ExternalType externalType && externalType.Kind == ExternalTypeKind.Object) + if (impl.Type is ExternalType externalType && (externalType.Kind == ExternalTypeKind.Object || externalType.Kind == ExternalTypeKind.ArrayAndObject)) { if (impl.TryGetPropertyNames(out var propertyNames)) { diff --git a/src/tests/Microsoft.PowerFx.Interpreter.Tests.Shared/PadUntypedObjectTests.cs b/src/tests/Microsoft.PowerFx.Interpreter.Tests.Shared/PadUntypedObjectTests.cs index 12ca12e450..353873c5a1 100644 --- a/src/tests/Microsoft.PowerFx.Interpreter.Tests.Shared/PadUntypedObjectTests.cs +++ b/src/tests/Microsoft.PowerFx.Interpreter.Tests.Shared/PadUntypedObjectTests.cs @@ -161,6 +161,23 @@ public void PadUntypedObjectMissingProperty() // PadUntypedObject2 overrides GetProperty. Returns error if property is missing. var result2 = engine.Eval(@"Index(padTable2, 1).Missing"); Assert.IsType(result2); + } + + [Fact] + public void PadUntypedObject2ColumnNamesTest() + { + var uo = new PadUntypedObject2(GetDataTable()); + var uov = new UntypedObjectValue(IRContext.NotInSource(FormulaType.UntypedObject), uo); + + PowerFxConfig config = new PowerFxConfig(Features.PowerFxV1); + RecalcEngine engine = new RecalcEngine(config); + + engine.Config.SymbolTable.EnableMutationFunctions(); + engine.UpdateVariable("padTable", uov, new SymbolProperties() { CanMutate = true, CanSetMutate = true }); + + var result = engine.Eval(@"ColumnNames(Index(padTable, 1))"); + + Assert.IsAssignableFrom(result); } private DataTable GetDataTable() @@ -570,7 +587,19 @@ public override bool TryGetProperty(string propertyName, out IUntypedObject resu } public override bool TryGetPropertyNames(out IEnumerable result) - { + { + if (DataTable != null) + { + result = DataTable.Columns.Cast().Select(c => c.ColumnName); + return true; + } + + if (DataRow != null) + { + result = DataRow.Table.Columns.Cast().Select(c => c.ColumnName); + return true; + } + result = null; return false; }