container_network/crawler/health/socket_error.go
crusader a7480afdd5 ing
2018-07-04 18:33:58 +09:00

56 lines
1.1 KiB
Go

package health
import (
"encoding/json"
"fmt"
)
type SocketErrorEnum int
const (
SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1
SocketErrorEnumHOSTUNREACH
SocketErrorEnumCONNREFUSED
)
var (
socketErrorEnumID = map[SocketErrorEnum]string{
SocketErrorEnumUNKNOWN: "UNKNOWN",
SocketErrorEnumHOSTUNREACH: "HOSTUNREACH",
SocketErrorEnumCONNREFUSED: "CONNREFUSED",
}
socketErrorEnumKey = map[string]SocketErrorEnum{
"UNKNOWN": SocketErrorEnumUNKNOWN,
"HOSTUNREACH": SocketErrorEnumHOSTUNREACH,
"CONNREFUSED": SocketErrorEnumCONNREFUSED,
}
)
func (e SocketErrorEnum) String() string {
return socketErrorEnumID[e]
}
func (e SocketErrorEnum) MarshalJSON() ([]byte, error) {
value, ok := socketErrorEnumID[e]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", e)
}
return json.Marshal(value)
}
func (e SocketErrorEnum) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := socketErrorEnumKey[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
e = value
return nil
}