Skip to content

Commit f469327

Browse files
author
Shlomi Noach
committed
Extracted and generalized encoding logic to encoding.go
1 parent dc3a03a commit f469327

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

go/sql/encoding.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2016 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package sql
7+
8+
import (
9+
"golang.org/x/text/encoding"
10+
"golang.org/x/text/encoding/charmap"
11+
)
12+
13+
type charsetEncoding map[string]encoding.Encoding
14+
15+
var charsetEncodingMap charsetEncoding
16+
17+
func init() {
18+
charsetEncodingMap = make(map[string]encoding.Encoding)
19+
// Begin mappings
20+
charsetEncodingMap["latin1"] = charmap.Windows1252
21+
}

go/sql/types.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"reflect"
1111
"strconv"
1212
"strings"
13-
14-
"golang.org/x/text/encoding/charmap"
1513
)
1614

1715
type Column struct {
@@ -22,9 +20,9 @@ type Column struct {
2220

2321
func (this *Column) convertArg(arg interface{}) interface{} {
2422
if s, ok := arg.(string); ok {
25-
switch this.Charset {
26-
case "latin1":
27-
arg, _ = charmap.Windows1252.NewDecoder().String(s)
23+
// string, charset conversion
24+
if encoding, ok := charsetEncodingMap[this.Charset]; ok {
25+
arg, _ = encoding.NewDecoder().String(s)
2826
}
2927
return arg
3028
}

0 commit comments

Comments
 (0)