gateway/subscribe/message.go

70 lines
1.3 KiB
Go
Raw Normal View History

2018-04-09 11:37:54 +00:00
package subscribe
import (
"encoding/json"
"fmt"
)
type Message struct {
TargetType TargetType `json:"targetType"`
Targets []string `json:"targets"`
MessageRaw *json.RawMessage `json:"message"`
Message []byte
}
type MessageBody struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
type TargetType int
const (
MEMBER_SESSION TargetType = iota
MEMBER
PROBE
)
var (
targetTypeID = map[TargetType]string{
MEMBER_SESSION: "MEMBER_SESSION",
MEMBER: "MEMBER",
PROBE: "PROBE",
}
targetTypeName = map[string]TargetType{
"MEMBER_SESSION": MEMBER_SESSION,
"MEMBER": MEMBER,
"PROBE": PROBE,
}
)
func (st TargetType) String() string {
return targetTypeID[st]
}
2018-04-13 07:54:47 +00:00
func (st *TargetType) MarshalJSON() ([]byte, error) {
value, ok := targetTypeID[*st]
2018-04-09 11:37:54 +00:00
if !ok {
2018-04-13 07:58:52 +00:00
return nil, fmt.Errorf("Invalid EnumType[%s] value", *st)
2018-04-09 11:37:54 +00:00
}
return json.Marshal(value)
}
2018-04-13 07:54:47 +00:00
func (st *TargetType) UnmarshalJSON(b []byte) error {
2018-04-09 11:37:54 +00:00
// unmarshal as string
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := targetTypeName[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
2018-04-13 07:58:52 +00:00
*st = value
2018-04-09 11:37:54 +00:00
return nil
}