external-go/const.go
crusader d871241375 ing
2018-05-11 16:46:06 +09:00

65 lines
1.1 KiB
Go

package external
import (
"encoding/json"
"fmt"
cuc "git.loafle.net/commons/util-go/context"
)
const (
GRPCClientTypeKey = cuc.ContextKey("OVERFLOW_GRPC_CLIENT_TYPE")
GRPCSessionIDKey = cuc.ContextKey("OVERFLOW_GRPC_SESSION_ID")
GRPCTargetIDKey = cuc.ContextKey("OVERFLOW_GRPC_TARGET_ID")
)
type ClientType int
const (
MEMBER ClientType = iota
PROBE
CONSUMER
)
var (
clientTypeID = map[ClientType]string{
MEMBER: "MEMBER",
PROBE: "PROBE",
CONSUMER: "CONSUMER",
}
clientTypeName = map[string]ClientType{
"MEMBER": MEMBER,
"PROBE": PROBE,
"CONSUMER": CONSUMER,
}
)
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
}