Skip to content

Commit 059a68e

Browse files
committed
Add supported encoding
Use htmlindex.Get to get the encoding.
1 parent ff1e2c2 commit 059a68e

File tree

8 files changed

+74
-19
lines changed

8 files changed

+74
-19
lines changed

cmd/header_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ func TestHeaderCmd_encoding_shift_jis(t *testing.T) {
201201
}
202202
}
203203

204-
func TestHeaderCmd_encoding_eucjp(t *testing.T) {
204+
func TestHeaderCmd_encoding_euc_jp(t *testing.T) {
205205

206206
rootCmd := newRootCmd()
207207
rootCmd.SetArgs([]string{
208208
"header",
209209
"-i", "../testdata/users-eucjp.csv",
210-
"--encoding", "eucjp",
210+
"--encoding", "EUC-JP",
211211
})
212212

213213
buf := new(bytes.Buffer)
@@ -225,13 +225,13 @@ func TestHeaderCmd_encoding_eucjp(t *testing.T) {
225225
}
226226
}
227227

228-
func TestHeaderCmd_encoding_euc_jp(t *testing.T) {
228+
func TestHeaderCmd_encoding_koi8_r(t *testing.T) {
229229

230230
rootCmd := newRootCmd()
231231
rootCmd.SetArgs([]string{
232232
"header",
233-
"-i", "../testdata/users-eucjp.csv",
234-
"--encoding", "EUC-JP",
233+
"-i", "../testdata/users-koi8r.csv",
234+
"--encoding", "koi8-r",
235235
})
236236

237237
buf := new(bytes.Buffer)
@@ -244,7 +244,55 @@ func TestHeaderCmd_encoding_euc_jp(t *testing.T) {
244244

245245
result := buf.String()
246246

247-
if result != "ID\n名前\n年齢\n" {
247+
if result != "ID\nНазовите\nвозраст\n" {
248+
t.Fatal("failed test\n", result)
249+
}
250+
}
251+
252+
func TestHeaderCmd_encoding_euc_kr(t *testing.T) {
253+
254+
rootCmd := newRootCmd()
255+
rootCmd.SetArgs([]string{
256+
"header",
257+
"-i", "../testdata/users-euckr.csv",
258+
"--encoding", "euc-kr",
259+
})
260+
261+
buf := new(bytes.Buffer)
262+
rootCmd.SetOutput(buf)
263+
264+
err := rootCmd.Execute()
265+
if err != nil {
266+
t.Fatal("failed test\n", err)
267+
}
268+
269+
result := buf.String()
270+
271+
if result != "ID\n이름\n나이\n" {
272+
t.Fatal("failed test\n", result)
273+
}
274+
}
275+
276+
func TestHeaderCmd_encoding_big5(t *testing.T) {
277+
278+
rootCmd := newRootCmd()
279+
rootCmd.SetArgs([]string{
280+
"header",
281+
"-i", "../testdata/users-big5.csv",
282+
"--encoding", "big5",
283+
})
284+
285+
buf := new(bytes.Buffer)
286+
rootCmd.SetOutput(buf)
287+
288+
err := rootCmd.Execute()
289+
if err != nil {
290+
t.Fatal("failed test\n", err)
291+
}
292+
293+
result := buf.String()
294+
295+
if result != "ID\n名稱\n年齡\n" {
248296
t.Fatal("failed test\n", result)
249297
}
250298
}

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newRootCmd() *cobra.Command {
2020
rootCmd.PersistentFlags().StringP("quote", "", "", "(optional) CSV quote. The default is '\"'")
2121
rootCmd.PersistentFlags().StringP("sep", "", "", "(optional) CSV record separator. The default is CRLF.")
2222
rootCmd.PersistentFlags().BoolP("allquote", "", false, "(optional) Always quote CSV fields. The default is to quote only the necessary fields.")
23-
rootCmd.PersistentFlags().StringP("encoding", "", "", "(optional) CSV encoding. The default is utf-8. Supported encodings: utf-8, shift_jis, euc-jp")
23+
rootCmd.PersistentFlags().StringP("encoding", "", "", "(optional) CSV encoding. The default is utf-8.")
2424
rootCmd.PersistentFlags().BoolP("bom", "", false, "(optional) CSV with BOM. When reading, the BOM will be automatically removed without this flag.")
2525
rootCmd.PersistentFlags().SortFlags = false
2626
rootCmd.Flags().SortFlags = false

cmd/transform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newTransformCmd() *cobra.Command {
4545
transformCmd.Flags().StringP("out-quote", "", "", "(optional) Output CSV quote. The default is '\"'")
4646
transformCmd.Flags().StringP("out-sep", "", "", "(optional) Output CSV record separator. The default is CRLF.")
4747
transformCmd.Flags().BoolP("out-allquote", "", false, "(optional) Always quote output CSV fields. The default is to quote only the necessary fields.")
48-
transformCmd.Flags().StringP("out-encoding", "", "", "(optional) Output CSV encoding. The default is utf-8. Supported encodings: utf-8, shift_jis, euc-jp")
48+
transformCmd.Flags().StringP("out-encoding", "", "", "(optional) Output CSV encoding. The default is utf-8.")
4949
transformCmd.Flags().BoolP("out-bom", "", false, "(optional) Output CSV with BOM.")
5050

5151
return transformCmd

csv/csv.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"strings"
77

88
"github.com/onozaty/go-customcsv"
9+
"golang.org/x/exp/slices"
910
"golang.org/x/text/encoding"
10-
"golang.org/x/text/encoding/japanese"
11+
"golang.org/x/text/encoding/htmlindex"
1112
"golang.org/x/text/transform"
1213
)
1314

@@ -78,18 +79,15 @@ func NewCsvWriter(w io.Writer, f Format) CsvWriter {
7879

7980
func Encoding(name string) (encoding.Encoding, error) {
8081

81-
normalizeName := strings.ReplaceAll(strings.ReplaceAll(strings.ToLower(name), "_", ""), "-", "")
82-
83-
switch normalizeName {
84-
case "utf8":
82+
if slices.Contains([]string{"utf8", "utf-8"}, strings.ToLower(name)) {
8583
// UTF-8の場合は変換不要
8684
return nil, nil
87-
case "shiftjis", "sjis":
88-
return japanese.ShiftJIS, nil
89-
case "eucjp":
90-
return japanese.EUCJP, nil
91-
default:
85+
}
86+
87+
encoding, err := htmlindex.Get(name)
88+
if err != nil {
9289
return nil, fmt.Errorf("invalid encoding name: %s", name)
9390
}
9491

92+
return encoding, nil
9593
}

csv/csv_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func TestEncoding_sjis(t *testing.T) {
299299

300300
func TestEncoding_eucjp(t *testing.T) {
301301

302-
encoding1, err := Encoding("eucjp")
302+
encoding1, err := Encoding("euc-jp")
303303
if err != nil {
304304
t.Fatal("failed test\n", err)
305305
}

testdata/users-big5.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ID,�W��,�~��
2+
1,"Taro, Yamada",20
3+
2,Hanako,21

testdata/users-euckr.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ID,�̸�,����
2+
1,"Taro, Yamada",20
3+
2,Hanako,21

testdata/users-koi8r.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ID,��������,�������
2+
1,"Taro, Yamada",20
3+
2,Hanako,21

0 commit comments

Comments
 (0)