-
Notifications
You must be signed in to change notification settings - Fork 3
/
codec.go
59 lines (50 loc) · 1.39 KB
/
codec.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
package y3
import (
"reflect"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
// Codec encode the user's data according to the Y3 encoding rules
type Codec interface {
// Marshal encode interface to []byte
Marshal(input interface{}) ([]byte, error)
}
// NewCodec create a Codec interface
func NewCodec(observe byte) Codec {
return &y3Codec{
observe: observe,
}
}
// y3Codec is implementation of the Codec interface
type y3Codec struct {
observe byte
}
// Marshal encode interface to []byte
func (c y3Codec) Marshal(input interface{}) ([]byte, error) {
if c.isStruct(input) {
return newStructEncoder(c.observe,
structEncoderOptionRoot(utils.RootToken),
structEncoderOptionForbidUserKey(utils.ForbidUserKey),
structEncoderOptionAllowSignalKey(utils.AllowSignalKey)).
Encode(input)
}
return newBasicEncoder(c.observe,
basicEncoderOptionRoot(utils.RootToken),
basicEncoderOptionForbidUserKey(utils.ForbidUserKey),
basicEncoderOptionAllowSignalKey(utils.AllowSignalKey)).
Encode(input)
}
// isStruct determine whether an interface is a structure
func (c y3Codec) isStruct(mold interface{}) bool {
isStruct := false
moldValue := reflect.Indirect(reflect.ValueOf(mold))
moldType := moldValue.Type()
switch moldType.Kind() {
case reflect.Struct:
isStruct = true
case reflect.Slice:
if moldType.Elem().Kind() == reflect.Struct {
isStruct = true
}
}
return isStruct
}