commons-go/core/constants/network-crypto.go
crusader 2eacff61a0 ing
2018-04-19 17:44:08 +09:00

54 lines
843 B
Go

package constants
import (
"encoding/json"
"fmt"
)
type CryptoType int
const (
CryptoTypeTLS CryptoType = iota
)
var (
cryptoTypeID = map[CryptoType]string{
CryptoTypeTLS: "TLS",
}
cryptoTypeName = map[string]CryptoType{
"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
}