-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
100 lines (89 loc) · 2.75 KB
/
encoding.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
package encoding
import (
"fmt"
"reflect"
"unsafe"
)
// MustFromEncoding parse specific encoding string to universal Go data.
// It panic if error occur.
func MustFromEncoding(encoding string, source string) interface{} {
d, err := FromEncoding(encoding, source)
if err != nil {
panic(err)
}
return d
}
// MustToEncoding encode a universal Go data into specific encoding string.
// It panic if error occur.
func MustToEncoding(encoding string, d interface{}, pretty bool) string {
s, err := ToEncoding(encoding, d, pretty)
if err != nil {
panic(err)
}
return s
}
// MustConvertEncoding converts source text from encoding to dstEncoding.
// It panic if error occur.
func MustConvertEncoding(encoding string, source string, dstEncoding string, pretty bool) string {
s, err := ConvertEncoding(encoding, source, dstEncoding, pretty)
if err != nil {
panic(err)
}
return s
}
//------------------------------------------------------------------------------
// FromEncoding parse specific encoding string to universal Go data.
func FromEncoding(encoding string, source string) (interface{}, error) {
switch enc := encoding; enc {
case "", consts.EncodingJson:
return FromJson(source)
case consts.EncodingXml:
return FromXml(source)
case consts.EncodingYaml:
return FromYaml(source)
default:
return nil, fmt.Errorf("encoding: unsupported encoding %s", enc)
}
}
// ToEncoding encode a universal Go data into specific encoding string.
func ToEncoding(encoding string, d interface{}, pretty bool) (string, error) {
switch enc := encoding; enc {
case "", consts.EncodingJson:
return ToJson(d, pretty)
case consts.EncodingXml:
return ToXml(d, pretty)
case consts.EncodingYaml:
return ToYaml(d, pretty)
default:
return "", fmt.Errorf("encoding: unsupported encoding %s", enc)
}
}
// ConvertEncoding converts source text from encoding to dstEncoding.
func ConvertEncoding(encoding string, source string, dstEncoding string, pretty bool) (string, error) {
if encoding == dstEncoding {
return source, nil
}
obj, err := FromEncoding(encoding, source)
if err != nil {
return "", err
}
dst, err := ToEncoding(dstEncoding, obj, pretty)
if err != nil {
return "", err
}
return dst, err
}
//------------------------------------------------------------------------------
// unsafeByteString convert []byte to string without copy
// the origin []byte **MUST NOT** accessed after that
func unsafeByteString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// unsafeStringBytes return GoString's buffer slice
// ** NEVER modify returned []byte **
func unsafeStringBytes(s string) []byte {
var bh reflect.SliceHeader
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return *(*[]byte)(unsafe.Pointer(&bh))
}