Implement load and store options from/to book options.

This commit is contained in:
John Ralls
2020-01-20 11:37:21 -08:00
parent 5a82aac639
commit cbd0607e80
5 changed files with 344 additions and 18 deletions

View File

@@ -222,16 +222,14 @@ GncOptionDateValue::in_stream(std::istream& iss)
}
QofInstance*
qof_instance_from_string(const std::string& str, GncOptionUIType type)
qof_instance_from_guid(GncGUID* guid, GncOptionUIType type)
{
QofIdType qof_type;
bool commodity_type{false};
switch(type)
{
case GncOptionUIType::CURRENCY:
case GncOptionUIType::COMMODITY:
qof_type = "Commodity";
commodity_type = true;
break;
case GncOptionUIType::BUDGET:
qof_type = "Budget";
@@ -264,8 +262,17 @@ qof_instance_from_string(const std::string& str, GncOptionUIType type)
break;
}
auto book{gnc_get_current_book()};
if (commodity_type)
auto col{qof_book_get_collection(book, qof_type)};
return QOF_INSTANCE(qof_collection_lookup_entity(col, guid));
}
QofInstance*
qof_instance_from_string(const std::string& str, GncOptionUIType type)
{
if (type == GncOptionUIType::CURRENCY ||
type == GncOptionUIType::COMMODITY)
{
auto book{gnc_get_current_book()};
auto sep{str.find(":")};
auto name_space{str.substr(0, sep)};
auto mnemonic{str.substr(sep + 1, -1)};
@@ -275,8 +282,7 @@ qof_instance_from_string(const std::string& str, GncOptionUIType type)
mnemonic.c_str()));
}
auto guid{static_cast<GncGUID>(gnc::GUID::from_string(str))};
auto col{qof_book_get_collection(book, qof_type)};
return QOF_INSTANCE(qof_collection_lookup_entity(col, &guid));
return qof_instance_from_guid(&guid, type);
}
std::string

View File

