diff --git a/README.md b/README.md index 6d560ac..4589374 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ Once your POCOs are annotated and you have installed the ShamWow NuGet package y ``` csharp //Fake object var obj = new object(); -//Get instance of ShamWow -IShamWow processor = ShamWowEngine.GetFactory().Create(obj); +//Get instance of ShamWow and run in marked only mode +IShamWow processor = ShamWowEngine.GetFactory().Create(obj, ScrubMode.Marked); ``` #### Step 2 Scrubbing diff --git a/ShamWow.Interfaces/Constants/ScrubbingType.cs b/ShamWow.Interfaces/Constants/ScrubbingType.cs index c47b009..d4f9e84 100644 --- a/ShamWow.Interfaces/Constants/ScrubbingType.cs +++ b/ShamWow.Interfaces/Constants/ScrubbingType.cs @@ -1,6 +1,6 @@ namespace ShamWow.Constants { - public enum ScrubTypes + public enum ScrubMode { Full, Marked diff --git a/ShamWow.Interfaces/ShamWow.Interfaces.csproj b/ShamWow.Interfaces/ShamWow.Interfaces.csproj index e831754..4bd3b3f 100644 --- a/ShamWow.Interfaces/ShamWow.Interfaces.csproj +++ b/ShamWow.Interfaces/ShamWow.Interfaces.csproj @@ -6,7 +6,7 @@ Dills122 Steele Inc. true - 2.0 + 2.1 Attributes and scrubbers for ShamWow scrubber. https://github.com/dills122/ShamWow/blob/master/LICENSE diff --git a/ShamWow.Tests/MarkedTest.cs b/ShamWow.Tests/MarkedTest.cs new file mode 100644 index 0000000..0b7d3f3 --- /dev/null +++ b/ShamWow.Tests/MarkedTest.cs @@ -0,0 +1,36 @@ +using ShamWow.Processor; +using ShamWowTests.TestModels; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace ShamWow.Tests +{ + public class MarkedTest + { + [Fact] + public void MarkedScrubModeTest() + { + const string email = "this really isn't an email right?"; + const string stayTheSame = "This should be.."; + var model = new SimpleTest + { + emailStr = email, + KeepMeTheSame = stayTheSame + }; + + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Marked); + processor.Scrub(); + + var cleanData = (SimpleTest)processor.CleanData(); + var man = processor.GetManifest(); + var IsSuccessful = processor.CheckManifest(); + + Assert.IsType(cleanData); + Assert.True(IsSuccessful); + Assert.Equal(stayTheSame, cleanData.KeepMeTheSame); + Assert.NotEqual(email, cleanData.emailStr); + } + } +} diff --git a/ShamWow.Tests/PreserveValueTests.cs b/ShamWow.Tests/PreserveValueTests.cs index e13eb2b..9ef2622 100644 --- a/ShamWow.Tests/PreserveValueTests.cs +++ b/ShamWow.Tests/PreserveValueTests.cs @@ -18,7 +18,7 @@ public void ScrubProperty_WithPredefinedValue() i = 50 }; - IShamWow processor = ShamWowEngine.GetFactory().Create(model); + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Full); processor.Scrub(); var cleanedData = (PreserveTest)processor.CleanData(); diff --git a/ShamWow.Tests/ScrubberTests.cs b/ShamWow.Tests/ScrubberTests.cs index 0de58bc..e2f85c8 100644 --- a/ShamWow.Tests/ScrubberTests.cs +++ b/ShamWow.Tests/ScrubberTests.cs @@ -27,7 +27,7 @@ public void SimpleTest(string email) Byte = Encoding.ASCII.GetBytes("Test STring") }; - IShamWow processor = ShamWowEngine.GetFactory().Create(test); + IShamWow processor = ShamWowEngine.GetFactory().Create(test, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (SimpleTest)processor.CleanData(); @@ -61,7 +61,7 @@ public void ComplexTest(string email, string phone) } }; - ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(complex); + ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(complex, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (ComplexTest)processor.CleanData(); @@ -94,7 +94,7 @@ public void FullScrubModeTest(string email, string randString, string randString }; - ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(test); + ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(test, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (SimpleTest)processor.CleanData(); @@ -114,7 +114,7 @@ public void PreserveValue_DoesNotScrubTheMarkedValue() KeepMeTheSame = expectedValue }; - ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(simpleTest); + ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(simpleTest, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (SimpleTest)processor.CleanData(); @@ -134,7 +134,7 @@ public void ArrayScrub_EnsureSameTypeArrayReturnedScrubbed() str = "Test string" }; - IShamWow processor = ShamWowEngine.GetFactory().Create(model); + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Full); processor.Scrub(); var cleanedData = (ArrayTest)processor.CleanData(); @@ -159,7 +159,7 @@ public void FileScrub_EnsureFileIsScrubbed() orderFile = Encoding.ASCII.GetBytes(fileStr) }; - IShamWow processor = ShamWowEngine.GetFactory().Create(model); + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Full); processor.Scrub(); var cleanData = (FileTest)processor.CleanData(); diff --git a/ShamWow.Tests/StatefulTests.cs b/ShamWow.Tests/StatefulTests.cs index e57a80b..a4c8242 100644 --- a/ShamWow.Tests/StatefulTests.cs +++ b/ShamWow.Tests/StatefulTests.cs @@ -25,7 +25,7 @@ public void TestStatefulAttribute(string randomStr, string otherStatful) }; - IShamWow processor = ShamWowEngine.GetFactory().Create(model); + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (StatefulTest)processor.CleanData(); @@ -55,7 +55,7 @@ public void TestComplexStatefulModel(string randomStr, string anotherRandomStr, }; - ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(model); + ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (ComplexStateTest)processor.CleanData(); @@ -108,7 +108,7 @@ public void FullModelTest(string randomStr, string anotherRandomStr, int num, in } }; - ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(model); + ShamWow.Processor.IShamWow processor = ShamWow.Processor.ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (FullModelTest)processor.CleanData(); @@ -143,7 +143,7 @@ public void GivenString_ScrubToNumber_SaveState() IdTwo = IdTwo }; - IShamWow processor = ShamWowEngine.GetFactory().Create(model); + IShamWow processor = ShamWowEngine.GetFactory().Create(model, Constants.ScrubMode.Marked); processor.Scrub(); var cleanedData = (StatefulStringIdTest)processor.CleanData(); diff --git a/ShamWow/Processor/Factory.cs b/ShamWow/Processor/Factory.cs index 5e1345c..72223ff 100644 --- a/ShamWow/Processor/Factory.cs +++ b/ShamWow/Processor/Factory.cs @@ -1,21 +1,22 @@ -using System; +using ShamWow.Constants; +using System; namespace ShamWow.Processor { public class Factory { - private Func _ctorCaller; + private Func _ctorCallerMode; - public Factory (Func ctorCaller) + public Factory (Func ctorCaller) { - _ctorCaller = ctorCaller; + _ctorCallerMode = ctorCaller; } - public ShamWowEngine Create(object unScrubbedData) + public ShamWowEngine Create(object unScrubbedData, ScrubMode mode) { - if(unScrubbedData != null) + if (unScrubbedData != null) { - return _ctorCaller(unScrubbedData); + return _ctorCallerMode(unScrubbedData, mode); } else { diff --git a/ShamWow/Processor/ShamWow.cs b/ShamWow/Processor/ShamWow.cs index e3225d4..f826ae8 100644 --- a/ShamWow/Processor/ShamWow.cs +++ b/ShamWow/Processor/ShamWow.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using System.Linq; using ShamWow.Interfaces.Attributes; +using ShamWow.Constants; namespace ShamWow.Processor { @@ -18,8 +19,9 @@ public class ShamWowEngine : IShamWow private object _dataInstance; private DocumentManifest _manifest; private bool _IsScrubbed = false; + private ScrubMode _mode; - private ShamWowEngine(object dirtyDataInstance) + private ShamWowEngine(object dirtyDataInstance, ScrubMode mode) { if (dirtyDataInstance == null) { @@ -30,10 +32,11 @@ private ShamWowEngine(object dirtyDataInstance) _type = dirtyDataInstance.GetType(); _dataInstance = dirtyDataInstance; _manifest = new DocumentManifest(); + _mode = mode; _router = new Router(_type, dirtyDataInstance); } - private ShamWowEngine(object dirtyDataInstance, Dictionary stateValues) + private ShamWowEngine(object dirtyDataInstance, ScrubMode mode, Dictionary stateValues) { if (dirtyDataInstance == null) { @@ -44,6 +47,7 @@ private ShamWowEngine(object dirtyDataInstance, Dictionary state _type = dirtyDataInstance.GetType(); _dataInstance = dirtyDataInstance; _manifest = new DocumentManifest(); + _mode = mode; _router = new Router(_type, _dataInstance, stateValues); } @@ -53,7 +57,7 @@ private ShamWowEngine(object dirtyDataInstance, Dictionary state /// public static Factory GetFactory() { - return new Factory((obj) => new ShamWowEngine(obj)); + return new Factory((obj, scrub) => new ShamWowEngine(obj, scrub)); } /// @@ -84,6 +88,7 @@ public DocumentManifest GetManifest() /// public ShamWowEngine Scrub() { + ScrubCollections(GetCollections()); ScrubClasses(GetClasses()); @@ -176,7 +181,7 @@ private List GetCollections() .Where(p => p.GetCustomAttribute() == null) // If a property has [PreserveValue], then don't return it in the collection to scrub .ToList(); - return collection; + return FilterProperties(collection); } /// @@ -205,7 +210,7 @@ private List GetBaseTypes() .Where(p => p.GetCustomAttribute() == null) // If a property has [PreserveValue], then don't return it in the collection to scrub .ToList(); - return collection; + return FilterProperties(collection); } /// @@ -215,7 +220,7 @@ private List GetBaseTypes() /// private Task ScrubClass(PropertyInfo property) { - ShamWowEngine process = new ShamWowEngine(property.GetValue(_dataInstance), _router.GetValues()) + ShamWowEngine process = new ShamWowEngine(property.GetValue(_dataInstance), _mode, _router.GetValues()) .Scrub(); _router.MergeStateValues(process.GetStateValues()); @@ -234,7 +239,7 @@ private Task ScrubClass(PropertyInfo property) /// private Task ScrubClass(object obj) { - ShamWowEngine process = new ShamWowEngine(obj, _router.GetValues()) + ShamWowEngine process = new ShamWowEngine(obj, _mode, _router.GetValues()) .Scrub(); _router.MergeStateValues(process.GetStateValues()); @@ -312,6 +317,21 @@ private object GetPropertyValue(PropertyInfo property) : null; } + private List FilterProperties(List properties) + { + if (_mode == ScrubMode.Marked) + { + var attributeList = Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.Namespace == "ShamWow.Interfaces.Attributes") + .ToList(); + + return properties.Where(prop => prop.GetCustomAttributes() + .Where(a => attributeList.Contains(a.GetType())) != null) + .ToList(); + } + return properties; + } + /// /// Checks if a value is a default value /// diff --git a/ShamWow/ShamWow.csproj b/ShamWow/ShamWow.csproj index b924d79..e6dc611 100644 --- a/ShamWow/ShamWow.csproj +++ b/ShamWow/ShamWow.csproj @@ -10,7 +10,7 @@ Rock.Nexsys.ShamWow Rock.Nexsys.ShamWow true - 2.0 + 2.1 https://github.com/dills122/ShamWow/blob/master/LICENSE https://github.com/dills122/ShamWow diff --git a/WorkingWithSrubbersAndAttributes.md b/WorkingWithSrubbersAndAttributes.md index b1b84ab..3203cea 100644 --- a/WorkingWithSrubbersAndAttributes.md +++ b/WorkingWithSrubbersAndAttributes.md @@ -2,14 +2,15 @@ ### Contents -1. [Strings](#Strings) -2. [Decimal](#Decimal) -3. [Double](#Double) -4. [Integer](#Integer) -5. [Long](#Long) -6. [Short](#Short) -7. [Misc](#Miscellaneous) -8. [Other Attributes](#Other-Scrubbers) +1. [Scrub Modes](#Scrub-Modes) +2. [Strings](#Strings) +3. [Decimal](#Decimal) +4. [Double](#Double) +5. [Integer](#Integer) +6. [Long](#Long) +7. [Short](#Short) +8. [Misc](#Miscellaneous) +9. [Other Attributes](#Other-Scrubbers) 1. [Predefined Value Atr.](#PredefinedValue) 2. [Preserve Value Atr.](#PreserveValue) 3. [Stateful Scrub Atr.](#StatefulScrub) @@ -27,6 +28,15 @@ ShamWow supports all primitive types such as 7. Byte (Only Arrays) 8. Arrays of any type above +## Scrub Modes + +ShamWow offers two types of scrubbing modes, which are pretty self explanatory. + +1. Marked +2. Full + +Marked will only scrub value which have a scrubbing attribute on the property, whereas full mode will scrub any property that is accessible and not null. + ## Strings