-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbinary.go
More file actions
80 lines (70 loc) · 1.91 KB
/
Copy pathbinary.go
File metadata and controls
80 lines (70 loc) · 1.91 KB
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
package godatabend
import (
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
)
type binaryOutputFormat uint8
const (
binaryOutputFormatHex binaryOutputFormat = iota
binaryOutputFormatBase64
binaryOutputFormatUTF8
binaryOutputFormatUTF8Lossy
)
type httpJSONResultMode uint8
const (
httpJSONResultModeDriver httpJSONResultMode = iota
httpJSONResultModeDisplay
)
func parseBinaryOutputFormat(s string) binaryOutputFormat {
switch strings.ToUpper(strings.TrimSpace(s)) {
case "BASE64":
return binaryOutputFormatBase64
case "UTF-8", "UTF8":
return binaryOutputFormatUTF8
case "UTF-8-LOSSY", "UTF8-LOSSY":
return binaryOutputFormatUTF8Lossy
default:
return binaryOutputFormatHex
}
}
func parseHTTPJSONResultMode(s string) httpJSONResultMode {
switch strings.ToLower(strings.TrimSpace(s)) {
case "display":
return httpJSONResultModeDisplay
default:
return httpJSONResultModeDriver
}
}
func materializeBinaryFromString(value string, format binaryOutputFormat, mode httpJSONResultMode) (driver.Value, error) {
// Databend HTTP API uses driver mode by default, where Binary cells are encoded as hex
// regardless of binary_output_format.
if mode != httpJSONResultModeDisplay {
raw, err := hex.DecodeString(value)
if err != nil {
return nil, fmt.Errorf("failed to decode binary hex value: %w", err)
}
return raw, nil
}
switch format {
case binaryOutputFormatBase64:
raw, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return nil, fmt.Errorf("failed to decode binary base64 value: %w", err)
}
return raw, nil
case binaryOutputFormatUTF8, binaryOutputFormatUTF8Lossy:
return []byte(value), nil
default:
raw, err := hex.DecodeString(value)
if err != nil {
return nil, fmt.Errorf("failed to decode binary hex value: %w", err)
}
return raw, nil
}
}
func materializeBinaryFromBinary(value []byte) driver.Value {
return append([]byte(nil), value...)
}