-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
261 lines (240 loc) · 4.91 KB
/
address.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package csvutil
import (
"fmt"
"io"
"math/rand"
"strconv"
gimei "github.com/pinzolo/go-gimei"
"github.com/pkg/errors"
)
var prefs = []string{
"北海道",
"青森県",
"岩手県",
"宮城県",
"秋田県",
"山形県",
"福島県",
"茨城県",
"栃木県",
"群馬県",
"埼玉県",
"千葉県",
"東京都",
"神奈川県",
"新潟県",
"富山県",
"石川県",
"福井県",
"山梨県",
"長野県",
"岐阜県",
"静岡県",
"愛知県",
"三重県",
"滋賀県",
"京都府",
"大阪府",
"兵庫県",
"奈良県",
"和歌山県",
"鳥取県",
"島根県",
"岡山県",
"広島県",
"山口県",
"徳島県",
"香川県",
"愛媛県",
"高知県",
"福岡県",
"佐賀県",
"長崎県",
"熊本県",
"大分県",
"宮崎県",
"鹿児島県",
"沖縄県",
}
// AddressOption is option holder for Address.
type AddressOption struct {
// Source file does not have header line. (default false)
NoHeader bool
// Encoding of source file. (default utf8)
Encoding string
// Encoding for output.
OutputEncoding string
// ZipCode column symbol.
ZipCode string
// Prefecture column symbol.
Prefecture string
// Output prefecture code.
PrefectureCode bool
// City column symbol.
City string
// Town column symbol.
Town string
// BlockNumber output flag.
BlockNumber bool
// BlockNumber width(1 or 2)
NumberWidth int
}
func (o AddressOption) hasTargetColumn() bool {
syms := []string{
o.ZipCode,
o.Prefecture,
o.City,
o.Town,
}
for _, s := range syms {
if s != "" {
return true
}
}
return false
}
func (o AddressOption) validate() error {
if !o.hasTargetColumn() {
return errors.New("no column")
}
if o.NoHeader {
if !isEmptyOrDigit(o.ZipCode) {
return errors.New("not number zip code column symbol")
}
if !isEmptyOrDigit(o.Prefecture) {
return errors.New("not number prefecture column symbol")
}
if !isEmptyOrDigit(o.City) {
return errors.New("not number city column symbol")
}
if !isEmptyOrDigit(o.Town) {
return errors.New("not number town column symbol")
}
}
if o.NumberWidth != 1 && o.NumberWidth != 2 {
return errors.New("invalid number width (1 or 2)")
}
return nil
}
func (o AddressOption) isFullWidthBlockNumber() bool {
return o.NumberWidth == 2
}
func (o AddressOption) outputEncoding() string {
if o.OutputEncoding != "" {
return o.OutputEncoding
}
return o.Encoding
}
type addrCols struct {
zipCode *column
prefecture *column
city *column
town *column
}
func (c *addrCols) err() error {
cols := []*column{
c.zipCode,
c.prefecture,
c.city,
c.town,
}
for _, c := range cols {
if c.err != nil {
return c.err
}
}
return nil
}
func (c *addrCols) indexes() []int {
return []int{
c.zipCode.index,
c.prefecture.index,
c.city.index,
c.town.index,
}
}
// Address overwrite value of given column by dummy address.
func Address(r io.Reader, w io.Writer, o AddressOption) error {
if err := o.validate(); err != nil {
return errors.Wrap(err, "invalid option")
}
cr, bom := reader(r, o.Encoding)
cw := writer(w, bom, o.outputEncoding())
defer cw.Flush()
var cols *addrCols
csvp := NewCSVProcessor(cr, cw)
if o.NoHeader {
csvp.SetPreBodyRead(func() error {
cols = setupAddressCols(o, nil)
return cols.err()
})
} else {
csvp.SetHeaderHanlder(func(hdr []string) ([]string, error) {
cols = setupAddressCols(o, hdr)
return hdr, cols.err()
})
}
csvp.SetRecordHandler(func(rec []string) ([]string, error) {
newRec := make([]string, len(rec))
for i, s := range rec {
if !containsInt(cols.indexes(), i) {
newRec[i] = s
continue
}
addr := gimei.NewAddress()
if i == cols.zipCode.index {
newRec[i] += fakeZipCode()
}
if i == cols.prefecture.index {
if o.PrefectureCode {
newRec[i] += prefCode(addr.Prefecture.Kanji())
} else {
newRec[i] += addr.Prefecture.Kanji()
}
}
if i == cols.city.index {
newRec[i] += addr.City.Kanji()
}
if i == cols.town.index {
newRec[i] += addr.Town.Kanji()
if o.BlockNumber {
newRec[i] += fakeBlockNumber(o.isFullWidthBlockNumber())
}
}
}
return newRec, nil
})
return csvp.Process()
}
func setupAddressCols(o AddressOption, hdr []string) *addrCols {
cols := &addrCols{}
cols.zipCode = newColumnWithIndex(o.ZipCode, hdr)
cols.prefecture = newColumnWithIndex(o.Prefecture, hdr)
cols.town = newColumnWithIndex(o.Town, hdr)
cols.city = newColumnWithIndex(o.City, hdr)
return cols
}
func fakeZipCode() string {
return fmt.Sprintf("%03d-%04d", rand.Intn(1000), rand.Intn(10000))
}
func fakeBlockNumber(fullWidth bool) string {
bn := strconv.Itoa(rand.Intn(70) + 1)
if lot(50) {
bn += "-" + strconv.Itoa(rand.Intn(30)+1)
}
if lot(25) {
bn += "-" + strconv.Itoa(rand.Intn(10)+1)
}
if !fullWidth {
return bn
}
return toFullWidthNum(bn)
}
func prefCode(s string) string {
for i, p := range prefs {
if p == s {
return fmt.Sprintf("%02d", i+1)
}
}
return ""
}