Skip to content

Commit

Permalink
Merge pull request #347 from zhang-accounting/fix/pad-directive-shoul…
Browse files Browse the repository at this point in the history
…d-be-remove-after-using

fix: pad info should be removed after being used
  • Loading branch information
Kilerd authored Jun 17, 2024
2 parents 1f3f940 + 1e0c9dc commit c7ffeec
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/beancount/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ log = { workspace = true }
chrono = { version = "0.4", features = ["serde"] }
bigdecimal = { workspace = true }
snailquote = "0.3"
latestmap = "0.1"
latestmap = "0.2"
once_cell = "1.19"

[dev-dependencies]
Expand Down
11 changes: 9 additions & 2 deletions extensions/beancount/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ impl DataType for Beancount {
}
BeancountOnlyDirective::Balance(balance) => {
let date = balance.date.naive_date();
let pad_account = pad_info.get_latest(&date).and_then(|it| it.get(&balance.account.content));

let latest_pad_info = pad_info.pop_latest(&date);
let pad_account = match latest_pad_info {
Some((pad_key, mut pad_map)) => {
let target_pad_account = pad_map.remove(&balance.account.content);
pad_info.insert(pad_key, pad_map);
target_pad_account
}
_ => None,
};
if let Some(pad_account) = pad_account {
// balance pad
ret.push(Spanned {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1970-01-01 commodity CNY

1970-01-01 open Assets:A CNY
1970-01-01 open Equity:OpenBalance CNY

2017-12-01 pad Assets:A Equity:OpenBalance

2017-12-01 balance Assets:A 0.10 CNY
2017-12-02 balance Assets:A 0.10 CNY
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"uri": "/api/store",
"validations": [
[
"$.data.errors.length()",
0
],
[
"$.data.transactions[*][?(@.sequence==1)].flag",
"BalancePad"
],
[
"$.data.transactions[*][?(@.sequence==2)].flag",
"BalanceCheck"
]
]
}
]

1 change: 1 addition & 0 deletions zhang-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ tower = "0.4"
mime = "0.3"
http-body-util = "0.1"
walkdir = "2"
tempfile = "3.8.0"
10 changes: 5 additions & 5 deletions zhang-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ async fn main() {

#[cfg(test)]
mod test {
use std::env::temp_dir;
use std::io::{stdout, Write};
use std::sync::Arc;

Expand All @@ -202,6 +201,7 @@ mod test {
use jsonpath_rust::JsonPathQuery;
use serde::Deserialize;
use serde_json::Value;
use tempfile::tempdir;
use tokio::sync::{mpsc, RwLock};
use tower::util::ServiceExt;
use zhang_core::ledger::Ledger;
Expand Down Expand Up @@ -239,7 +239,8 @@ mod test {
}
let original_test_source_folder = path.path();
pprintln!(" \x1b[0;32mIntegration Test\x1b[0;0m: {}", original_test_source_folder.display());
let test_temp_folder = temp_dir();
let tempdir = tempdir().unwrap();
let test_temp_folder = tempdir.path();

for entry in walkdir::WalkDir::new(&original_test_source_folder).into_iter().filter_map(|e| e.ok()) {
if entry.path().eq(&original_test_source_folder) {
Expand All @@ -266,11 +267,10 @@ mod test {
pprintln!(" \x1b[0;32mTesting\x1b[0;0m: {}", &validation.uri);

let is_zhang_test = test_temp_folder.join("main.zhang").exists();

let data_source = OpendalDataSource::from_env(
FileSystem::Fs,
&mut ServerOpts {
path: test_temp_folder.clone(),
path: test_temp_folder.to_path_buf(),
endpoint: if is_zhang_test { "main.zhang".to_owned() } else { "main.bean".to_owned() },
addr: "".to_string(),
port: 0,
Expand All @@ -282,7 +282,7 @@ mod test {
.await;
let data_source = Arc::new(data_source);
let ledger = Ledger::async_load(
test_temp_folder.clone(),
test_temp_folder.to_path_buf(),
if is_zhang_test { "main.zhang".to_owned() } else { "main.bean".to_owned() },
data_source.clone(),
)
Expand Down

0 comments on commit c7ffeec

Please sign in to comment.