@@ -264,6 +264,7 @@ private:
QofInstance* qof_instance_from_string(const std::string& str,
GncOptionUIType type);
QofInstance* qof_instance_from_guid(GncGUID*, GncOptionUIType type);
std::string qof_instance_to_string(const QofInstance* inst);
/* These will work when m_value is a built-in class; GnuCash class and container

View File

@@ -24,6 +24,7 @@
#include "gnc-optiondb.hpp"
#include <limits>
#include <sstream>
#include <kvp-value.hpp>
auto constexpr stream_max = std::numeric_limits<std::streamsize>::max();
GncOptionDB::GncOptionDB() : m_default_section{std::nullopt} {}
@@ -445,7 +446,10 @@ GncOptionDB::load_option_scheme(std::istream& iss)
}
if (!lookup_id)
throw std::runtime_error("No gnc:lookup-option found");
{
iss.setstate(std::ios_base::eofbit);
return iss; // No options
}
const auto& classifier = lookup_id->get().m_ids;
if (classifier.size() != 3)
throw std::runtime_error("Malformed option classifier.");
@@ -473,16 +477,55 @@ GncOptionDB::load_option_scheme(std::istream& iss)
return iss;
}
std::ostream&
GncOptionDB::save_to_scheme(std::ostream& oss, const char* options_prolog) const noexcept
{
for (auto section : m_sections)
{
const auto& [s_name, s_vec] = section;
oss << "\n; Section: " << s_name << "\n\n";
for (auto option : s_vec)
{
if (!option.is_changed())
continue;
oss << scheme_tags[0] << options_prolog << "\n";
oss << scheme_tags[1] << '"' << section.first.substr(0, classifier_size_max) << "\"\n";
oss << scheme_tags[1] << '"' << option.get_name().substr(0, classifier_size_max) << '"';
oss << scheme_tags[2] << "\n" << scheme_tags[3];
option.to_scheme(oss);
oss << scheme_tags[4] << "\n\n";
}
}
return oss;
}
std::istream&
GncOptionDB::load_from_scheme(std::istream& iss) noexcept
{
try {
while (iss.good())
load_option_scheme(iss);
iss.clear(); //unset eofbit and maybe failbit
}
catch (const std::runtime_error& err)
{
std::cerr << "Load of options from Scheme failed: " <<
err.what() << std::endl;
}
return iss;
}
std::ostream&
GncOptionDB::save_option_key_value(std::ostream& oss,
const char* section,
const char* name) const noexcept
const std::string& section,
const std::string& name) const noexcept
{
auto db_opt = find_option(section, name);
if (!db_opt || !db_opt->get().is_changed())
return oss;
oss << section << ":" << name << "=" << db_opt->get() << ";";
oss << section.substr(0, classifier_size_max) << ":" <<
name.substr(0, classifier_size_max) << "=" << db_opt->get() << ";";
return oss;
}
@@ -508,6 +551,128 @@ GncOptionDB::load_option_key_value(std::istream& iss)
return iss;
}
std::ostream&
GncOptionDB::save_to_key_value(std::ostream& oss) const noexcept
{
for (auto section : m_sections)
{
const auto& [s_name, s_vec] = section;
oss << "[Options]\n";
for (auto option : s_vec)
{
if (option.is_changed())
oss << section.first.substr(0, classifier_size_max) <<
':' << option.get_name().substr(0, classifier_size_max) <<
'=' << option << '\n';
}
}
return oss;
}
std::istream&
GncOptionDB::load_from_key_value(std::istream& iss)
{
if (iss.peek() == '[')
{
char buf[classifier_size_max];
iss.getline(buf, classifier_size_max);
if (strcmp(buf, "[Options]") != 0) // safe
throw std::runtime_error("Wrong secion header for options.");
}
// Otherwise assume we were sent here correctly:
while (iss.peek() != '[') //Indicates the start of the next file section
{
load_option_key_value(iss);
}
return iss;
}
void
GncOptionDB::save_to_kvp(QofBook* book, bool clear_options) const noexcept
{
if (clear_options)
qof_book_options_delete(book, nullptr);
for (auto section : m_sections)
{
const auto& [s_name, s_vec] = section;
for (auto option : s_vec)
if (option.is_changed())
{
// qof_book_set_option wants a GSList path. Let's avoid allocating and make one here.
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
GSList list_head{(void*)s_name.c_str(), &list_tail};
auto type{option.get_ui_type()};
if (type == GncOptionUIType::BOOLEAN)
{
auto val{option.get_value<bool>()};
auto kvp{new KvpValue(val ? "t" : "f")};
qof_book_set_option(book, kvp, &list_head);
}
else if (type > GncOptionUIType::DATE_FORMAT)
{
QofInstance* inst{QOF_INSTANCE(option.get_value<QofInstance*>())};
auto guid = guid_copy(qof_instance_get_guid(inst));
auto kvp{new KvpValue(guid)};
qof_book_set_option(book, kvp, &list_head);
}
else if (type == GncOptionUIType::NUMBER_RANGE)
{
auto kvp{new KvpValue(option.get_value<int64_t>())};
qof_book_set_option(book, kvp, &list_head);
}
else
{
auto kvp{new KvpValue{g_strdup(option.get_value<std::string>().c_str())}};
qof_book_set_option(book, kvp, &list_head);
}
}
}
}
void
GncOptionDB::load_from_kvp(QofBook* book) noexcept
{
for (auto section : m_sections)
{
const auto& [s_name, s_vec] = section;
for (auto option : s_vec)
{
/* qof_book_set_option wants a GSList path. Let's avoid allocating
* and make one here. */
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
GSList list_head{(void*)s_name.c_str(), &list_tail};
auto kvp = qof_book_get_option(book, &list_head);
if (!kvp)
continue;
switch (kvp->get_type())
{
case KvpValue::Type::INT64:
option.set_value(kvp->get<int64_t>());
break;
case KvpValue::Type::STRING:
{
auto str{kvp->get<const char*>()};
if (option.get_ui_type() == GncOptionUIType::BOOLEAN)
option.set_value(*str == 't' ? true : false);
else
option.set_value(str);
break;
}
case KvpValue::Type::GUID:
{
auto guid{kvp->get<GncGUID*>()};
option.set_value(qof_instance_from_guid(guid, option.get_ui_type()));
break;
}
default:
continue;
break;
}
}
}
}
GncOptionDBPtr
gnc_option_db_new(void)
{

View File

@@ -82,7 +82,7 @@ public:
}
// void set_selectable(const char* section, const char* name);
void make_internal(const char* section, const char* name);
void commit();
void commit() {};
std::optional<std::reference_wrapper<GncOptionSection>> find_section(const std::string& section);
std::optional<std::reference_wrapper<GncOption>> find_option(const std::string& section, const std::string& name) {
return static_cast<const GncOptionDB&>(*this).find_option(section, name);
@@ -92,17 +92,17 @@ public:
const char* options_prolog) const noexcept;
std::istream& load_from_scheme(std::istream& iss) noexcept;
std::ostream& save_to_key_value(std::ostream& oss) const noexcept;
std::istream& load_from_key_value(std::istream& iss) noexcept;
void save_to_kvp() const noexcept;
void load_from_kvp() noexcept;
std::istream& load_from_key_value(std::istream& iss);
void save_to_kvp(QofBook* book, bool clear_book) const noexcept;
void load_from_kvp(QofBook* book) noexcept;
std::ostream& save_option_scheme(std::ostream& oss,
const char* option_prolog,
const std::string& section,
const std::string& name) const noexcept;
std::istream& load_option_scheme(std::istream& iss);
std::ostream& save_option_key_value(std::ostream& oss,
const char* section,
const char* name) const noexcept;
const std::string& section,
const std::string& name) const noexcept;
std::istream& load_option_key_value(std::istream& iss);
private:
std::optional<std::reference_wrapper<GncOptionSection>> m_default_section;

