overflow_gateway_websocket/constants.go
crusader bde58378b4 ing
2018-03-19 16:57:56 +09:00

74 lines
1.3 KiB
Go

package overflow_gateway_websocket
import (
"encoding/json"
"fmt"
cuc "git.loafle.net/commons_go/util/context"
)
var (
ServletSocketKey = cuc.ContextKey("ServletSocket")
GRPCMetadataKey = cuc.ContextKey("GRPCMetadata")
ClientTypeKey = cuc.ContextKey("ClientType")
SocketIDKey = cuc.ContextKey("SocketID")
TargetIDKey = cuc.ContextKey("TargetID")
)
const (
ConfigPathFlagName = "config-dir"
ConfigFileName = "config.json"
GRPCClientTypeKey = "OVERFLOW_GRPC_CLIENT_TYPE"
GRPCSessionIDKey = "OVERFLOW_GRPC_SESSION_ID"
GRPCTargetIDKey = "OVERFLOW_GRPC_TARGET_ID"
)
type ClientType int
const (
MEMBER ClientType = iota
PROBE
)
var (
clientTypeID = map[ClientType]string{
MEMBER: "MEMBER",
PROBE: "PROBE",
}
clientTypeName = map[string]ClientType{
"MEMBER": MEMBER,
"PROBE": PROBE,
}
)
func (ct ClientType) String() string {
return clientTypeID[ct]
}
func (ct *ClientType) MarshalJSON() ([]byte, error) {
value, ok := clientTypeID[*ct]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", *ct)
}
return json.Marshal(value)
}
func (ct *ClientType) UnmarshalJSON(b []byte) error {
// unmarshal as string
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := clientTypeName[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
*ct = value
return nil
}