-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
convert.go
103 lines (82 loc) · 2.35 KB
/
convert.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
package v2
import (
"bytes"
"database/sql"
"fmt"
"strconv"
)
type ConversionFunc func(value sql.RawBytes) (interface{}, error)
func ParseInt(value sql.RawBytes) (interface{}, error) {
v, err := strconv.ParseInt(string(value), 10, 64)
// Ignore ErrRange. When this error is set the returned value is "the
// maximum magnitude integer of the appropriate bitSize and sign."
if err, ok := err.(*strconv.NumError); ok && err.Err == strconv.ErrRange {
return v, nil
}
return v, err
}
func ParseBoolAsInteger(value sql.RawBytes) (interface{}, error) {
if bytes.EqualFold(value, []byte("YES")) || bytes.EqualFold(value, []byte("ON")) {
return int64(1), nil
}
return int64(0), nil
}
func ParseGTIDMode(value sql.RawBytes) (interface{}, error) {
// https://dev.mysql.com/doc/refman/8.0/en/replication-mode-change-online-concepts.html
v := string(value)
switch v {
case "OFF":
return int64(0), nil
case "ON":
return int64(1), nil
case "OFF_PERMISSIVE":
return int64(0), nil
case "ON_PERMISSIVE":
return int64(1), nil
default:
return nil, fmt.Errorf("unrecognized gtid_mode: %q", v)
}
}
func ParseValue(value sql.RawBytes) (interface{}, error) {
if bytes.EqualFold(value, []byte("YES")) || bytes.Equal(value, []byte("ON")) {
return 1, nil
}
if bytes.EqualFold(value, []byte("NO")) || bytes.Equal(value, []byte("OFF")) {
return 0, nil
}
if val, err := strconv.ParseInt(string(value), 10, 64); err == nil {
return val, nil
}
if val, err := strconv.ParseFloat(string(value), 64); err == nil {
return val, nil
}
if len(string(value)) > 0 {
return string(value), nil
}
return nil, fmt.Errorf("unconvertible value: %q", string(value))
}
var GlobalStatusConversions = map[string]ConversionFunc{
"ssl_ctx_verify_depth": ParseInt,
"ssl_verify_depth": ParseInt,
}
var GlobalVariableConversions = map[string]ConversionFunc{
"gtid_mode": ParseGTIDMode,
}
func ConvertGlobalStatus(key string, value sql.RawBytes) (interface{}, error) {
if bytes.Equal(value, []byte("")) {
return nil, nil
}
if conv, ok := GlobalStatusConversions[key]; ok {
return conv(value)
}
return ParseValue(value)
}
func ConvertGlobalVariables(key string, value sql.RawBytes) (interface{}, error) {
if bytes.Equal(value, []byte("")) {
return nil, nil
}
if conv, ok := GlobalVariableConversions[key]; ok {
return conv(value)
}
return ParseValue(value)
}