-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
95 lines (82 loc) · 2.12 KB
/
parser.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
package typedjson
import (
"encoding/json"
"fmt"
"reflect"
)
type (
Parser interface {
Register(in TypedMessage)
Encode(in []byte) (TypedMessage, error)
Decode(in TypedMessage) ([]byte, error)
ForceDecode(in TypedMessage) ([]byte, error)
}
parserImpl struct {
registered map[string]TypedMessage
}
typedJsonObject struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
)
var defaultParser = NewJsonParser()
func Register(in TypedMessage) {
defaultParser.Register(in)
}
func Encode(in []byte) (TypedMessage, error) {
return defaultParser.Encode(in)
}
func Decode(in TypedMessage) ([]byte, error) {
return defaultParser.Decode(in)
}
func ForceDecode(in TypedMessage) ([]byte, error) {
return defaultParser.ForceDecode(in)
}
func NewJsonParser() Parser {
return &parserImpl{
registered: make(map[string]TypedMessage),
}
}
func (a *parserImpl) Register(in TypedMessage) {
a.registered[in.Type()] = in
}
func (a *parserImpl) Encode(in []byte) (TypedMessage, error) {
jsonObject := typedJsonObject{}
if err := json.Unmarshal(in, &jsonObject); err != nil {
return nil, err
}
for _, v := range a.registered {
if jsonObject.Type == v.Type() {
tmp, err := json.Marshal(jsonObject.Data)
if err != nil {
return nil, err
}
// ポインタをコピーして空の構造体を作る
object := reflect.New(reflect.ValueOf(v).Elem().Type()).Interface().(TypedMessage)
err = json.Unmarshal(tmp, &object)
if err != nil {
return nil, err
}
return object, nil
}
}
return nil, fmt.Errorf("encode failed: unregistered")
}
func (a *parserImpl) Decode(in TypedMessage) ([]byte, error) {
for _, v := range a.registered {
if in.Type() == v.Type() {
jsonObject := typedJsonObject{}
jsonObject.Type = in.Type()
jsonObject.Data = in
return json.Marshal(jsonObject)
}
}
return nil, fmt.Errorf("decode failed: unregistered")
}
// TODO 登録制にしてるなら、なくていいかも
func (a *parserImpl) ForceDecode(in TypedMessage) ([]byte, error) {
jsonObject := typedJsonObject{}
jsonObject.Type = in.Type()
jsonObject.Data = in
return json.Marshal(jsonObject)
}