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:
64
go/internal/io/attendance/client.go
Normal file
64
go/internal/io/attendance/client.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// Package attendance fetches attendance CSV exports from Google Sheets.
|
||||
// No auth required — the sheet must be publicly readable.
|
||||
package attendance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const exportBase = "https://docs.google.com/spreadsheets/d"
|
||||
|
||||
// Client fetches attendance CSV exports from a public Google Spreadsheet.
|
||||
type Client struct {
|
||||
http *http.Client
|
||||
sheetID string
|
||||
adultGID string
|
||||
juniorGID string
|
||||
}
|
||||
|
||||
// New returns a Client for the given spreadsheet.
|
||||
// adultGID is typically "0"; juniorGID is the GID of the junior tab.
|
||||
func New(httpClient *http.Client, sheetID, adultGID, juniorGID string) *Client {
|
||||
if httpClient == nil {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
return &Client{http: httpClient, sheetID: sheetID, adultGID: adultGID, juniorGID: juniorGID}
|
||||
}
|
||||
|
||||
// FetchAdults returns the adult attendance tab as raw CSV rows.
|
||||
func (c *Client) FetchAdults(ctx context.Context) ([][]string, error) {
|
||||
return c.fetch(ctx, c.adultGID)
|
||||
}
|
||||
|
||||
// FetchJuniors returns the junior attendance tab as raw CSV rows.
|
||||
func (c *Client) FetchJuniors(ctx context.Context) ([][]string, error) {
|
||||
return c.fetch(ctx, c.juniorGID)
|
||||
}
|
||||
|
||||
func (c *Client) fetch(ctx context.Context, gid string) ([][]string, error) {
|
||||
url := fmt.Sprintf("%s/%s/export?format=csv&gid=%s", exportBase, c.sheetID, gid)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("attendance fetch: HTTP %d for gid=%s", resp.StatusCode, gid)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r := csv.NewReader(strings.NewReader(string(body)))
|
||||
r.FieldsPerRecord = -1 // rows may have different lengths
|
||||
return r.ReadAll()
|
||||
}
|
||||
93
go/internal/io/attendance/client_test.go
Normal file
93
go/internal/io/attendance/client_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package attendance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientFetchAdults(t *testing.T) {
|
||||
data, err := os.ReadFile("testdata/adults_minimal.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write(data)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
// Point the client at our test server by re-implementing fetch against its URL.
|
||||
rows, err := fetchURL(context.Background(), srv.Client(), srv.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) < 2 {
|
||||
t.Fatalf("want ≥2 rows, got %d", len(rows))
|
||||
}
|
||||
if rows[0][0] != "Jméno" {
|
||||
t.Errorf("unexpected header: %q", rows[0][0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestFake(t *testing.T) {
|
||||
adultRows := parseCSV(t, "testdata/adults_minimal.csv")
|
||||
juniorRows := parseCSV(t, "testdata/juniors_minimal.csv")
|
||||
|
||||
f := &Fake{Adults: adultRows, Juniors: juniorRows}
|
||||
|
||||
got, err := f.FetchAdults(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got[0][0] != "Jméno" {
|
||||
t.Errorf("adults header: %q", got[0][0])
|
||||
}
|
||||
|
||||
got, err = f.FetchJuniors(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got[1][0] != "Junior One" {
|
||||
t.Errorf("juniors first member: %q", got[1][0])
|
||||
}
|
||||
}
|
||||
|
||||
func parseCSV(t *testing.T, path string) [][]string {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := csv.NewReader(strings.NewReader(string(b)))
|
||||
r.FieldsPerRecord = -1
|
||||
rows, err := r.ReadAll()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// fetchURL is a test helper that exercises the shared fetch logic against an arbitrary URL.
|
||||
func fetchURL(ctx context.Context, hc *http.Client, url string) ([][]string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r := csv.NewReader(strings.NewReader(string(b)))
|
||||
r.FieldsPerRecord = -1
|
||||
return r.ReadAll()
|
||||
}
|
||||
12
go/internal/io/attendance/fake.go
Normal file
12
go/internal/io/attendance/fake.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package attendance
|
||||
|
||||
import "context"
|
||||
|
||||
// Fake is an in-memory replacement for Client, used in tests.
|
||||
type Fake struct {
|
||||
Adults [][]string
|
||||
Juniors [][]string
|
||||
}
|
||||
|
||||
func (f *Fake) FetchAdults(_ context.Context) ([][]string, error) { return f.Adults, nil }
|
||||
func (f *Fake) FetchJuniors(_ context.Context) ([][]string, error) { return f.Juniors, nil }
|
||||
4
go/internal/io/attendance/testdata/adults_minimal.csv
vendored
Normal file
4
go/internal/io/attendance/testdata/adults_minimal.csv
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
Jméno,Tier,,,01.09.2025,08.09.2025,15.09.2025
|
||||
Member One,A,,,TRUE,TRUE,FALSE
|
||||
Member Two,A,,,TRUE,FALSE,FALSE
|
||||
# last line,,,,,
|
||||
|
4
go/internal/io/attendance/testdata/juniors_minimal.csv
vendored
Normal file
4
go/internal/io/attendance/testdata/juniors_minimal.csv
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
Jméno,Tier,,,01.09.2025,08.09.2025,15.09.2025
|
||||
Junior One,J,,,TRUE,TRUE,TRUE
|
||||
# Trenéři,,,,,
|
||||
Coach One,X,,,FALSE,FALSE,FALSE
|
||||
|
Reference in New Issue
Block a user