Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColumnName to work with array untyped object #2601

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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