-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvelope.go
77 lines (66 loc) · 2.21 KB
/
envelope.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
package jsmu
import "encoding/json"
var (
// DefaultEnveloperFunc creates and returns an *Envelope from this package.
DefaultEnveloperFunc = func() Enveloper {
return &Envelope{}
}
)
// EnveloperFunc is a function that creates a new Enveloper.
type EnveloperFunc func() Enveloper
// Enveloper is the interface for message Envelopes.
type Enveloper interface {
// GetMessage returns the message in the envelope.
GetMessage() interface{}
// SetMessage sets the message in the envelope.
SetMessage(interface{})
// GetRawMessage returns the raw JSON message in the envelope.
GetRawMessage() json.RawMessage
// SetRawMessage sets the raw JSON message in the envelope.
SetRawMessage(json.RawMessage)
// GetTypeName returns the type name string.
GetTypeName() string
// SetTypeName sets the type name string.
SetTypeName(string)
}
// Envelope is a default Enveloper for basic needs.
//
// JSON must conform to this format:
// {
// "type" : string, // A string uniquely identifying "message".
// "id" : string, // An optional unique message ID; provided as a convenience for request-reply message flow.
// "message" : {}, // Any JSON data your application can receive.
// }
type Envelope struct {
Type string `json:"type"`
Id string `json:"id"`
Raw json.RawMessage `json:"message"`
//
// This is the concrete type; even though it's a private field and invisible to the json
// package we still explicitly exclude it.
message interface{} `json:"-"`
}
// GetMessage returns the message in the envelope.
func (me *Envelope) GetMessage() interface{} {
return me.message
}
// SetMessage sets the message in the envelope.
func (me *Envelope) SetMessage(message interface{}) {
me.message = message
}
// GetRawMessage returns the raw JSON message in the envelope.
func (me *Envelope) GetRawMessage() json.RawMessage {
return me.Raw
}
// SetRawMessage sets the raw JSON message in the envelope.
func (me *Envelope) SetRawMessage(raw json.RawMessage) {
me.Raw = raw
}
// GetTypeName returns the type name string.
func (me *Envelope) GetTypeName() string {
return me.Type
}
// SetTypeName sets the type name string.
func (me *Envelope) SetTypeName(typeName string) {
me.Type = typeName
}