package constants import ( "encoding/json" "fmt" ) type CryptoType int const ( CryptoTypeNONE CryptoType = iota CryptoTypeTLS ) var ( cryptoTypeID = map[CryptoType]string{ CryptoTypeNONE: "", CryptoTypeTLS: "TLS", } cryptoTypeName = map[string]CryptoType{ "": CryptoTypeNONE, "TLS": CryptoTypeTLS, } ) func ToCryptoType(v string) CryptoType { return cryptoTypeName[v] } func (ct CryptoType) String() string { return cryptoTypeID[ct] } func (ct CryptoType) MarshalJSON() ([]byte, error) { value, ok := cryptoTypeID[ct] if !ok { return nil, fmt.Errorf("Invalid EnumType[%s] value", ct) } return json.Marshal(value) } func (ct CryptoType) UnmarshalJSON(b []byte) error { var s string err := json.Unmarshal(b, &s) if err != nil { return err } value, ok := cryptoTypeName[s] if !ok { return fmt.Errorf("Invalid EnumType[%s] value", s) } ct = value return nil }