better domain isolation
Generate check / check-changes (pull_request) Successful in 26s
Generate check / verify-generate (pull_request) Skipped
Quality / check-changes (pull_request) Successful in 2s
Quality / static-checks (pull_request) Failing after 16s
Quality / run-tests (pull_request) Failing after 56s

This commit is contained in:
natercio committed 2026-10-05 14:56:27 +01:00
1 parent ebb471524e
commit 3dfc7bd6a7
10 files changed
+342 -242

No files matched your search

+11 -132
View File
@@ -1,155 +1,34 @@
// Package table renders an internal.AggregatorWriter as the human-readable
// Anexo J report table, for the terminal (Render) and for embedding in HTML
// documents (RenderHTML).
// Anexo J report table for the terminal. The shared table definition it (and
// the html package) renders from lives in internal/writing/internal/tabledef.
package table
import (
"fmt"
"io"
"github.com/biter777/countries"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/nmoniz/any2anexoj/internal"
"github.com/nmoniz/any2anexoj/internal/writing/internal/tabledef"
)
// translator is the contract the table printer relies on for human-readable
// column labels. *Localizer satisfies it.
type translator interface {
Translate(key string, count int, values map[string]any) string
}
// Printer writes a simple, human readable, table row to the provided io.Writer for each
// ReportItem received. Render and RenderHTML each build a fresh table.Writer so that
// invoking them (in any order or repeatedly) on the same instance never duplicates rows.
// Printer writes a simple, human readable, table to the provided io.Writer
// for the report items in an AggregatorWriter. Each Render call builds a
// fresh table from the aggregator, so invoking it repeatedly on the same
// instance never duplicates rows.
type Printer struct {
output io.Writer
translator translator
translator tabledef.Translator
}
func NewPrinter(w io.Writer, tr translator) *Printer {
func NewPrinter(w io.Writer, tr tabledef.Translator) *Printer {
return &Printer{
output: w,
translator: tr,
}
}
// buildTable constructs a fully configured table.Writer from the items in aw.
// Each call returns a new table.Writer so callers can render the same data
// multiple times without duplicating rows. The returned table is not wired to
// pp.output via an output mirror; callers that want the rendered output
// written to pp.output must set the mirror themselves before calling .Render().
func (pp *Printer) buildTable(aw *internal.AggregatorWriter) table.Writer {
tw := table.NewWriter()
tw.SetAutoIndex(true)
tw.SetStyle(table.StyleLight)
tw.SetColumnConfigs([]table.ColumnConfig{
colCountry(1),
colOther(2),
colOther(3),
colOther(4),
colOther(5),
colEuros(6),
colOther(7),
colOther(8),
colOther(9),
colEuros(10),
colEuros(11),
colEuros(12),
colCountry(13),
})
realizationTxt := pp.translator.Translate("realization", 1, nil)
acquisitionTxt := pp.translator.Translate("acquisition", 1, nil)
yearTxt := pp.translator.Translate("year", 1, nil)
monthTxt := pp.translator.Translate("month", 1, nil)
dayTxt := pp.translator.Translate("day", 1, nil)
valorTxt := pp.translator.Translate("value", 1, nil)
tw.AppendHeader(table.Row{"", "", realizationTxt, realizationTxt, realizationTxt, realizationTxt, acquisitionTxt, acquisitionTxt, acquisitionTxt, acquisitionTxt, "", "", ""}, table.RowConfig{AutoMerge: true})
tw.AppendHeader(table.Row{
pp.translator.Translate("source_country", 1, nil), pp.translator.Translate("code", 1, nil),
yearTxt, monthTxt, dayTxt, valorTxt,
yearTxt, monthTxt, dayTxt, valorTxt,
pp.translator.Translate("expenses", 2, nil), pp.translator.Translate("foreign_tax_paid", 1, nil), pp.translator.Translate("counter_country", 1, nil),
})
for ri := range aw.Iter() {
tw.AppendRow(table.Row{
ri.AssetCountry, ri.Nature,
ri.SellTimestamp.Year(), int(ri.SellTimestamp.Month()), ri.SellTimestamp.Day(), ri.SellValue.StringFixed(2),
ri.BuyTimestamp.Year(), int(ri.BuyTimestamp.Month()), ri.BuyTimestamp.Day(), ri.BuyValue.StringFixed(2),
ri.Fees.StringFixed(2), ri.Taxes.StringFixed(2), ri.BrokerCountry,
})
}
sumTxt := pp.translator.Translate("sum", 1, nil)
// The footer lists one cell per column (13) so it never relies on
// go-pretty's implicit short-row padding; the trailing empty cell is the
// counter-country column, which has no total. Totals are formatted with
// StringFixed(2) so they match the two-decimal data rows.
tw.AppendFooter(table.Row{sumTxt, sumTxt, sumTxt, sumTxt, sumTxt, aw.TotalEarned().StringFixed(2), "", "", "", aw.TotalSpent().StringFixed(2), aw.TotalFees().StringFixed(2), aw.TotalTaxes().StringFixed(2), ""}, table.RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
return tw
}
// Render writes the terminal table for aw to the configured output.
func (pp *Printer) Render(aw *internal.AggregatorWriter) {
t := pp.buildTable(aw)
t := tabledef.Build(pp.translator, aw)
t.SetOutputMirror(pp.output)
t.Render()
}
// RenderHTML builds a fresh table from aw and renders it as an HTML string
// using go-pretty's HTMLOptions.
func (pp *Printer) RenderHTML(aw *internal.AggregatorWriter) string {
t := pp.buildTable(aw)
t.Style().HTML = table.HTMLOptions{
CSSClass: "table table-striped table-hover table-bordered",
EscapeText: true,
Newline: "<br/>",
EmptyColumn: "&nbsp;",
}
return t.RenderHTML()
}
func colEuros(n int) table.ColumnConfig {
return table.ColumnConfig{
Number: n,
Align: text.AlignRight,
AlignFooter: text.AlignRight,
AlignHeader: text.AlignRight,
WidthMin: 12,
WidthMax: 15,
Transformer: func(val any) string {
return fmt.Sprintf("%v €", val)
},
TransformerFooter: func(val any) string {
return fmt.Sprintf("%v €", val)
},
}
}
func colOther(n int) table.ColumnConfig {
return table.ColumnConfig{
Number: n,
Align: text.AlignLeft,
AlignFooter: text.AlignLeft,
AlignHeader: text.AlignLeft,
WidthMax: 12,
}
}
func colCountry(n int) table.ColumnConfig {
return table.ColumnConfig{
Number: n,
Align: text.AlignLeft,
AlignFooter: text.AlignLeft,
AlignHeader: text.AlignLeft,
WidthMax: 24,
WidthMaxEnforcer: text.Trim,
Transformer: func(val any) string {
countryCode := val.(int64)
return fmt.Sprintf("%v - %s", val, countries.ByNumeric(int(countryCode)).Info().Name)
},
}
}
+1 -71
View File
@@ -2,7 +2,6 @@ package table_test
import (
"bytes"
"strings"
"testing"
"time"
@@ -69,8 +68,7 @@ func TestPrinter_Render(t *testing.T) {
// Expected output
want := `┌───┬────────────────────────────┬───────────────────────────────────┬───────────────────────────────────┬──────────────────────────────────────────────────────────┐
│ │ │ REALIZATION │ ACQUISITION │ │
│ │ SOURCE COUNTRY │ CODE │ YEAR │ MONTH │ DAY │ VALUE │ YEAR │ MONTH │ DAY │ VALUE │ EXPENSES AND CH │ TAX PAID ABROAD │ COUNTER COUNTRY │
│ │ │ │ │ │ │ │ │ │ │ │ ARGES │ │ │
│ │ SOURCE COUNTRY │ CODE │ YEAR │ MONTH │ DAY │ VALUE │ YEAR │ MONTH │ DAY │ VALUE │ EXPENSES AND C… │ TAX PAID ABROAD │ COUNTER COUNTRY │
├───┼─────────────────────┼──────┼──────┼───────┼─────┼──────────────┼──────┼───────┼─────┼──────────────┼─────────────────┼─────────────────┼──────────────────────┤
│ 1 │ 840 - United States │ G01 │ 2023 │ 6 │ 20 │ 150.75 € │ 2023 │ 1 │ 15 │ 100.50 € │ 2.50 € │ 5.00 € │ 826 - United Kingdom │
│ 2 │ 840 - United States │ G20 │ 2023 │ 9 │ 5 │ 225.50 € │ 2023 │ 3 │ 10 │ 200.00 € │ 3.00 € │ 7.50 € │ 826 - United Kingdom │
@@ -84,71 +82,3 @@ func TestPrinter_Render(t *testing.T) {
t.Errorf("Printer.Render() output doesn't match expected.\n\nGot:\n%s\n\nWant:\n%s", got, want)
}
}
func TestRenderHTML(t *testing.T) {
// Create test data
aw := internal.NewAggregatorWriter()
ctx := t.Context()
err := aw.Write(ctx, internal.ReportItem{
Symbol: "AAPL",
Nature: internal.NatureG01,
BrokerCountry: 826, // United Kingdom
AssetCountry: 840, // United States
BuyValue: decimal.NewFromFloat(100.50),
BuyTimestamp: time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC),
SellValue: decimal.NewFromFloat(150.75),
SellTimestamp: time.Date(2023, 6, 20, 0, 0, 0, 0, time.UTC),
Fees: decimal.NewFromFloat(2.50),
Taxes: decimal.NewFromFloat(5.00),
})
if err != nil {
t.Fatalf("failed to write report item: %v", err)
}
// Create Portuguese localizer so the header text is in pt
localizer, err := writing.NewLocalizer("pt")
if err != nil {
t.Fatalf("failed to create localizer: %v", err)
}
// Output buffer used only to satisfy the constructor contract; RenderHTML
// must not write to it.
var buf bytes.Buffer
pp := table.NewPrinter(&buf, localizer)
html := pp.RenderHTML(aw)
// RenderHTML must not side-write to pp.output.
if buf.Len() != 0 {
t.Errorf("RenderHTML should not write to pp.output, but got %d bytes: %q", buf.Len(), buf.String())
}
// Structural assertions.
wantSubstrings := []string{
`<table class="table table-striped table-hover table-bordered">`,
`</table>`,
`<thead>`,
`</thead>`,
`<tbody>`,
`</tbody>`,
`<tfoot>`,
`</tfoot>`,
"Realização",
"Aquisição",
"Soma",
}
for _, s := range wantSubstrings {
if !strings.Contains(html, s) {
t.Errorf("RenderHTML output missing expected substring %q\nGot:\n%s", s, html)
}
}
// Calling RenderHTML twice on the same instance must not duplicate rows.
html2 := pp.RenderHTML(aw)
if strings.Count(html2, "<tr") != strings.Count(html, "<tr") {
t.Errorf("expected second RenderHTML call to produce the same number of rows as the first\nFirst (%d <tr>):\n%s\nSecond (%d <tr>):\n%s",
strings.Count(html, "<tr"), html,
strings.Count(html2, "<tr"), html2)
}
}