overflow_subscriber/subscribe_message.go
crusader 9c9b7882be ing
2018-03-19 16:23:39 +09:00

70 lines
1.4 KiB
Go

package overflow_subscriber
import (
"encoding/json"
"fmt"
)
type SubscribeMessage struct {
TargetType SubscribeTargetType `json:"targetType"`
Targets []string `json:"targets"`
MessageRaw *json.RawMessage `json:"message"`
Message []byte
}
type SubscribeMessageBody struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
type SubscribeTargetType int
const (
MEMBER_SESSION SubscribeTargetType = iota
MEMBER
PROBE
)
var (
subscribeTargetTypeID = map[SubscribeTargetType]string{
MEMBER_SESSION: "MEMBER_SESSION",
MEMBER: "MEMBER",
PROBE: "PROBE",
}
subscribeTargetTypeName = map[string]SubscribeTargetType{
"MEMBER_SESSION": MEMBER_SESSION,
"MEMBER": MEMBER,
"PROBE": PROBE,
}
)
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
}