View File

@@ -23,6 +23,13 @@
#include <gtest/gtest.h>
#include <gnc-optiondb.hpp>
#include <kvp-value.hpp>
extern "C"
{
#include <glib-2.0/glib.h>
#include <gnc-ui-util.h>
#include <gnc-session.h>
}
class GncOptionDBTest : public ::testing::Test
{
@@ -280,8 +287,34 @@ TEST_F(GncOptionDBUITest, test_option_value_from_ui)
class GncOptionDBIOTest : public ::testing::Test
{
protected:
GncOptionDBIOTest() : m_db{gnc_option_db_new()}
GncOptionDBIOTest() : m_book{gnc_get_current_book()}, m_root{gnc_account_create_root(m_book)}, m_db{gnc_option_db_new()}
{
auto create_account = [this](Account* parent, GNCAccountType type,
const char* name)->Account* {
auto account = xaccMallocAccount(this->m_book);
xaccAccountBeginEdit(account);
xaccAccountSetType(account, type);
xaccAccountSetName(account, name);
xaccAccountBeginEdit(parent);
gnc_account_append_child(parent, account);
xaccAccountCommitEdit(parent);
xaccAccountCommitEdit(account);
return account;
};
auto assets = create_account(m_root, ACCT_TYPE_ASSET, "Assets");
auto liabilities = create_account(m_root, ACCT_TYPE_LIABILITY, "Liabilities");
auto expenses = create_account(m_root, ACCT_TYPE_EXPENSE, "Expenses");
create_account(assets, ACCT_TYPE_BANK, "Bank");
auto broker = create_account(assets, ACCT_TYPE_ASSET, "Broker");
auto stocks = create_account(broker, ACCT_TYPE_STOCK, "Stocks");
auto aapl = create_account(stocks, ACCT_TYPE_STOCK, "AAPL");
create_account(stocks, ACCT_TYPE_STOCK, "MSFT");
auto hpe = create_account(stocks, ACCT_TYPE_STOCK, "HPE");
create_account(broker, ACCT_TYPE_BANK, "Cash Management");
create_account(expenses, ACCT_TYPE_EXPENSE, "Food");
create_account(expenses, ACCT_TYPE_EXPENSE, "Gas");
create_account(expenses, ACCT_TYPE_EXPENSE, "Rent");
gnc_register_string_option(m_db, "foo", "bar", "baz", "Phony Option",
std::string{"waldo"});
gnc_register_text_option(m_db, "foo", "sausage", "links",
@@ -290,8 +323,23 @@ protected:
std::string{""});
gnc_register_text_option(m_db, "qux", "garply", "fred",
"Phony Option", std::string{"waldo"});
gnc_register_date_interval_option(m_db, "pork", "garply", "first",
"Phony Date Option",
RelativeDatePeriod::START_CURRENT_QUARTER);
gnc_register_account_list_option(m_db, "quux", "xyzzy", "second",
"Phony AccountList Option",
{aapl, hpe});
}
~GncOptionDBIOTest()
{
xaccAccountBeginEdit(m_root);
xaccAccountDestroy(m_root); //It does the commit
gnc_clear_current_session();
}
QofBook* m_book;
Account* m_root;
GncOptionDBPtr m_db;
};
@@ -313,7 +361,7 @@ TEST_F(GncOptionDBIOTest, test_option_scheme_output)
"))) option))\n\n", oss.str().c_str());
}
TEST_F(GncOptionDBIOTest, test_option_scheme_input)
TEST_F(GncOptionDBIOTest, test_string_option_scheme_input)
{
const char* input{"(let ((option (gnc:lookup-option option\n"
" \"foo\"\n"
@@ -326,6 +374,84 @@ TEST_F(GncOptionDBIOTest, test_option_scheme_input)
EXPECT_STREQ("pepper", m_db->lookup_string_option("foo", "sausage").c_str());
}
TEST_F(GncOptionDBIOTest, test_date_interval_option_scheme_input)
{
const char* input{"(let ((option (gnc:lookup-option option\n"
" \"pork\"\n"
" \"garply\")))\n"
" ((lambda (o) (if o (gnc:option-set-value o "
"'(relative . end-prev-month)"
"))) option))\n\n"};
std::istringstream iss{input};
GDate month_end;
g_date_set_time_t(&month_end, time(nullptr));
g_date_subtract_months(&month_end, 1);
gnc_gdate_set_month_end(&month_end);
auto time1 = time64_from_gdate(&month_end, DayPart::end);
m_db->load_option_scheme(iss);
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get().get_value<time64>());
}
TEST_F(GncOptionDBIOTest, test_account_list_option_scheme_input)
{
auto acclist{gnc_account_list_from_types(m_book, {ACCT_TYPE_STOCK})};
auto hpe_guid{qof_instance_to_string(QOF_INSTANCE(acclist[3]))};
auto msft_guid{qof_instance_to_string(QOF_INSTANCE(acclist[2]))};
std::string input{"(let ((option (gnc:lookup-option option\n"
" \"quux\"\n"
" \"xyzzy\")))\n"
" ((lambda (o) (if o (gnc:option-set-value o '(\""};
input += hpe_guid + "\" \"";
input += msft_guid + "\")))) option))\n\n";
std::istringstream iss{input};
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[0]);
m_db->load_option_scheme(iss);
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[1]);
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>().size());
}
TEST_F(GncOptionDBIOTest, test_multiple_options_scheme_input)
{
auto acclist{gnc_account_list_from_types(m_book, {ACCT_TYPE_STOCK})};
auto hpe_guid{qof_instance_to_string(QOF_INSTANCE(acclist[3]))};
auto msft_guid{qof_instance_to_string(QOF_INSTANCE(acclist[2]))};
std::string input{";; Foo\n\n"
"(let ((option (gnc:lookup-option option\n"
" \"foo\"\n"
" \"sausage\")))\n"
" ((lambda (o) (if o (gnc:option-set-value o \"pepper\""
"))) option))\n\n"
";; Pork\n\n"
"(let ((option (gnc:lookup-option option\n"
" \"pork\"\n"
" \"garply\")))\n"
" ((lambda (o) (if o (gnc:option-set-value o "
"'(relative . end-prev-month)"
"))) option))\n\n"
";; Quux\n\n"
"(let ((option (gnc:lookup-option option\n"
" \"quux\"\n"
" \"xyzzy\")))\n"
" ((lambda (o) (if o (gnc:option-set-value o '(\""};
input += hpe_guid + "\" \"";
input += msft_guid + "\")))) option))\n\n";
std::istringstream iss{input};
GDate month_end;
g_date_set_time_t(&month_end, time(nullptr));
g_date_subtract_months(&month_end, 1);
gnc_gdate_set_month_end(&month_end);
auto time1 = time64_from_gdate(&month_end, DayPart::end);
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[0]);
m_db->load_from_scheme(iss);
EXPECT_STREQ("pepper", m_db->lookup_string_option("foo", "sausage").c_str());
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get().get_value<time64>());
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[1]);
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>().size());
}
TEST_F(GncOptionDBIOTest, test_option_key_value_output)
{
std::ostringstream oss;
@@ -345,3 +471,31 @@ TEST_F(GncOptionDBIOTest, test_option_key_value_input)
m_db->load_option_key_value(iss);
EXPECT_STREQ("pepper", m_db->lookup_string_option("foo", "sausage").c_str());
}
TEST_F(GncOptionDBIOTest, test_option_kvp_save)
{
m_db->save_to_kvp(m_book, false);
auto foo = "foo";
auto bar = "bar";
auto sausage = "sausage";
auto grault = "grault";
auto garply = "garply";
GSList foo_bar_tail{(void*)bar, nullptr};
GSList foo_bar_head{(void*)foo, &foo_bar_tail};
GSList foo_sausage_tail{(void*)sausage, nullptr};
GSList foo_sausage_head{(void*)foo, &foo_sausage_tail};
GSList qux_grault_tail{(void*)grault, nullptr};
GSList qux_grault_head{(void*)foo, &qux_grault_tail};
GSList qux_garply_tail{(void*)garply, nullptr};
GSList qux_garply_head{(void*)foo, &qux_grault_tail};
m_db->set_option("foo", "sausage", std::string{"pepper"});
m_db->save_to_kvp(m_book, true);
auto foo_bar = qof_book_get_option(m_book, &foo_bar_head);
auto foo_sausage = qof_book_get_option(m_book, &foo_sausage_head);
auto qux_garply = qof_book_get_option(m_book, &qux_garply_head);
auto qux_grault = qof_book_get_option(m_book, &qux_grault_head);
EXPECT_EQ(nullptr, foo_bar);
EXPECT_EQ(nullptr, qux_garply);
EXPECT_EQ(nullptr, qux_grault);
EXPECT_STREQ("pepper", foo_sausage->get<const char*>());
}