-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
138 lines (111 loc) · 3.39 KB
/
Copy pathexample_test.go
File metadata and controls
138 lines (111 loc) · 3.39 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pgfmt_test
import (
"encoding/csv"
"fmt"
"log/slog"
"os"
"time"
"github.com/jackc/pgx/v5"
"github.com/mdigger/pgfmt"
)
// This example demonstrates how to use the high-level Write function
// for a quick and standard export using default settings.
func ExampleWrite() {
// Assume rows is a valid pgx.Rows object from a query
var rows pgx.Rows
w := csv.NewWriter(os.Stdout)
// Set global preferences if needed
pgfmt.DefaultConfig.Null = "N/A"
pgfmt.DefaultConfig.DateTimeFormat = time.Stamp
// The destination writer comes first, followed by the database rows.
err := pgfmt.Write(w, rows)
if err != nil {
fmt.Fprintf(os.Stderr, "Export failed: %v\n", err)
}
w.Flush()
}
// This example shows how to create a custom RowStreamer with specific
// formatting rules and string truncation.
func ExampleNewRowStreamer() {
var rows pgx.Rows
// Define custom formatting
cfg := pgfmt.Config{
Null: "NULL",
BoolTrue: "yes",
BoolFalse: "no",
MaxStringLength: 20,
}
// Create a specialized streaming worker
stream := pgfmt.NewRowStreamer(cfg.Format, nil)
w := csv.NewWriter(os.Stdout)
err := stream(w, rows)
if err != nil {
panic(err)
}
w.Flush()
}
// This example demonstrates how to configure the export for localized environments,
// specifically for opening CSV files in Russian or European Excel versions.
func ExampleConfig_localizedExcel() {
var rows pgx.Rows // Assume this is your query result
// 1. Setup config with localized formats
cfg := pgfmt.Config{
DecimalSeparator: ",", // Standard for RU/EU locales
DateOnlyFormat: "02.01.2006", // Typical Russian date format
DateTimeFormat: "02.01.2006 15:04:05",
Null: "", // Better look for empty cells in Excel
}
// 2. Write UTF-8 BOM to stdout (or your file)
// This is essential for Excel to correctly detect UTF-8 encoding.
_, _ = os.Stdout.Write([]byte{0xEF, 0xBB, 0xBF})
// 3. Setup CSV writer with a semicolon as the delimiter
w := csv.NewWriter(os.Stdout)
w.Comma = ';'
// 4. Create and run the streamer
stream := pgfmt.NewRowStreamer(cfg.Format, nil)
if err := stream(w, rows); err != nil {
panic(err)
}
// Don't forget to flush the CSV writer!
w.Flush()
}
// This example demonstrates low-level Handler usage for non-CSV outputs,
// such as implementing a custom printer.
func ExampleNewHandler() {
var rows pgx.Rows
// Create a handler that processes a slice of strings
handler := pgfmt.NewHandler(func(values []any, dst []string) {
for i, v := range values {
dst[i] = fmt.Sprint(v)
}
}, nil)
// Yield receives the pre-allocated slice for each row
err := handler(rows, func(row []string) error {
fmt.Printf("Data: %v\n", row)
return nil
})
if err != nil {
panic(err)
}
}
// This example demonstrates how to enable logging for unrecognized types
// and how to inspect them after the export is finished.
func ExampleConfig_unrecognizedTypes() {
var rows pgx.Rows
// 1. Setup config with a logger
cfg := &pgfmt.Config{
Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
w := csv.NewWriter(os.Stdout)
stream := pgfmt.NewRowStreamer(cfg.Format, nil)
// 2. Perform export
if err := stream(w, rows); err != nil {
panic(err)
}
w.Flush()
// 3. Check if any unknown types were encountered during the process
unknowns := cfg.GetUnknownTypes()
if len(unknowns) > 0 {
fmt.Printf("Unrecognized types found: %v\n", unknowns)
}
}