feat(go): IO layer behind interfaces (M4)
All checks were successful
Deploy to K8s / deploy (push) Successful in 11s
All checks were successful
Deploy to K8s / deploy (push) Successful in 11s
- io/attendance: CSV-over-public-URL client + Fake for adult/junior tabs - io/drive: Drive v3 modifiedTime client + Fake - io/sheets: Sheets v4 client (GetValues/AppendValues/BatchUpdateValues/ WriteHeader/SortByDateColumn) + Fake with call-capture - io/cache: Drive-modifiedTime-gated FileCache; two TTL knobs; atomic writes; generic Get[T]; Python-compatible JSON format; Flush() - io/fio: Client interface backed by Fio REST API (apiClient) and HTML scraper (transparentClient); Fake; testdata fixtures - membership/sources: NewSources wires attendance CSV + Sheets + cache into LoadAdults/LoadJuniors/LoadTransactions/LoadExceptions; Czech month parsing + merged-month maps - banksync: SyncToSheets (SHA-256 dedup, optional sort) and InferPayments ([?] review prefix, dry-run) — tested with fakes - cmd/fuj: sync and infer subcommands wired; fees and reconcile use real NewSources; go.mod gains google.golang.org/api + x/net - gofumpt extra-rules applied across all packages; lint clean Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
124
go/internal/io/sheets/client.go
Normal file
124
go/internal/io/sheets/client.go
Normal file
@@ -0,0 +1,124 @@
|
||||
// Package sheets provides a typed wrapper around the Google Sheets v4 API.
|
||||
package sheets
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"google.golang.org/api/option"
|
||||
sheetsv4 "google.golang.org/api/sheets/v4"
|
||||
)
|
||||
|
||||
// ValueRange pairs an R1C1 range with its cell values, used for batchUpdate.
|
||||
type ValueRange struct {
|
||||
Range string // R1C1 notation, e.g. "R2C4:R2C6"
|
||||
Values [][]any // one sub-slice per row
|
||||
}
|
||||
|
||||
// Client wraps the Sheets v4 API with the operations needed by this project.
|
||||
type Client struct {
|
||||
svc *sheetsv4.Service
|
||||
}
|
||||
|
||||
// New builds a Client using a service-account credentials file.
|
||||
func New(ctx context.Context, credentialsPath string, _ time.Duration) (*Client, error) {
|
||||
svc, err := sheetsv4.NewService(ctx,
|
||||
option.WithCredentialsFile(credentialsPath), //nolint:staticcheck
|
||||
option.WithScopes(sheetsv4.SpreadsheetsScope),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Client{svc: svc}, nil
|
||||
}
|
||||
|
||||
// GetValues fetches a range from a spreadsheet with UNFORMATTED_VALUE rendering
|
||||
// (numbers as numbers, dates as serial floats — matching Python's behaviour).
|
||||
func (c *Client) GetValues(ctx context.Context, spreadsheetID, a1Range string) ([][]any, error) {
|
||||
resp, err := c.svc.Spreadsheets.Values.
|
||||
Get(spreadsheetID, a1Range).
|
||||
ValueRenderOption("UNFORMATTED_VALUE").
|
||||
Context(ctx).
|
||||
Do()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows := make([][]any, len(resp.Values))
|
||||
copy(rows, resp.Values)
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// AppendValues appends rows to the first empty row after a1Range.
|
||||
func (c *Client) AppendValues(ctx context.Context, spreadsheetID, a1Range string, rows [][]any) error {
|
||||
vals := make([][]any, len(rows))
|
||||
copy(vals, rows)
|
||||
_, err := c.svc.Spreadsheets.Values.
|
||||
Append(spreadsheetID, a1Range, &sheetsv4.ValueRange{Values: vals}).
|
||||
ValueInputOption("USER_ENTERED").
|
||||
Context(ctx).
|
||||
Do()
|
||||
return err
|
||||
}
|
||||
|
||||
// BatchUpdateValues writes multiple non-contiguous ranges in one API call.
|
||||
func (c *Client) BatchUpdateValues(ctx context.Context, spreadsheetID string, updates []ValueRange) error {
|
||||
data := make([]*sheetsv4.ValueRange, len(updates))
|
||||
for i, u := range updates {
|
||||
vals := make([][]any, len(u.Values))
|
||||
copy(vals, u.Values)
|
||||
data[i] = &sheetsv4.ValueRange{Range: u.Range, Values: vals}
|
||||
}
|
||||
_, err := c.svc.Spreadsheets.Values.
|
||||
BatchUpdate(spreadsheetID, &sheetsv4.BatchUpdateValuesRequest{
|
||||
ValueInputOption: "USER_ENTERED",
|
||||
Data: data,
|
||||
}).
|
||||
Context(ctx).
|
||||
Do()
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteHeader overwrites row 1 of the spreadsheet with the given labels.
|
||||
func (c *Client) WriteHeader(ctx context.Context, spreadsheetID string, labels []string) error {
|
||||
row := make([]any, len(labels))
|
||||
for i, l := range labels {
|
||||
row[i] = l
|
||||
}
|
||||
_, err := c.svc.Spreadsheets.Values.
|
||||
Update(spreadsheetID, "A1", &sheetsv4.ValueRange{Values: [][]any{row}}).
|
||||
ValueInputOption("USER_ENTERED").
|
||||
Context(ctx).
|
||||
Do()
|
||||
return err
|
||||
}
|
||||
|
||||
// SortByDateColumn sorts rows 2..10000 of the first sheet ascending by column A (Date).
|
||||
// Looks up the sheetId (gid) from spreadsheet metadata.
|
||||
func (c *Client) SortByDateColumn(ctx context.Context, spreadsheetID string) error {
|
||||
meta, err := c.svc.Spreadsheets.Get(spreadsheetID).Context(ctx).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("sheets: get spreadsheet: %w", err)
|
||||
}
|
||||
if len(meta.Sheets) == 0 {
|
||||
return fmt.Errorf("sheets: spreadsheet has no sheets")
|
||||
}
|
||||
sheetID := meta.Sheets[0].Properties.SheetId
|
||||
|
||||
_, err = c.svc.Spreadsheets.BatchUpdate(spreadsheetID, &sheetsv4.BatchUpdateSpreadsheetRequest{
|
||||
Requests: []*sheetsv4.Request{{
|
||||
SortRange: &sheetsv4.SortRangeRequest{
|
||||
Range: &sheetsv4.GridRange{
|
||||
SheetId: sheetID,
|
||||
StartRowIndex: 1,
|
||||
EndRowIndex: 10000,
|
||||
},
|
||||
SortSpecs: []*sheetsv4.SortSpec{{
|
||||
DimensionIndex: 0,
|
||||
SortOrder: "ASCENDING",
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}).Context(ctx).Do()
|
||||
return err
|
||||
}
|
||||
53
go/internal/io/sheets/fake.go
Normal file
53
go/internal/io/sheets/fake.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package sheets
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Fake is an in-memory replacement for Client used in tests.
|
||||
// Values maps a "<spreadsheetID>/<a1Range>" key to pre-seeded rows.
|
||||
type Fake struct {
|
||||
// Values maps "spreadsheetID/range" → rows returned by GetValues.
|
||||
Values map[string][][]any
|
||||
// Appended collects rows passed to AppendValues for assertion.
|
||||
Appended []AppendCall
|
||||
// BatchUpdated collects calls to BatchUpdateValues.
|
||||
BatchUpdated []BatchCall
|
||||
}
|
||||
|
||||
// AppendCall records one AppendValues invocation.
|
||||
type AppendCall struct {
|
||||
SpreadsheetID string
|
||||
Range string
|
||||
Rows [][]any
|
||||
}
|
||||
|
||||
// BatchCall records one BatchUpdateValues invocation.
|
||||
type BatchCall struct {
|
||||
SpreadsheetID string
|
||||
Updates []ValueRange
|
||||
}
|
||||
|
||||
func (f *Fake) GetValues(_ context.Context, spreadsheetID, a1Range string) ([][]any, error) {
|
||||
key := spreadsheetID + "/" + a1Range
|
||||
rows, ok := f.Values[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sheets fake: no seed for %q", key)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (f *Fake) AppendValues(_ context.Context, spreadsheetID, a1Range string, rows [][]any) error {
|
||||
f.Appended = append(f.Appended, AppendCall{SpreadsheetID: spreadsheetID, Range: a1Range, Rows: rows})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fake) BatchUpdateValues(_ context.Context, spreadsheetID string, updates []ValueRange) error {
|
||||
f.BatchUpdated = append(f.BatchUpdated, BatchCall{SpreadsheetID: spreadsheetID, Updates: updates})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fake) WriteHeader(_ context.Context, _ string, _ []string) error { return nil }
|
||||
|
||||
func (f *Fake) SortByDateColumn(_ context.Context, _ string) error { return nil }
|
||||
Reference in New Issue
Block a user