Skip to content

Commit

Permalink
fixed code warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dandraka committed Apr 25, 2024
1 parent b296c22 commit 00f6e58
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Zoro.Processor/CharExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Dandraka.Zoro.Processor
/// </summary>
public static class CharExtension
{
private static Random rnd = new Random(DateTime.Now.Millisecond);
private static readonly Random rnd = new Random(DateTime.Now.Millisecond);

/// <summary>
/// Returns true if the character is a vowel.
Expand Down
85 changes: 32 additions & 53 deletions Zoro.Processor/DataMasking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ public void Mask()

private DataTable GetData()
{
switch (config.DataSource)
{
case DataSource.CsvFile:
return ReadDataFromCSVFile();
case DataSource.JsonFile:
return ReadDataFromJSONFile();
case DataSource.Database:
return ReadDataFromDb();
default:
throw new NotSupportedException();
}
return config.DataSource switch
{
DataSource.CsvFile => ReadDataFromCSVFile(),
DataSource.JsonFile => ReadDataFromJSONFile(),
DataSource.Database => ReadDataFromDb(),
_ => throw new NotSupportedException(),
};
}

private void SaveData(DataTable dt)
Expand Down Expand Up @@ -157,26 +153,15 @@ private string GetMaskedString(string data, FieldMask fieldMask, DataRow row)
}
else
{
switch (fieldMask.MaskType)
s = fieldMask.MaskType switch
{
case MaskType.Asterisk:
s = GetAsteriskString(matchData, fieldMask.Asterisk[0]);
break;
case MaskType.Similar:
s = GetSimilarString(matchData);
break;
case MaskType.List:
s = GetStringFromList(row, fieldMask.ListOfPossibleReplacements);
break;
case MaskType.Query:
s = GetStringFromQuery(row, fieldMask.QueryReplacement);
break;
case MaskType.None:
s = matchData;
break;
default:
throw new NotImplementedException($"Mask type {fieldMask.MaskType} is not yet implemented");
}
MaskType.Asterisk => GetAsteriskString(matchData, fieldMask.Asterisk[0]),
MaskType.Similar => GetSimilarString(matchData),
MaskType.List => GetStringFromList(row, fieldMask.ListOfPossibleReplacements),
MaskType.Query => GetStringFromQuery(row, fieldMask.QueryReplacement),
MaskType.None => matchData,
_ => throw new NotImplementedException($"Mask type {fieldMask.MaskType} is not yet implemented"),
};
}
replaceStr += s;
}
Expand All @@ -185,27 +170,21 @@ private string GetMaskedString(string data, FieldMask fieldMask, DataRow row)
}
else
{
switch (fieldMask.MaskType)
return fieldMask.MaskType switch
{
case MaskType.Asterisk:
return GetAsteriskString(data, fieldMask.Asterisk[0]);
case MaskType.Similar:
return GetSimilarString(data);
case MaskType.List:
return GetStringFromList(row, fieldMask.ListOfPossibleReplacements);
case MaskType.Query:
return GetStringFromQuery(row, fieldMask.QueryReplacement);
case MaskType.None:
return data;
default:
throw new NotImplementedException($"Mask type {fieldMask.MaskType} is not yet implemented");
}
MaskType.Asterisk => GetAsteriskString(data, fieldMask.Asterisk[0]),
MaskType.Similar => GetSimilarString(data),
MaskType.List => GetStringFromList(row, fieldMask.ListOfPossibleReplacements),
MaskType.Query => GetStringFromQuery(row, fieldMask.QueryReplacement),
MaskType.None => data,
_ => throw new NotImplementedException($"Mask type {fieldMask.MaskType} is not yet implemented"),
};
}
}

private string GetSimilarString(string data)
{
Func<char, char> method = (c) =>
char method(char c)
{
if (Char.IsDigit(c))
{
Expand All @@ -229,14 +208,14 @@ private string GetSimilarString(string data)
}
return a;
}
};
return getReplacedString(data, method);
}
return GetReplacedString(data, method);
}

private string GetAsteriskString(string data, char asterisk)
{
Func<char, char> method = (c) => { return asterisk; };
return getReplacedString(data, method);
char method(char c) { return asterisk; }
return GetReplacedString(data, method);
}

