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