From f260a01bfd523886c807b3c901b0700712f577cd Mon Sep 17 00:00:00 2001 From: lmat Date: Thu, 16 Nov 2017 12:57:31 -0500 Subject: [PATCH] Keep tokens as they are, don't translate them Before, it was necessary to remove '/' from tokens so that they won't be divided up within kvp. Now that kvp doesn't parse tokens, it's okay to pass '/', and it's better not to translate user-provided tokens if at all possible. --- libgnucash/engine/Account.cpp | 12 ++--- libgnucash/engine/test/gtest-import-map.cpp | 56 ++++++++++----------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 895ff8f210..19d829d050 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -5234,10 +5234,8 @@ get_first_pass_probabilities(GncImportMatchMap * imap, GList * tokens) * in the input tokens list. */ for (auto current_token = tokens; current_token; current_token = current_token->next) { - auto translated_token = std::string {static_cast (current_token->data)}; - std::replace (translated_token.begin (), translated_token.end (), '/', '-'); token_accounts_info tokenInfo{}; - auto path = std::string{IMAP_FRAME_BAYES "-"} + translated_token; + auto path = std::string{IMAP_FRAME_BAYES "/"} + static_cast (current_token->data); qof_instance_foreach_slot_prefix (QOF_INSTANCE (imap->acc), path, &build_token_info, tokenInfo); for (auto const & current_account_token : tokenInfo.accounts) { @@ -5329,7 +5327,6 @@ change_imap_entry (GncImportMatchMap *imap, std::string const & path, int64_t to gnc_features_set_used (imap->book, GNC_FEATURE_GUID_BAYESIAN); } - /** Updates the imap for a given account using a list of tokens */ void gnc_account_imap_add_account_bayes (GncImportMatchMap *imap, @@ -5369,9 +5366,7 @@ gnc_account_imap_add_account_bayes (GncImportMatchMap *imap, /* start off with one token for this account */ token_count = 1; PINFO("adding token '%s'", (char*)current_token->data); - std::string translated_token {static_cast(current_token->data)}; - std::replace(translated_token.begin(), translated_token.end(), '/', '-'); - auto path = std::string {IMAP_FRAME_BAYES} + '-' + translated_token + '-' + guid_string; + auto path = std::string {IMAP_FRAME_BAYES} + '/' + static_cast(current_token->data) + '/' + guid_string; /* change the imap entry for the account */ change_imap_entry (imap, path, token_count); } @@ -5450,7 +5445,7 @@ build_bayes (const char *key, KvpValue * value, imap_info & imapInfo) auto temp_guid = gnc::GUID::from_string (std::get <2> (parsed_key)); GncGUID guid = temp_guid; auto map_account = xaccAccountLookup (&guid, gnc_account_get_book (imapInfo.source_account)); - std::string category_head {std::get <0> (parsed_key) + "-" + std::get <1> (parsed_key)}; + std::string category_head {std::get <0> (parsed_key) + "/" + std::get <1> (parsed_key)}; auto imap_node = static_cast (g_malloc (sizeof (imap_info))); auto count = entry.second->get (); imap_node->source_account = imapInfo.source_account; @@ -5609,7 +5604,6 @@ convert_entry (std::pair , KvpValue*> entry, Account* entry.first.emplace_back (guid_str); std::string new_key {std::accumulate (entry.first.begin(), entry.first.end(), std::string {})}; new_key = IMAP_FRAME_BAYES + new_key; - std::replace (new_key.begin(), new_key.end(), '/', '-'); return {new_key, entry.second}; } diff --git a/libgnucash/engine/test/gtest-import-map.cpp b/libgnucash/engine/test/gtest-import-map.cpp index d811574387..df72445db7 100644 --- a/libgnucash/engine/test/gtest-import-map.cpp +++ b/libgnucash/engine/test/gtest-import-map.cpp @@ -254,12 +254,12 @@ TEST_F(ImapBayesTest, FindAccountBayes) auto acct2_guid = guid_to_string (xaccAccountGetGUID(t_expense_account2)); auto value = new KvpValue(INT64_C(42)); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + foo + "-" + acct1_guid}, value); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + bar + "-" + acct1_guid}, new KvpValue{*value}); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + baz + "-" + acct2_guid}, new KvpValue{*value}); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + waldo + "-" + acct2_guid}, new KvpValue{*value}); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + pepper + "-" + acct1_guid}, new KvpValue{*value}); - root->set_path({std::string{IMAP_FRAME_BAYES} + "-" + salt + "-" + acct2_guid}, new KvpValue{*value}); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + foo + "/" + acct1_guid}, value); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + bar + "/" + acct1_guid}, new KvpValue{*value}); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + baz + "/" + acct2_guid}, new KvpValue{*value}); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + waldo + "/" + acct2_guid}, new KvpValue{*value}); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + pepper + "/" + acct1_guid}, new KvpValue{*value}); + root->set_path({std::string{IMAP_FRAME_BAYES} + "/" + salt + "/" + acct2_guid}, new KvpValue{*value}); auto account = gnc_account_imap_find_account_bayes(t_imap, t_list1); EXPECT_EQ(t_expense_account1, account); @@ -292,29 +292,29 @@ TEST_F(ImapBayesTest, AddAccountBayes) auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account)); auto acct1_guid = guid_to_string (xaccAccountGetGUID(t_expense_account1)); auto acct2_guid = guid_to_string (xaccAccountGetGUID(t_expense_account2)); - auto value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-foo-bar"}); + auto value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/foo/bar"}); auto check_account = [this](KvpValue* v) { return (v->get(), this->t_imap->book); }; - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + foo + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + foo + "/" + acct1_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + bar + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + bar + "/" + acct1_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + baz + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + baz + "/" + acct2_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + waldo + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + waldo + "/" + acct2_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + pepper + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + pepper + "/" + acct1_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + salt + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + salt + "/" + acct2_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + baz + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + baz + "/" + acct1_guid}); EXPECT_EQ(nullptr, value); qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account)); gnc_account_imap_add_account_bayes(t_imap, t_list2, t_expense_account2); qof_instance_mark_clean(QOF_INSTANCE(t_bank_account)); qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account)); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + baz + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + baz + "/" + acct2_guid}); EXPECT_EQ(2, value->get()); } @@ -335,16 +335,16 @@ TEST_F(ImapBayesTest, ConvertAccountBayes) auto val2 = new KvpValue(static_cast(5)); auto val3 = new KvpValue(static_cast(2)); // Test for existing entries, all will be 1 - auto value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + foo + "-" + acct1_guid}); + auto value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + foo + "/" + acct1_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + bar + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + bar + "/" + acct1_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + baz + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + baz + "/" + acct2_guid}); EXPECT_EQ(1, value->get()); - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + waldo + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + waldo + "/" + acct2_guid}); EXPECT_EQ(1, value->get()); // Set up some old entries - root->set_path({IMAP_FRAME_BAYES, "token", "with", "slashes", "Asset-Bank"}, val1); + root->set_path({IMAP_FRAME_BAYES, "severely", "divided", "token", "Asset-Bank"}, val1); root->set_path({IMAP_FRAME_BAYES, salt, "Asset-Bank#Bank"}, new KvpValue{*val1}); root->set_path({IMAP_FRAME_BAYES, salt, "Asset>Bank#Bank"}, val2); root->set_path({IMAP_FRAME_BAYES, pork, "Expense#Food"}, new KvpValue{*val2}); @@ -356,19 +356,19 @@ TEST_F(ImapBayesTest, ConvertAccountBayes) // Start Convert gnc_account_imap_convert_bayes (t_imap->book); // convert from 'Asset-Bank' to 'Asset-Bank' guid - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-token-with-slashes-" + acct3_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/severely/divided/token/" + acct3_guid}); EXPECT_EQ(10, value->get()); // convert from 'Asset-Bank#Bank' to 'Sav Bank' guid - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + salt + "-" + acct4_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + salt + "/" + acct4_guid}); EXPECT_EQ(10, value->get()); // convert from 'Expense#Food' to 'Food' guid - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + pork + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + pork + "/" + acct1_guid}); EXPECT_EQ(5, value->get()); // convert from 'Expense#Drink' to 'Drink' guid - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + sausage + "-" + acct2_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + sausage + "/" + acct2_guid}); EXPECT_EQ(2, value->get()); // convert from 'Expense#Food' to 'Food' guid but add to original value - value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "-" + foo + "-" + acct1_guid}); + value = root->get_slot({std::string{IMAP_FRAME_BAYES} + "/" + foo + "/" + acct1_guid}); EXPECT_EQ(5, value->get()); // Check for run once flag auto vals = book->get_slot({"changed-bayesian-to-guid"}); @@ -402,9 +402,9 @@ TEST_F (ImapBayesTest, get_bayes_info) EXPECT_EQ (info->source_account, t_bank_account); EXPECT_EQ (info->map_account, t_expense_account1); auto acct1_guid = guid_to_string (xaccAccountGetGUID(t_expense_account1)); //Food - EXPECT_STREQ (info->full_category, (std::string {IMAP_FRAME_BAYES} + "-one-two-three-" + acct1_guid).c_str ()); - EXPECT_STREQ (info->match_string, "one-two-three"); - EXPECT_STREQ (info->category_head, (std::string {IMAP_FRAME_BAYES} + "-one-two-three").c_str ()); + EXPECT_STREQ (info->full_category, (std::string {IMAP_FRAME_BAYES} + "/one/two/three/" + acct1_guid).c_str ()); + EXPECT_STREQ (info->match_string, "one/two/three"); + EXPECT_STREQ (info->category_head, (std::string {IMAP_FRAME_BAYES} + "/one/two/three").c_str ()); EXPECT_STREQ (info->count, "1"); }