From b96e38c0596ce7a4c8dbe8765bb893d181f720db Mon Sep 17 00:00:00 2001 From: "Martin Hinshelwood nkdAgility.com" Date: Fri, 4 Oct 2024 11:17:35 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(StringManipulatorTool):?= =?UTF-8?q?=20refactor=20to=20add=20default=20manipulator=20if=20none=20ar?= =?UTF-8?q?e=20configured=20=F0=9F=94=A7=20(appsettings.json):=20remove=20?= =?UTF-8?q?default=20manipulator=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the default manipulator configuration from `appsettings.json` to allow for more dynamic configuration. The code now checks if manipulators are configured; if not, it adds a default manipulator programmatically. This change enhances flexibility by allowing the application to operate with a default manipulator without requiring explicit configuration in the settings file. --- appsettings.json | 10 +--------- .../Tools/StringManipulatorTool.cs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/appsettings.json b/appsettings.json index 5187bb442..1bfc0ef6e 100644 --- a/appsettings.json +++ b/appsettings.json @@ -150,15 +150,7 @@ "StringManipulatorTool": { "Enabled": true, "MaxStringLength": 1000000, - "Manipulators": [ - { - "$type": "RegexStringManipulator", - "Enabled": true, - "Pattern": "[^( -~)\n\r\t]+", - "Replacement": "", - "Description": "Remove invalid characters from the end of the string" - } - ] + "Manipulators": [] }, "TfsUserMappingTool": { "Enabled": false, diff --git a/src/MigrationTools/Tools/StringManipulatorTool.cs b/src/MigrationTools/Tools/StringManipulatorTool.cs index 2cb9d5fb6..95e393688 100644 --- a/src/MigrationTools/Tools/StringManipulatorTool.cs +++ b/src/MigrationTools/Tools/StringManipulatorTool.cs @@ -34,9 +34,11 @@ public void ProcessorExecutionWithFieldItem(IProcessor processor, FieldItem fiel } if (fieldItem.FieldType == "String" && fieldItem.Value != null) { - if (HasManipulators()) + if (!HasManipulators()) { - foreach (var manipulator in Options.Manipulators) + AddDefaultManipulator(); + } + foreach (var manipulator in Options.Manipulators) { if (manipulator.Enabled) { @@ -49,7 +51,7 @@ public void ProcessorExecutionWithFieldItem(IProcessor processor, FieldItem fiel Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Disabled::{Description}", GetType().Name, manipulator.Description); } } - } + if (HasStringTooLong(fieldItem)) { fieldItem.Value = fieldItem.Value.ToString().Substring(0, Math.Min(fieldItem.Value.ToString().Length, Options.MaxStringLength)); @@ -58,6 +60,15 @@ public void ProcessorExecutionWithFieldItem(IProcessor processor, FieldItem fiel } + private void AddDefaultManipulator() + { + if (Options.Manipulators == null) + { + Options.Manipulators = new List(); + } + Options.Manipulators.Add(new RegexStringManipulator() { Enabled = true, Description = "Default: Removes invalid chars!", Pattern = "[^( -~)\n\r\t]+", Replacement = "" }); + } + private bool HasStringTooLong(FieldItem fieldItem) { return fieldItem.Value.ToString().Length > 0 && fieldItem.Value.ToString().Length > Options.MaxStringLength;