Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

367 tags come after link would be parse #382

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/build-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ jobs:
path: cobertura.xml
formatter:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
Expand All @@ -130,7 +129,6 @@ jobs:

clippy-check:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
Expand Down
8 changes: 5 additions & 3 deletions extensions/beancount/src/beancount.pest
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ valuable_comment = { space* ~ comment_prefix ~ space* ~ comment_value }
comment_prefix = { ";" | "*" | "#" | "//" }
comment_value = { (!line ~ ANY)* }

transaction = { date ~ transaction_flag? ~ (space+ ~ quote_string){0, 2} ~ tags? ~ links? ~ space* ~ comment? ~ transaction_detail }
transaction = { date ~ transaction_flag? ~ (space+ ~ quote_string){0, 2} ~ tags_or_links? ~ space* ~ comment? ~ transaction_detail }

transaction_flag = { space+ ~ ("!" | "*" | "#" | ASCII_ALPHA_UPPER) }
tags = { (space* ~ tag)* }

tags_or_links = { (space* ~ (tag | link))* }
tag = { "#" ~ unquote_string }
links = { (space* ~ link)* }
link = { "^" ~ unquote_string }

transaction_detail = _{ indentation_push ~ transaction_lines ~ DROP }
transaction_lines = { transaction_line ~ (transaction_next_line)* }
transaction_line = { (transaction_posting | key_value_line) ~ space* ~ valuable_comment? }
Expand Down
49 changes: 28 additions & 21 deletions extensions/beancount/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,25 @@ impl BeancountParser {
);
Ok(ret)
}
fn tags(input: Node) -> Result<Vec<String>> {
let ret = match_nodes!(input.into_children();
[tag(tags)..] => tags.collect(),
);
Ok(ret)
}
fn links(input: Node) -> Result<Vec<String>> {
let ret = match_nodes!(input.into_children();
[link(links)..] => links.collect(),
);
Ok(ret)
fn tags_or_links(input: Node) -> Result<(Vec<String>, Vec<String>)> {
let mut tags = vec![];
let mut links = vec![];
let nodes = input.into_children();
for node in nodes {
match node.as_rule() {
Rule::tag => {
tags.push(Self::tag(node)?);
}
Rule::link => {
links.push(Self::link(node)?);
}
_ => {
// Optionally handle unexpected rules
}
}
}

Ok((tags, links))
}

fn transaction(input: Node) -> Result<Directive> {
Expand All @@ -358,28 +366,27 @@ impl BeancountParser {
Option<Flag>,
Option<ZhangString>,
Option<ZhangString>,
Vec<String>,
Vec<String>,
(Vec<String>, Vec<String>),
Vec<(Option<Posting>, Option<(String, ZhangString)>)>,
) = match_nodes!(input.into_children();
[date(date), quote_string(payee), tags(tags), links(links), transaction_lines(lines)] => (date, None, Some(payee), None, tags, links,lines),
[date(date), quote_string(payee), quote_string(narration), tags(tags), links(links), transaction_lines(lines)] => (date, None, Some(payee), Some(narration), tags, links,lines),
[date(date), transaction_flag(flag), tags(tags), links(links), transaction_lines(lines)] => (date, flag, None, None, tags, links, lines),
[date(date), transaction_flag(flag), quote_string(narration), tags(tags), links(links), transaction_lines(lines)] => (date, flag, None, Some(narration), tags, links, lines),
[date(date), transaction_flag(flag), quote_string(payee), quote_string(narration), tags(tags), links(links), transaction_lines(lines)] => (date, flag, Some(payee), Some(narration), tags, links,lines),
[date(date), quote_string(payee), tags_or_links(tags_or_links), transaction_lines(lines)] => (date, None, Some(payee), None, tags_or_links,lines),
[date(date), quote_string(payee), quote_string(narration), tags_or_links(tags_or_links), transaction_lines(lines)] => (date, None, Some(payee), Some(narration), tags_or_links,lines),
[date(date), transaction_flag(flag), tags_or_links(tags_or_links), transaction_lines(lines)] => (date, flag, None, None, tags_or_links, lines),
[date(date), transaction_flag(flag), quote_string(narration), tags_or_links(tags_or_links), transaction_lines(lines)] => (date, flag, None, Some(narration), tags_or_links, lines),
[date(date), transaction_flag(flag), quote_string(payee), quote_string(narration), tags_or_links(tags_or_links), transaction_lines(lines)] => (date, flag, Some(payee), Some(narration), tags_or_links,lines),
);
let mut transaction = Transaction {
date: ret.0,
flag: ret.1,
payee: ret.2,
narration: ret.3,
tags: ret.4.into_iter().collect(),
links: ret.5.into_iter().collect(),
tags: ret.4 .0.into_iter().collect(),
links: ret.4 .1.into_iter().collect(),
postings: vec![],
meta: MultiValueMap::default(),
};

for line in ret.6 {
for line in ret.5 {
match line {
(Some(trx), None) => {
transaction.postings.push(trx);
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/random-order-of-links-and-tags/main.bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
option "operating_currency" "CNY"

1970-01-01 open Assets:BankCard CNY

1970-01-01 open Expenses:Food CNY

2023-12-06 "KFC" "VME50 Package" #tag1 ^link1
Assets:BankCard -50 CNY
Expenses:Food

2023-12-05 "KFC" "VME50 Package" ^link1 #tag1
Assets:BankCard -50 CNY
Expenses:Food

2023-12-04 "KFC" "VME50 Package" #tag1 #tag2 ^link1 ^link2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-03 "KFC" "VME50 Package" ^link1 ^link2 #tag1 #tag2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-02 "KFC" "VME50 Package" ^link1 #tag1 ^link2 #tag2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-01 "KFC" "VME50 Package" #tag1 ^link1 #tag2 ^link2
Assets:BankCard -50 CNY
Expenses:Food
29 changes: 29 additions & 0 deletions integration-tests/random-order-of-links-and-tags/main.zhang
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
option "operating_currency" "CNY"

1970-01-01 open Assets:BankCard CNY

1970-01-01 open Expenses:Food CNY

2023-12-06 "KFC" "VME50 Package" #tag1 ^link1
Assets:BankCard -50 CNY
Expenses:Food

2023-12-05 "KFC" "VME50 Package" ^link1 #tag1
Assets:BankCard -50 CNY
Expenses:Food

2023-12-04 "KFC" "VME50 Package" #tag1 #tag2 ^link1 ^link2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-03 "KFC" "VME50 Package" ^link1 ^link2 #tag1 #tag2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-02 "KFC" "VME50 Package" ^link1 #tag1 ^link2 #tag2
Assets:BankCard -50 CNY
Expenses:Food

2023-12-01 "KFC" "VME50 Package" #tag1 ^link1 #tag2 ^link2
Assets:BankCard -50 CNY
Expenses:Food
87 changes: 87 additions & 0 deletions integration-tests/random-order-of-links-and-tags/validations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[
{
"uri": "/api/journals",
"validations": [
[
"$.data.records[0].links",
[
"link1"
]
],
[
"$.data.records[0].tags",
[
"tag1"
]
],
[
"$.data.records[1].links",
[
"link1"
]
],
[
"$.data.records[1].tags",
[
"tag1"
]
],
[
"$.data.records[2].links",
[
"link1",
"link2"
]
],
[
"$.data.records[2].tags",
[
"tag1",
"tag2"
]
],
[
"$.data.records[3].links",
[
"link1",
"link2"
]
],
[
"$.data.records[3].tags",
[
"tag1",
"tag2"
]
],
[
"$.data.records[4].links",
[
"link1",
"link2"
]
],
[
"$.data.records[4].tags",
[
"tag1",
"tag2"
]
],
[
"$.data.records[5].links",
[
"link1",
"link2"
]
],
[
"$.data.records[5].tags",
[
"tag1",
"tag2"
]
]
]
}
]
126 changes: 64 additions & 62 deletions zhang-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,74 +266,76 @@ mod test {
for validation in validations {
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.to_path_buf(),
endpoint: if is_zhang_test { "main.zhang".to_owned() } else { "main.bean".to_owned() },
addr: "".to_string(),
port: 0,
auth: None,
source: None,
no_report: false,
},
)
.await;
let data_source = Arc::new(data_source);
let ledger = Ledger::async_load(
test_temp_folder.to_path_buf(),
if is_zhang_test { "main.zhang".to_owned() } else { "main.bean".to_owned() },
data_source.clone(),
)
.await
.expect("cannot load ledger");
let ledger_data = Arc::new(RwLock::new(ledger));
let broadcaster = Broadcaster::create();
let (tx, _) = mpsc::channel(1);
let reload_sender = Arc::new(ReloadSender(tx));
let app = create_server_app(ledger_data, broadcaster, reload_sender, None);

let response = app
.oneshot(
Request::builder()
.method(http::Method::GET)
.uri(&validation.uri)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::empty())
.unwrap(),
for main_file in ["main.zhang", "main.bean"] {
let main_file_exists = test_temp_folder.join(main_file).exists();
if !main_file_exists {
continue;
}
pprintln!(" \x1b[0;32mDetected main file\x1b[0;0m: {}", &main_file);
let data_source = OpendalDataSource::from_env(
FileSystem::Fs,
&mut ServerOpts {
path: test_temp_folder.to_path_buf(),
endpoint: main_file.to_string(),
addr: "".to_string(),
port: 0,
auth: None,
source: None,
no_report: false,
},
)
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let res: Value = serde_json::from_slice(&body).unwrap();

for point in validation.validations {
pprintln!(
" \x1b[0;32mValidating\x1b[0;0m: \x1b[0;34m{}\x1b[0;0m to be \x1b[0;34m{}\x1b[0;0m",
point.0,
&point.1
);

let value = res.clone().path(&point.0).unwrap();
let expected_value = Value::Array(vec![point.1]);
if !expected_value.eq(&value) {
panic!(
"Validation fail\n\
.await;
let data_source = Arc::new(data_source);
let ledger = Ledger::async_load(test_temp_folder.to_path_buf(), main_file.to_string(), data_source.clone())
.await
.expect("cannot load ledger");
let ledger_data = Arc::new(RwLock::new(ledger));
let broadcaster = Broadcaster::create();
let (tx, _) = mpsc::channel(1);
let reload_sender = Arc::new(ReloadSender(tx));
let app = create_server_app(ledger_data, broadcaster, reload_sender, None);

let response = app
.oneshot(
Request::builder()
.method(http::Method::GET)
.uri(&validation.uri)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let res: Value = serde_json::from_slice(&body).unwrap();

for point in validation.validations.iter() {
pprintln!(
" \x1b[0;32mValidating\x1b[0;0m: \x1b[0;34m{}\x1b[0;0m to be \x1b[0;34m{}\x1b[0;0m",
point.0,
&point.1
);

let value = res.clone().path(&point.0).unwrap();
let expected_value = Value::Array(vec![point.1.clone()]);
if !expected_value.eq(&value) {
panic!(
"Validation fail\n\
Test case: {} \n\
Test URL: {} \n\
Test rule: {} \n\
Excepted value: {} \n\
Get: {}",
original_test_source_folder.display(),
&validation.uri,
point.0,
&expected_value,
&value
);
original_test_source_folder.display(),
&validation.uri,
point.0,
&expected_value,
&value
);
}
}
}
}
Expand Down
Loading
Loading