Skip to content

Commit

Permalink
ColumnName to work with array untyped object (#2601)
Browse files Browse the repository at this point in the history
Reply to #2588.
  • Loading branch information
anderson-joyle authored Aug 20, 2024
1 parent 6559d64 commit 930ca69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorValue>(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<TableValue>(result);
}

private DataTable GetDataTable()
Expand Down Expand Up @@ -570,7 +587,19 @@ public override bool TryGetProperty(string propertyName, out IUntypedObject resu
}

public override bool TryGetPropertyNames(out IEnumerable<string> result)
{
{
if (DataTable != null)
{
result = DataTable.Columns.Cast<DataColumn>().Select(c => c.ColumnName);
return true;
}

if (DataRow != null)
{
result = DataRow.Table.Columns.Cast<DataColumn>().Select(c => c.ColumnName);
return true;
}

result = null;
return false;
}
Expand Down

0 comments on commit 930ca69

Please sign in to comment.