forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcoding.go
98 lines (86 loc) · 2.67 KB
/
transcoding.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
package gocb
import (
"encoding/json"
"gopkg.in/couchbase/gocbcore.v7"
)
// Transcoder provides an interface for transforming Go values to and
// from raw bytes for storage and retreival from Couchbase data storage.
type Transcoder interface {
// Decodes retrieved bytes into a Go type.
Decode([]byte, uint32, interface{}) error
// Encodes a Go type into bytes for storage.
Encode(interface{}) ([]byte, uint32, error)
}
// DefaultTranscoder implements the default transcoding behaviour of
// all Couchbase SDKs.
type DefaultTranscoder struct {
}
// Decode applies the default Couchbase transcoding behaviour to decode into a Go type.
func (t DefaultTranscoder) Decode(bytes []byte, flags uint32, out interface{}) error {
valueType, compression := gocbcore.DecodeCommonFlags(flags)
// Make sure compression is disabled
if compression != gocbcore.NoCompression {
return clientError{"Unexpected value compression"}
}
// Normal types of decoding
if valueType == gocbcore.BinaryType {
switch typedOut := out.(type) {
case *[]byte:
*typedOut = bytes
return nil
case *interface{}:
*typedOut = bytes
return nil
default:
return clientError{"You must encode binary in a byte array or interface"}
}
} else if valueType == gocbcore.StringType {
switch typedOut := out.(type) {
case *string:
*typedOut = string(bytes)
return nil
case *interface{}:
*typedOut = string(bytes)
return nil
default:
return clientError{"You must encode a string in a string or interface"}
}
} else if valueType == gocbcore.JsonType {
err := json.Unmarshal(bytes, &out)
if err != nil {
return err
}
return nil
}
return clientError{"Unexpected flags value"}
}
// Encode applies the default Couchbase transcoding behaviour to encode a Go type.
func (t DefaultTranscoder) Encode(value interface{}) ([]byte, uint32, error) {
var bytes []byte
var flags uint32
var err error
switch typeValue := value.(type) {
case []byte:
bytes = typeValue
flags = gocbcore.EncodeCommonFlags(gocbcore.BinaryType, gocbcore.NoCompression)
case *[]byte:
bytes = *typeValue
flags = gocbcore.EncodeCommonFlags(gocbcore.BinaryType, gocbcore.NoCompression)
case string:
bytes = []byte(typeValue)
flags = gocbcore.EncodeCommonFlags(gocbcore.StringType, gocbcore.NoCompression)
case *string:
bytes = []byte(*typeValue)
flags = gocbcore.EncodeCommonFlags(gocbcore.StringType, gocbcore.NoCompression)
case *interface{}:
return t.Encode(*typeValue)
default:
bytes, err = json.Marshal(value)
if err != nil {
return nil, 0, err
}
flags = gocbcore.EncodeCommonFlags(gocbcore.JsonType, gocbcore.NoCompression)
}
// No compression supported currently
return bytes, flags, nil
}