overflow_subscriber/subscribe_message.go

70 lines
1.4 KiB
Go
Raw Normal View History

2017-09-27 07:35:13 +00:00
package overflow_subscriber
2018-03-19 06:35:40 +00:00
import (
"encoding/json"
"fmt"
)
2017-09-27 08:21:34 +00:00
2017-09-27 07:35:13 +00:00
type SubscribeMessage struct {
2018-03-19 06:35:40 +00:00
TargetType SubscribeTargetType `json:"targetType"`
Targets []string `json:"targets"`
MessageRaw *json.RawMessage `json:"message"`
2017-09-27 08:30:32 +00:00
Message []byte
2017-09-27 07:35:13 +00:00
}
type SubscribeMessageBody struct {
2017-12-09 07:33:48 +00:00
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
2017-09-27 07:35:13 +00:00
}
2018-03-19 06:35:40 +00:00
type SubscribeTargetType int
const (
MEMBER_SESSION SubscribeTargetType = iota
2018-03-19 07:23:39 +00:00
MEMBER
PROBE
2018-03-19 06:35:40 +00:00
)
var (
subscribeTargetTypeID = map[SubscribeTargetType]string{
MEMBER_SESSION: "MEMBER_SESSION",
2018-03-19 07:23:39 +00:00
MEMBER: "MEMBER",
PROBE: "PROBE",
2018-03-19 06:35:40 +00:00
}
subscribeTargetTypeName = map[string]SubscribeTargetType{
"MEMBER_SESSION": MEMBER_SESSION,
2018-03-19 07:23:39 +00:00
"MEMBER": MEMBER,
"PROBE": PROBE,
2018-03-19 06:35:40 +00:00
}
)
func (st SubscribeTargetType) String() string {
return subscribeTargetTypeID[st]
}
func (st *SubscribeTargetType) MarshalJSON() ([]byte, error) {
value, ok := subscribeTargetTypeID[*st]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", *st)
}
return json.Marshal(value)
}
func (st *SubscribeTargetType) UnmarshalJSON(b []byte) error {
// unmarshal as string
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := subscribeTargetTypeName[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
*st = value
return nil
}