Extracted and generalized encoding logic to encoding.go

This commit is contained in:
Shlomi Noach 2016-09-08 09:37:22 +02:00
parent dc3a03ad6f
commit f4693274fb
2 changed files with 24 additions and 5 deletions

21
go/sql/encoding.go Normal file
View File

@ -0,0 +1,21 @@
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package sql
import (
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
)
type charsetEncoding map[string]encoding.Encoding
var charsetEncodingMap charsetEncoding
func init() {
charsetEncodingMap = make(map[string]encoding.Encoding)
// Begin mappings
charsetEncodingMap["latin1"] = charmap.Windows1252
}

View File

@ -10,8 +10,6 @@ import (
"reflect"
"strconv"
"strings"
"golang.org/x/text/encoding/charmap"
)
type Column struct {
@ -22,9 +20,9 @@ type Column struct {
func (this *Column) convertArg(arg interface{}) interface{} {
if s, ok := arg.(string); ok {
switch this.Charset {
case "latin1":
arg, _ = charmap.Windows1252.NewDecoder().String(s)
// string, charset conversion
if encoding, ok := charsetEncodingMap[this.Charset]; ok {
arg, _ = encoding.NewDecoder().String(s)
}
return arg
}