Skip to content

Commit

Permalink
Merge pull request #6 from dills122/AddScrubMode
Browse files Browse the repository at this point in the history
Add scrub mode
  • Loading branch information
dills122 authored Dec 16, 2018
2 parents 4677cc7 + 10b5f42 commit 1594b53
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ShamWow.Interfaces/Constants/ScrubbingType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ShamWow.Constants
{
public enum ScrubTypes
public enum ScrubMode
{
Full,
Marked
Expand Down
2 changes: 1 addition & 1 deletion ShamWow.Interfaces/ShamWow.Interfaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Dills122</Authors>
<Company>Steele Inc.</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0</Version>
<Version>2.1</Version>
<Description>Attributes and scrubbers for ShamWow scrubber.</Description>
<PackageProjectUrl></PackageProjectUrl>
<PackageLicenseUrl>https://github.com/dills122/ShamWow/blob/master/LICENSE</PackageLicenseUrl>
Expand Down
36 changes: 36 additions & 0 deletions ShamWow.Tests/MarkedTest.cs
Original file line number Diff line number Diff line change
@@ -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<SimpleTest>(cleanData);
Assert.True(IsSuccessful);
Assert.Equal(stayTheSame, cleanData.KeepMeTheSame);
Assert.NotEqual(email, cleanData.emailStr);
}
}
}
2 changes: 1 addition & 1 deletion ShamWow.Tests/PreserveValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions ShamWow.Tests/ScrubberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions ShamWow.Tests/StatefulTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions ShamWow/Processor/Factory.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using ShamWow.Constants;
using System;

namespace ShamWow.Processor
{
public class Factory
{
private Func<Object, ShamWowEngine> _ctorCaller;
private Func<Object, ScrubMode, ShamWowEngine> _ctorCallerMode;

public Factory (Func<Object, ShamWowEngine> ctorCaller)
public Factory (Func<Object, ScrubMode, ShamWowEngine> 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
{
Expand Down
34 changes: 27 additions & 7 deletions ShamWow/Processor/ShamWow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using System.Linq;
using ShamWow.Interfaces.Attributes;
using ShamWow.Constants;

namespace ShamWow.Processor
{
Expand All @@ -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)
{
Expand All @@ -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<string, object> stateValues)
private ShamWowEngine(object dirtyDataInstance, ScrubMode mode, Dictionary<string, object> stateValues)
{
if (dirtyDataInstance == null)
{
Expand All @@ -44,6 +47,7 @@ private ShamWowEngine(object dirtyDataInstance, Dictionary<string, object> state
_type = dirtyDataInstance.GetType();
_dataInstance = dirtyDataInstance;
_manifest = new DocumentManifest();
_mode = mode;
_router = new Router(_type, _dataInstance, stateValues);
}

Expand All @@ -53,7 +57,7 @@ private ShamWowEngine(object dirtyDataInstance, Dictionary<string, object> state
/// <returns></returns>
public static Factory GetFactory()
{
return new Factory((obj) => new ShamWowEngine(obj));
return new Factory((obj, scrub) => new ShamWowEngine(obj, scrub));
}

/// <summary>
Expand Down Expand Up @@ -84,6 +88,7 @@ public DocumentManifest GetManifest()
/// <returns></returns>
public ShamWowEngine Scrub()
{

ScrubCollections(GetCollections());

ScrubClasses(GetClasses());
Expand Down Expand Up @@ -176,7 +181,7 @@ private List<PropertyInfo> GetCollections()
.Where(p => p.GetCustomAttribute<PreserveValueAttribute>() == null) // If a property has [PreserveValue], then don't return it in the collection to scrub
.ToList();

return collection;
return FilterProperties(collection);
}

/// <summary>
Expand Down Expand Up @@ -205,7 +210,7 @@ private List<PropertyInfo> GetBaseTypes()
.Where(p => p.GetCustomAttribute<PreserveValueAttribute>() == null) // If a property has [PreserveValue], then don't return it in the collection to scrub
.ToList();

return collection;
return FilterProperties(collection);
}

/// <summary>
Expand All @@ -215,7 +220,7 @@ private List<PropertyInfo> GetBaseTypes()
/// <returns></returns>
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());
Expand All @@ -234,7 +239,7 @@ private Task ScrubClass(PropertyInfo property)
/// <returns></returns>
private Task<object> ScrubClass(object obj)
{
ShamWowEngine process = new ShamWowEngine(obj, _router.GetValues())
ShamWowEngine process = new ShamWowEngine(obj, _mode, _router.GetValues())
.Scrub();

_router.MergeStateValues(process.GetStateValues());
Expand Down Expand Up @@ -312,6 +317,21 @@ private object GetPropertyValue(PropertyInfo property)
: null;
}

private List<PropertyInfo> FilterProperties(List<PropertyInfo> 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;
}

/// <summary>
/// Checks if a value is a default value
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion ShamWow/ShamWow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AssemblyName>Rock.Nexsys.ShamWow</AssemblyName>
<RootNamespace>Rock.Nexsys.ShamWow</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0</Version>
<Version>2.1</Version>
<PackageLicenseUrl>https://github.com/dills122/ShamWow/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl></PackageProjectUrl>
<RepositoryUrl>https://github.com/dills122/ShamWow</RepositoryUrl>
Expand Down
26 changes: 18 additions & 8 deletions WorkingWithSrubbersAndAttributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 1594b53

Please sign in to comment.