gateway/subscribe/message.go
crusader 9d8e274d6f ing
2018-04-13 16:54:47 +09:00

70 lines
1.3 KiB
Go

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]
}
func (st *TargetType) MarshalJSON() ([]byte, error) {
value, ok := targetTypeID[*st]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", st)
}
return json.Marshal(value)
}
func (st *TargetType) UnmarshalJSON(b []byte) error {
// 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)
}
st = &value
return nil
}