diff --git a/subscribe_message.go b/subscribe_message.go index c846999..f0499d4 100644 --- a/subscribe_message.go +++ b/subscribe_message.go @@ -1,10 +1,14 @@ package overflow_subscriber -import "encoding/json" +import ( + "encoding/json" + "fmt" +) type SubscribeMessage struct { - Targets []string `json:"targets"` - MessageRaw *json.RawMessage `json:"message"` + TargetType SubscribeTargetType `json:"targetType"` + Targets []string `json:"targets"` + MessageRaw *json.RawMessage `json:"message"` Message []byte } @@ -13,3 +17,53 @@ type SubscribeMessageBody struct { Method string `json:"method"` Params interface{} `json:"params,omitempty"` } + +type SubscribeTargetType int + +const ( + MEMBER_SESSION SubscribeTargetType = iota + MEMBER_ID + PROBE_ID +) + +var ( + subscribeTargetTypeID = map[SubscribeTargetType]string{ + MEMBER_SESSION: "MEMBER_SESSION", + MEMBER_ID: "MEMBER_ID", + PROBE_ID: "PROBE_ID", + } + + subscribeTargetTypeName = map[string]SubscribeTargetType{ + "MEMBER_SESSION": MEMBER_SESSION, + "MEMBER_ID": MEMBER_ID, + "PROBE_ID": PROBE_ID, + } +) + +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 +}