From de2fbd06b50a70e4346fe6272b90489aa2cebde3 Mon Sep 17 00:00:00 2001 From: Anderson Silva Date: Wed, 12 Feb 2025 18:08:56 -0600 Subject: [PATCH] Dont anonymize Join nor default enums (#2847) 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) --- .../Logging/StructuralPrint.cs | 12 +++++++++- .../Texl/BuiltinFunctionsCore.cs | 7 ++++-- .../FormatterTests.cs | 24 +++++++++++++++---- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs b/src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs index a5330764cb..a1b9d2384a 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Logging/StructuralPrint.cs @@ -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; @@ -121,7 +122,16 @@ public override LazyList 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.Of(node.ToString()); + } var values = node.Left.Accept(this, Precedence.Primary); values = values.With(separator); diff --git a/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs b/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs index 4460f31f36..5822db9f2f 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Texl/BuiltinFunctionsCore.cs @@ -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 OtherKnownFunctions = new HashSet() { "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" diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs index 57cb68c8d9..87bd613afe 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs @@ -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; @@ -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())); @@ -66,6 +68,14 @@ 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) { @@ -73,17 +83,21 @@ public void TestStucturalPrintWithBinding(string script, string beforebinding, s 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