summaryrefslogtreecommitdiff
path: root/src/item.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2010-06-12 15:42:02 -0400
committerJohn Wiegley <johnw@newartisans.com>2010-06-12 15:43:00 -0400
commitb1b4e2aadff5983d443d70c09ea86a41b015873f (patch)
tree2c40305c9f9841a4c3d453a4a5c49ec69056b4b2 /src/item.cc
parent0555e7f61ef14ec667f8ba611e60fd96133a9676 (diff)
downloadfork-ledger-b1b4e2aadff5983d443d70c09ea86a41b015873f.tar.gz
fork-ledger-b1b4e2aadff5983d443d70c09ea86a41b015873f.tar.bz2
fork-ledger-b1b4e2aadff5983d443d70c09ea86a41b015873f.zip
Add support for typed metadata
The metadata construct 'Key: Value' is now just a special case for 'Key:: "Value"'. Another after a :: in metadata setting is parsed as a full value expression and typed as such. For example: ; Key:: $400 + $500 ledger -l 'tag("Key") < $1000'
Diffstat (limited to 'src/item.cc')
-rw-r--r--src/item.cc30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/item.cc b/src/item.cc
index 1f42510c..f59c9e29 100644
--- a/src/item.cc
+++ b/src/item.cc
@@ -135,7 +135,9 @@ item_t::set_tag(const string& tag,
}
}
-void item_t::parse_tags(const char * p, bool overwrite_existing,
+void item_t::parse_tags(const char * p,
+ scope_t& scope,
+ bool overwrite_existing,
optional<date_t::year_type> current_year)
{
if (const char * b = std::strchr(p, '[')) {
@@ -164,14 +166,21 @@ void item_t::parse_tags(const char * p, bool overwrite_existing,
std::strcpy(buf.get(), p);
string tag;
+ bool by_value = false;
for (char * q = std::strtok(buf.get(), " \t");
q;
q = std::strtok(NULL, " \t")) {
const string::size_type len = std::strlen(q);
+ if (len < 2) continue;
if (! tag.empty()) {
- string_map::iterator i =
- set_tag(tag, string_value(string(p + (q - buf.get()))),
- overwrite_existing);
+ string_map::iterator i;
+ string field(p + (q - buf.get()));
+ if (by_value) {
+ bind_scope_t bound_scope(scope, *this);
+ i = set_tag(tag, expr_t(field).calc(bound_scope), overwrite_existing);
+ } else {
+ i = set_tag(tag, string_value(field), overwrite_existing);
+ }
(*i).second.second = true;
break;
}
@@ -184,12 +193,19 @@ void item_t::parse_tags(const char * p, bool overwrite_existing,
}
}
else if (q[len - 1] == ':') { // a metadata setting
- tag = string(q, len - 1);
+ int index = 1;
+ if (q[len - 2] == ':') {
+ by_value = true;
+ index = 2;
+ }
+ tag = string(q, len - index);
}
}
}
-void item_t::append_note(const char * p, bool overwrite_existing,
+void item_t::append_note(const char * p,
+ scope_t& scope,
+ bool overwrite_existing,
optional<date_t::year_type> current_year)
{
if (note) {
@@ -199,7 +215,7 @@ void item_t::append_note(const char * p, bool overwrite_existing,
note = p;
}
- parse_tags(p, overwrite_existing, current_year);
+ parse_tags(p, scope, overwrite_existing, current_year);
}
namespace {