/
smbc_card.go
71 lines (57 loc) · 1.86 KB
/
smbc_card.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package handlers
import (
"context"
"regexp"
"time"
"go.nownabe.dev/bqloader"
"golang.org/x/text/encoding/japanese"
"golang.org/x/xerrors"
)
// SMBCCardStatement build a *bqloader.Handler for statements of SMBC card (三井住友VISAカード).
// To add column of payment month, keep the file name when you downloaded it.
func SMBCCardStatement(name, pattern string, table Table, notifier bqloader.Notifier) *bqloader.Handler {
var monthKey contextKey = "month"
re := regexp.MustCompile(`/(\d+)\.csv`)
preprocessor := func(ctx context.Context, e bqloader.Event) (context.Context, error) {
match := re.FindStringSubmatch(e.Name)
if len(match) < 2 {
return ctx, xerrors.Errorf("wrong object path: %s", e.Name)
}
month, err := time.Parse("200601", match[1])
if err != nil {
return ctx, xerrors.Errorf("failed to parse payment month from object path: %s: %w", match[1], err)
}
return context.WithValue(ctx, monthKey, month.Format("2006-01-02")), nil
}
projector := func(ctx context.Context, r []string) ([]string, error) {
if r[0] == "" {
return nil, nil
}
paymentMonth, ok := ctx.Value(monthKey).(string)
if !ok {
return nil, xerrors.Errorf("failed to get payment month from context: %v", paymentMonth)
}
// 0: date (ご利用日)
t, err := time.Parse("2006/01/02", r[0])
if err != nil {
return nil, xerrors.Errorf("failed to parse date: %v", err)
}
r[0] = t.Format("2006-01-02")
// 7: payment_month (支払い月)
r = append(r, paymentMonth)
return r, nil
}
return &bqloader.Handler{
Name: name,
Pattern: regexp.MustCompile(pattern),
SkipLeadingRows: 0,
Encoding: japanese.ShiftJIS,
Parser: PartialCSVParser(1, 0, "\r\n"),
Projector: projector,
Preprocessor: preprocessor,
Notifier: notifier,
Project: table.Project,
Dataset: table.Dataset,
Table: table.Table,
}
}