Skip to content

Commit

Permalink
Dont anonymize Join nor default enums (#2847)
Browse files Browse the repository at this point in the history
This pull request includes changes to enhance the handling of enums in
the `StructuralPrint` class, update the list of known functions, and
improve test coverage.

Enhancements to `StructuralPrint`:

*
[`src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs`](diffhunk://#diff-4760f19aba839606fcde0849467658572c272afd030c115afae8263986234a6dR9):
Added logic to handle default enums in the `Visit` method for
`DottedNameNode`, and included the `Enums` namespace.
[[1]](diffhunk://#diff-4760f19aba839606fcde0849467658572c272afd030c115afae8263986234a6dR9)
[[2]](diffhunk://#diff-4760f19aba839606fcde0849467658572c272afd030c115afae8263986234a6dR126-R134)

Updates to known functions:

*
[`src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs`](diffhunk://#diff-5a3b8429b8a783e4a4f50cbc410c8c1d83eda1fc561760948b79fc8ce675a5baR20-R26):
Updated the `OtherKnownFunctions` list to include the `Join` function.

Improvements to test coverage:

*
[`src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs`](diffhunk://#diff-f6ab2187c7ce1b7dea3e0655fb70f738656098817671383d60e9233859294a34R8):
Added new test cases for the `Join` and `WeekNum` functions in
`TestStucturalPrintWithBinding`, and included the `Builtins` namespace.
[[1]](diffhunk://#diff-f6ab2187c7ce1b7dea3e0655fb70f738656098817671383d60e9233859294a34R8)
[[2]](diffhunk://#diff-f6ab2187c7ce1b7dea3e0655fb70f738656098817671383d60e9233859294a34R37-R46)
[[3]](diffhunk://#diff-f6ab2187c7ce1b7dea3e0655fb70f738656098817671383d60e9233859294a34R72-R101)
  • Loading branch information
anderson-joyle authored Feb 13, 2025
1 parent c5e5c9b commit de2fbd0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Parser;
using Microsoft.PowerFx.Core.Texl;
using Microsoft.PowerFx.Core.Types.Enums;
using Microsoft.PowerFx.Core.UtilityDataStructures;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Syntax;
Expand Down Expand Up @@ -121,7 +122,16 @@ public override LazyList<string> Visit(DottedNameNode node, Precedence parentPre
{
Contracts.AssertValue(node);

var separator = TexlParser.GetTokString(node.Token.Kind);
var separator = TexlParser.GetTokString(node.Token.Kind);
var nodeType = _binding?.GetType(node);

// If default enum, show it plain.
if (nodeType != null &&
nodeType.OptionSetInfo != null &&
EnumStoreBuilder.DefaultEnumSymbols.ContainsKey(nodeType.OptionSetInfo.EntityName.Value))
{
return LazyList<string>.Of(node.ToString());
}

var values = node.Left.Accept(this, Precedence.Primary);
values = values.With(separator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ internal class BuiltinFunctionsCore
{
// This is the list of Power Apps functions that aren't supported/implemeted in Power Fx
// Binder will recognize these functions names and return a "recognized but not yet supported function" message
// instead of the classic "unknown or unsupported function".
// instead of the classic "unknown or unsupported function".

// This list also contains functions that are added to the interpreter at runtime and dont have their definitions
// included into the BuiltinFunctionsLibrary library. Examples: Set, Join.
internal static readonly IReadOnlyCollection<string> OtherKnownFunctions = new HashSet<string>()
{
"Assert", "Back", "Choices", "ClearData", "Concurrent", "Confirm", "Copy", "DataSourceInfo", "Defaults", "Disable", "Distinct", "Download", "EditForm", "Enable", "Errors", "Exit",
"GroupBy", "HashTags", "IsMatch", "IsType", "JSON", "Launch", "LoadData", "Match", "MatchAll", "Navigate", "NewForm", "Notify", "PDF", "Param", "Pending", "Print", "ReadNFC",
"GroupBy", "HashTags", "IsMatch", "IsType", "Join", "JSON", "Launch", "LoadData", "Match", "MatchAll", "Navigate", "NewForm", "Notify", "PDF", "Param", "Pending", "Print", "ReadNFC",
"RecordInfo", "Relate", "RemoveAll", "RemoveIf", "RequestHide", "Reset", "ResetForm", "Revert", "SaveData", "ScanBarcode", "Select", "SetFocus",
"SetProperty", "ShowColumns", "State", "SubmitForm", "TraceValue", "Ungroup", "Unrelate", "Update", "UpdateContext", "UpdateIf", "User", "Validate", "ValidateRecord", "ViewForm",
"Collect", "Clear", "Patch", "Remove", "ClearCollect", "Set"
Expand Down
24 changes: 19 additions & 5 deletions src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.PowerFx.Core.Logging;
using Microsoft.PowerFx.Core.Tests;
using Microsoft.PowerFx.Core.Texl.Builtins;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
using Xunit;
Expand Down Expand Up @@ -40,7 +41,8 @@ public void TestStucturalPrint(string script, string expected)
flags: Flags.EnableExpressionChaining,
features: Features.PowerFxV1);

Assert.Equal(expected, StructuralPrint.Print(result.Root));
var actual = StructuralPrint.Print(result.Root);
Assert.Equal(expected, actual);

// Test same cases via CheckResult
var check = new CheckResult(new Engine(new PowerFxConfig()));
Expand All @@ -66,24 +68,36 @@ public void TestStucturalPrint(string script, string expected)
"ForAll([1,2,3], ThisRecord.Value * 2)",
"ForAll([ #$decimal$#, #$decimal$#, #$decimal$# ], #$firstname$#.#$righthandid$# * #$decimal$#)",
"ForAll([ #$decimal$#, #$decimal$#, #$decimal$# ], #$LambdaFullRecord$#.#$righthandid$# * #$decimal$#)")]
[InlineData(
"Join(Table({a:1}), Table({a:1}), LeftRecord.a = RightRecord.a, JoinType.Inner, RightRecord.a As AAA)",
"Join(Table({ #$fieldname$#:#$decimal$# }), Table({ #$fieldname$#:#$decimal$# }), #$firstname$#.#$righthandid$# = #$firstname$#.#$righthandid$#, #$firstname$#.#$righthandid$#, #$firstname$#.#$righthandid$# As #$righthandid$#)",
"Join(Table({ #$fieldname$#:#$decimal$# }), Table({ #$fieldname$#:#$decimal$# }), #$LambdaField$#.#$righthandid$# = #$LambdaField$#.#$righthandid$#, JoinType.Inner, #$LambdaField$#.#$righthandid$# As #$righthandid$#)")]
[InlineData(
"WeekNum(Date(2020, 12, 8),StartOfWeek.Sunday)",
"WeekNum(Date(#$decimal$#, #$decimal$#, #$decimal$#), #$firstname$#.#$righthandid$#)",
"WeekNum(Date(#$decimal$#, #$decimal$#, #$decimal$#), StartOfWeek.Sunday)")]

public void TestStucturalPrintWithBinding(string script, string beforebinding, string afterbinding)
{
var result = ParseScript(
script,
flags: Flags.EnableExpressionChaining);

Assert.Equal(beforebinding, StructuralPrint.Print(result.Root));
var actual = StructuralPrint.Print(result.Root);
Assert.Equal(beforebinding, actual);

var symbolTable = new SymbolTable();
symbolTable.AddFunction(new JoinFunction());

// Test same cases via CheckResult
var check = new CheckResult(new Engine());

check.SetText(script, new ParserOptions { AllowsSideEffects = true })
.SetBindingInfo()
.SetBindingInfo(symbolTable)
.ApplyBinding();

var result2 = check.ApplyGetLogging();
Assert.Equal(afterbinding, result2);
actual = check.ApplyGetLogging();
Assert.Equal(afterbinding, actual);
}

private class TestSanitizer : ISanitizedNameProvider
Expand Down

0 comments on commit de2fbd0

Please sign in to comment.