2018-04-19 07:47:10 +00:00
|
|
|
package constants
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PortType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PortTypeTCP PortType = iota
|
|
|
|
PortTypeUDP
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
portTypeID = map[PortType]string{
|
2018-04-28 14:51:47 +00:00
|
|
|
PortTypeTCP: "tcp",
|
|
|
|
PortTypeUDP: "udp",
|
2018-04-19 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
portTypeName = map[string]PortType{
|
2018-04-28 14:51:47 +00:00
|
|
|
"tcp": PortTypeTCP,
|
|
|
|
"udp": PortTypeUDP,
|
2018-04-19 07:47:10 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-04-19 08:44:08 +00:00
|
|
|
func ToPortType(v string) PortType {
|
|
|
|
return portTypeName[v]
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:47:10 +00:00
|
|
|
func (pt PortType) String() string {
|
|
|
|
return portTypeID[pt]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt PortType) MarshalJSON() ([]byte, error) {
|
|
|
|
value, ok := portTypeID[pt]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid EnumType[%s] value", pt)
|
|
|
|
}
|
|
|
|
return json.Marshal(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt PortType) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(b, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := portTypeName[s]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Invalid EnumType[%s] value", s)
|
|
|
|
}
|
|
|
|
pt = value
|
|
|
|
return nil
|
|
|
|
}
|