-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_custom_envelope_test.go
132 lines (120 loc) · 3 KB
/
example_custom_envelope_test.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
123
124
125
126
127
128
129
130
131
132
package jsmu_test
// This example demonstrates using a custom envelope with a JSON structure not expected by jsmu.Envelope.
import (
"encoding/json"
"fmt"
"github.com/nofeaturesonlybugs/jsmu"
)
// CustomEnvelope is for JSON that has a custom structure.
type CustomEnvelope struct {
TypeInfo string `json:"typeInfo"`
Payload json.RawMessage `json:"payload"`
CustomA string `json:"customA"`
CustomB string `json:"customB"`
data interface{} `json:"-"`
}
// GetMessage returns the message in the envelope.
func (me *CustomEnvelope) GetMessage() interface{} {
return me.data
}
// SetMessage sets the message in the envelope.
func (me *CustomEnvelope) SetMessage(message interface{}) {
me.data = message
}
// GetRawMessage returns the raw JSON message in the envelope.
func (me *CustomEnvelope) GetRawMessage() json.RawMessage {
return me.Payload
}
// SetRawMessage sets the raw JSON message in the envelope.
func (me *CustomEnvelope) SetRawMessage(raw json.RawMessage) {
me.Payload = raw
}
// GetTypeName returns the type name string.
func (me *CustomEnvelope) GetTypeName() string {
return me.TypeInfo
}
// SetTypeName sets the type name string.
func (me *CustomEnvelope) SetTypeName(typeInfo string) {
me.TypeInfo = typeInfo
}
func ExampleMU_customEnvelope() {
type Person struct {
jsmu.TypeName `js:"-" jsmu:"person"`
//
Name string `json:"name"`
Age int `json:"age"`
}
type Animal struct {
jsmu.TypeName `js:"-" jsmu:"animal"`
//
Name string `json:"name"`
Says string `json:"says"`
}
//
mu := &jsmu.MU{
EnveloperFn: func() jsmu.Enveloper {
return &CustomEnvelope{}
},
// StructTag : "", // "jsmu" is the default.
}
mu.MustRegister(&Person{})
mu.MustRegister(&Animal{})
//
strings := []string{
`{
"typeInfo" : "person",
"payload" : {
"name" : "Bob",
"age" : 40
},
"customA" : "Hello!",
"customB" : "Goodbye!"
}`,
`{
"typeInfo" : "animal",
"payload" : {
"name" : "cat",
"says" : "meow"
},
"customA" : "Foo",
"customB" : "Bar"
}`,
`{
"typeInfo" : "person",
"payload" : {
"name" : "Sally",
"age" : 30
},
"customA" : "qwerty",
"customB" : "dvorak"
}`,
`{
"typeInfo" : "animal",
"payload" : {
"name" : "cow",
"says" : "moo"
},
"customA" : "Batman",
"customB" : "Robin"
}`,
}
var envelope jsmu.Enveloper
var err error
for _, str := range strings {
if envelope, err = mu.Unmarshal([]byte(str)); err != nil {
fmt.Println(err)
return
}
custom := envelope.(*CustomEnvelope)
switch message := envelope.GetMessage().(type) {
case *Animal:
fmt.Printf("A %v says, \"%v;\" [%v,%v].\n", message.Name, message.Says, custom.CustomA, custom.CustomB)
case *Person:
fmt.Printf("%v is %v year(s) old; [%v,%v].\n", message.Name, message.Age, custom.CustomA, custom.CustomB)
}
}
// Output: Bob is 40 year(s) old; [Hello!,Goodbye!].
// A cat says, "meow;" [Foo,Bar].
// Sally is 30 year(s) old; [qwerty,dvorak].
// A cow says, "moo;" [Batman,Robin].
}