commons-go/probe/constants/container.go

56 lines
926 B
Go
Raw Normal View History

2018-04-18 17:37:18 +09:00
package constants
import (
"encoding/json"
"fmt"
)
type ContainerType int
const (
DISCOVERY ContainerType = iota
NETWORK
GENERNAL
)
var (
containerTypeID = map[ContainerType]string{
DISCOVERY: "DISCOVERY",
NETWORK: "NETWORK",
GENERNAL: "GENERNAL",
}
containerTypeName = map[string]ContainerType{
"DISCOVERY": DISCOVERY,
"NETWORK": NETWORK,
"GENERNAL": GENERNAL,
}
)
func (ct ContainerType) String() string {
return containerTypeID[ct]
}
func (ct ContainerType) MarshalJSON() ([]byte, error) {
value, ok := containerTypeID[ct]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", ct)
}
return json.Marshal(value)
}
func (ct ContainerType) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := containerTypeName[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
ct = value
return nil
}