From 6aa366c7148e53c3731c6f417102ae45d01e490d Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Sun, 27 Sep 2020 13:06:44 +1000 Subject: [PATCH] use a CSV file for the rules instead --- .gitignore | 2 +- Program.cs | 19 +++++++------------ README.md | 23 +++++++++-------------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 35d5b10..dcaa92c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ bin/ obj/ test.csv -rules.json +rules.csv diff --git a/Program.cs b/Program.cs index 448b6b7..7a9aac5 100644 --- a/Program.cs +++ b/Program.cs @@ -20,10 +20,8 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Text.Json; using System.CommandLine.DragonFruit; using System.Linq; -using System.Text.Json.Serialization; using CsvHelper; namespace BunyMuny { @@ -32,18 +30,16 @@ namespace BunyMuny { /// BunyMuny parses the CSV output of various bank statement listings and converts it to something more human readable with nice visualisations. /// /// The CSV file to read - /// The JSON file to use for rules when parsing statement descriptions + /// The CSV file to use for rules when parsing statement descriptions /// - private static int Main(string file = "test.csv", string ruleFile = "rules.json") { + private static int Main(string file = "test.csv", string ruleFile = "rules.csv") { var bank = Bank.Other; var statements = new List(); List rules; using (var ruleStreamReader = new StreamReader(ruleFile)) { - var jsonOptions = new JsonSerializerOptions(); - jsonOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); - jsonOptions.WriteIndented = true; - rules = JsonSerializer.Deserialize>(ruleStreamReader.ReadToEnd(), jsonOptions); + using var ruleCsv = new CsvReader(ruleStreamReader,CultureInfo.InvariantCulture); + rules = ruleCsv.GetRecords().ToList(); } using var sr = new StreamReader(file); @@ -73,14 +69,13 @@ namespace BunyMuny { switch (bank) { case Bank.ME: var value = double.Parse(csv.GetField("Debits and credits").TrimStart().Replace("$", "")); - // SHOW PET THIS - var ruleValues = MatchAgainstRules(rules, csv.GetField("Description")); + var (category, description) = MatchAgainstRules(rules, csv.GetField("Description")); statements.Add(new Statement() { Date = DateTime.ParseExact(csv.GetField("Date"), "dd/MM/yyyy", CultureInfo.InvariantCulture), OriginalDescription = csv.GetField("Description"), - Description = ruleValues.Description, - Category = ruleValues.Category, + Description = description, + Category = category, Value = value }); break; diff --git a/README.md b/README.md index 9d3fdb4..9ef1b2a 100644 --- a/README.md +++ b/README.md @@ -13,24 +13,19 @@ dotnet build ## The rules file By default, BunyMuny checks for rules in `rules.json` in the current directory. An example rules file might look like this: -```json -[ - { - "Match":"Start", - "Value": "Purchase Cash Converters", - "Category":"Personal", - "Description":"Cashies" - } -] +```csv +Match,Value,Category,Description,CaseSensitive +Start,Purchase Cash Converters,Personal,Cashies,true ``` -This means that any statement that starts with "Purchase Cash Converters" will be assigned the category "Personal", and the description "Cashies". +This means that any statement that starts with "Purchase Cash Converters" (case sensitive) will be assigned the category "Personal", and the description "Cashies". -- `"Match"` specifies the type of matching to perform. It can be any of the following: +- `Match` specifies the type of matching to perform. It can be any of the following: - "Start": Match the start of the description. - "End": Match the end of the description. - "Contains": Match anywhere in the description. - "Exact": Only match if the bank statement's description is exactly equal to the provided value. - "Regex": Match with [regular expressions](https://en.wikipedia.org/wiki/Regular_expression). -- `"Value"` specifies the value to match against. -- `"Category"` is the category that the statement should be filed under. -- `"Description"` is a short description to distinguish the particular merchant. +- `Value` specifies the value to match against. +- `Category` is the category that the statement should be filed under. +- `Description` is a short description to distinguish the particular merchant. +- `CaseSensitive` specifies whether or not the match should be case sensitive.