-
Notifications
You must be signed in to change notification settings - Fork 6
/
serializer.go
122 lines (100 loc) · 2.93 KB
/
serializer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package serialize
import (
"errors"
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type (
SuppportedType string
TargetType string
GenericSerializer struct {
Supports []SuppportedType
Target TargetType
UseProtoNames bool // to use proto field name instead of lowerCamelCase name in JSON field names
}
StringSerializer struct {
}
BinarySerializer struct {
}
JSONSerializer struct {
}
)
var (
// AnyType defines that serializer supports any supportable variable type
AnyType SuppportedType = `any`
// StructType defines that serializer supports struct type
StructType SuppportedType = `struct`
ProtoType SuppportedType = `proto`
ScalarType SuppportedType = `scalar`
DefaultTarget TargetType = `default`
PreferJSON TargetType = `json`
DefaultSerializer = &GenericSerializer{
Supports: []SuppportedType{AnyType},
Target: DefaultTarget,
UseProtoNames: true,
}
PreferJSONSerializer = &GenericSerializer{
Supports: []SuppportedType{AnyType},
Target: PreferJSON,
UseProtoNames: true,
}
KeySerializer = &StringSerializer{}
ErrOnlyStringSupported = errors.New(`only string supported`)
)
func (g *GenericSerializer) ToBytesFrom(entry interface{}) ([]byte, error) {
switch entryType := entry.(type) {
case Serializable:
return entryType.ToBytes(g)
case proto.Message:
if g.Target == PreferJSON {
mo := &protojson.MarshalOptions{UseProtoNames: g.UseProtoNames}
return JSONProtoMarshal(entryType, mo)
} else {
return BinaryProtoMarshal(entryType)
}
default:
return toBytes(entry)
}
}
func (g *GenericSerializer) FromBytesTo(serialized []byte, target interface{}) (interface{}, error) {
switch targetType := target.(type) {
case proto.Message:
if g.Target == PreferJSON {
return JSONProtoUnmarshal(serialized, targetType)
} else {
return BinaryProtoUnmarshal(serialized, targetType)
}
default:
return fromBytes(serialized, target)
}
}
func (g *StringSerializer) ToBytesFrom(entry interface{}) ([]byte, error) {
switch v := entry.(type) {
case string:
return []byte(v), nil
}
return nil, ErrOnlyStringSupported
}
func (g *StringSerializer) FromBytesTo(serialized []byte, target interface{}) (interface{}, error) {
switch t := target.(type) {
case string:
return string(serialized), nil
default:
return nil, fmt.Errorf(`type=%s: %w`, t, ErrOnlyStringSupported)
}
}
//func (js *JSONSerializer) ToBytes(entry interface{}) ([]byte, error) {
// return json.Marshal(entry)
//}
//
//func (js *JSONSerializer) FromBytes(serialized []byte, target interface{}) (interface{}, error) {
// return JSONUnmarshalPtr(serialized, target)
//}
//func (ps *ProtoSerializer) ToBytes(entry interface{}) ([]byte, error) {
// return proto.Marshal(entry.(proto.Message))
//}
//
//func (ps *ProtoSerializer) FromBytes(serialized []byte, target interface{}) (interface{}, error) {
// return convert.FromBytes(serialized, target)
//}