Skip to content

Commit bc93145

Browse files
rustyrussellsangbida
authored andcommitted
bkpr-report: make ? work as expected on amount fields.
They should test if it's zero, not if it's present. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 48271d5 commit bc93145

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

contrib/msggen/msggen/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5162,7 +5162,7 @@
51625162
"",
51635163
"If a field is unavailable, it expands to an empty string.",
51645164
"",
5165-
"You can provide fallback with ?, including more variable:",
5165+
"You can provide fallback with ?, which is used when the tag is not present (or zero, for credit, debit, fees and creditdebit). This fallback can include more tags:",
51665166
" * {outpoint?NONE}",
51675167
" * {payment_id?txid: {txid?UNKNOWN}}",
51685168
"The first one the outpoint, or NONE if that is not available. ",

doc/schemas/bkpr-report.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"",
4949
"If a field is unavailable, it expands to an empty string.",
5050
"",
51-
"You can provide fallback with ?, including more variable:",
51+
"You can provide fallback with ?, which is used when the tag is not present (or zero, for credit, debit, fees and creditdebit). This fallback can include more tags:",
5252
" * {outpoint?NONE}",
5353
" * {payment_id?txid: {txid?UNKNOWN}}",
5454
"The first one the outpoint, or NONE if that is not available. ",

plugins/bkpr/report.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <plugins/bkpr/report.h>
1616
#include <plugins/libplugin.h>
1717

18+
/* This is a zero, but treated specially if tested with ? */
19+
static const char ZERO_AMOUNT[] = "0";
20+
1821
static const char *report_fmt_acct_name(const tal_t *ctx UNNEEDED,
1922
const struct bkpr *bkpr UNNEEDED,
2023
const struct income_event *e)
@@ -40,20 +43,26 @@ static const char *report_fmt_credit(const tal_t *ctx,
4043
const struct bkpr *bkpr UNNEEDED,
4144
const struct income_event *e)
4245
{
46+
if (amount_msat_is_zero(e->credit))
47+
return ZERO_AMOUNT;
4348
return fmt_amount_msat_btc(ctx, e->credit, false);
4449
}
4550

4651
static const char *report_fmt_debit(const tal_t *ctx,
4752
const struct bkpr *bkpr UNNEEDED,
4853
const struct income_event *e)
4954
{
55+
if (amount_msat_is_zero(e->debit))
56+
return ZERO_AMOUNT;
5057
return fmt_amount_msat_btc(ctx, e->debit, false);
5158
}
5259

5360
static const char *report_fmt_fees(const tal_t *ctx,
5461
const struct bkpr *bkpr UNNEEDED,
5562
const struct income_event *e)
5663
{
64+
if (amount_msat_is_zero(e->fees))
65+
return ZERO_AMOUNT;
5766
return fmt_amount_msat_btc(ctx, e->fees, false);
5867
}
5968

@@ -134,7 +143,7 @@ static const char *report_fmt_credit_debit(const tal_t *ctx,
134143
if (!amount_msat_is_zero(e->debit))
135144
return tal_fmt(ctx, "-%s",
136145
fmt_amount_msat_btc(tmpctx, e->debit, false));
137-
return "0";
146+
return ZERO_AMOUNT;
138147
}
139148

140149
static const char *report_fmt_currency_credit(const tal_t *ctx,
@@ -411,6 +420,10 @@ static char *format_event(const tal_t *ctx,
411420
}
412421

413422
v = fmt->fmt[i](tmpctx, bkpr, e);
423+
/* If there's an alternative, we treat ZERO_AMOUNT as missing. */
424+
if (v == ZERO_AMOUNT && fmt->alt[i])
425+
v = NULL;
426+
414427
if (v) {
415428
v = escape_value(tmpctx, v, esc);
416429
out = tal_strcat(ctx, take(out), v);

tests/test_bookkeeper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,22 +1275,24 @@ def test_bkpr_report_tags_and_fallback(node_factory):
12751275
assert any(r[2] == "NONE" or r[3] == "NONE" or r[4] == "NONE" for r in rows)
12761276

12771277
# Fancier fields should work, too
1278-
res = l1.rpc.bkpr_report(format="{tag}|{account}|{credit}|{debit}|{creditdebit}|{currencycredit}|{currencydebit}|{currencycreditdebit}")
1278+
res = l1.rpc.bkpr_report(format="{tag}|{account}|{credit}|{debit}|{creditdebit}|{currencycredit}|{currencydebit}|{currencycreditdebit}|{credit?NONE}")
12791279
rows = [line.split("|") for line in res["report"]]
12801280

12811281
for r in rows:
1282-
assert len(r) == 8
1282+
assert len(r) == 9
12831283
# Credit or debit?
12841284
if float(r[2]) > 0:
12851285
assert r[4] == '+' + r[2]
12861286
assert float(r[5]) > 0
12871287
assert r[6] == '0.00'
12881288
assert r[7] == '+' + r[5]
1289+
assert r[8] == r[2]
12891290
else:
12901291
assert r[4] == '-' + r[3]
12911292
assert float(r[6]) > 0
12921293
assert r[5] == '0.00'
12931294
assert r[7] == '-' + r[6]
1295+
assert r[8] == 'NONE'
12941296

12951297

12961298
def test_bkpr_report_invoice(node_factory):

0 commit comments

Comments
 (0)