private string GetStringFromList(DataRow row, List<Replacement> listOfReplacements)
Expand Down Expand Up @@ -323,7 +302,7 @@ private string GetStringFromQuery(DataRow row, QueryReplacement queryReplacement
return GetStringFromList(row, queryReplacement.ListOfPossibleReplacements);
}

private string getReplacedString(string data, Func<char, char> method)
private string GetReplacedString(string data, Func<char, char> method)
{
string anon = string.Empty;

Expand Down Expand Up @@ -437,9 +416,9 @@ private void AddJSONData(JsonElement jsonNode, ref DataTable tbl)

if (tbl.Columns.Count == 0)
{
// do setup
List<JsonProperty> childrenListForColumns = null;
var topNodeForColumns = jsonNode.EnumerateObject().ToList()[0].Value;
// do setup
List<JsonProperty> childrenListForColumns;
if (topNodeForColumns.ValueKind == JsonValueKind.Object)
{
childrenListForColumns = jsonNode.EnumerateObject().ToList()[0].Value.EnumerateObject().ToList();
Expand Down Expand Up @@ -501,8 +480,8 @@ private void AddJSONData(JsonElement jsonNode, ref DataTable tbl)

// add row
var row = tbl.NewRow();
List<JsonProperty> childrenList = null;
var topNode = jsonNode.EnumerateObject().ToList()[0].Value;
List<JsonProperty> childrenList;
if (topNode.ValueKind == JsonValueKind.Object)
{
childrenList = jsonNode.EnumerateObject().ToList()[0].Value.EnumerateObject().ToList();
Expand Down
6 changes: 5 additions & 1 deletion Zoro.Processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Zoro is a data masking and anonymization utility. It fetches data from a database, a JSON or a CSV file, and either creates a JSON file, a CSV file or runs SQL statements with the masked data.

It can be used as a command line program or as a dotnet standard 2.1 library. To run the command line program, simply copy the ```tools``` dir from the Nuget package. Windows and Linux versions, both 64-bit, are available.
It can be used as a command line program or as a dotnet standard 2.1 library. To run the command line program, simply copy the ```tools``` dir from the [Nuget package](https://www.nuget.org/packages/Dandraka.Zoro). Windows and Linux versions, both 64-bit, are available.

## Usage:

Expand Down Expand Up @@ -139,6 +139,10 @@ ID;Name;BankAccount

If using a database to write data (DataDestination=Database), the number of parameters in SqlCommand ($field) must match the number of FieldMasks.

### Developer documentation

Please see the [generated docs](https://github.com/dandraka/Zoro/blob/master/docs/Zoro.Processor.md).

### Note:

Although not required by the license, the author kindly asks that you share any improvements you made.
17 changes: 11 additions & 6 deletions Zoro.Tests/DataMasking_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ namespace Dandraka.Zoro.Tests
/// </summary>
public class DataMasking_Tests : IDisposable
{
private Utility utility = new Utility();
private readonly Utility utility = new();
private static readonly string[] CHcities = ["Geneva", "Bern", "Thun"];
private static readonly string[] DEcities = ["Köln", "Berlin", "Hamburg"];
private static readonly string[] ITcities = ["Roma", "Venezia", "Milano"];
private static readonly string[] GRCities = ["Rethimno", "Trikala", "Patra"];

public DataMasking_Tests()
{
Expand All @@ -21,7 +25,8 @@ public DataMasking_Tests()

public void Dispose()
{
this.utility.Dispose();
utility.Dispose();
GC.SuppressFinalize(this);
}

[Fact]
Expand Down Expand Up @@ -264,16 +269,16 @@ public void T08_Mask_MaskType_Query_Test()
switch (country)
{
case "CH":
Assert.Contains<string>(city, new string[] { "Geneva", "Bern", "Thun" });
Assert.Contains<string>(city, CHcities);
break;
case "DE":
Assert.Contains<string>(city, new string[] { "Köln", "Berlin", "Hamburg" });
Assert.Contains<string>(city, DEcities);
break;
case "IT":
Assert.Contains<string>(city, new string[] { "Roma", "Venezia", "Milano" });
Assert.Contains<string>(city, ITcities);
break;
case "GR":
Assert.Contains<string>(city, new string[] { "Rethimno", "Trikala", "Patra" });
Assert.Contains<string>(city, GRCities);
break;
default:
throw new NotSupportedException($"Unexpected country {country} found");
Expand Down

0 comments on commit 00f6e58

Please sign in to comment.