commons-go/core/constants/network-crypto.go

57 lines
907 B
Go
Raw Permalink Normal View History

2018-04-19 08:25:56 +00:00
package constants
import (
"encoding/json"
"fmt"
)
type CryptoType int
const (
2018-04-27 15:23:30 +00:00
CryptoTypeNONE CryptoType = iota
CryptoTypeTLS
2018-04-19 08:25:56 +00:00
)
var (
cryptoTypeID = map[CryptoType]string{
2018-04-27 15:23:30 +00:00
CryptoTypeNONE: "",
CryptoTypeTLS: "TLS",
2018-04-19 08:25:56 +00:00
}
cryptoTypeName = map[string]CryptoType{
2018-04-27 15:23:30 +00:00
"": CryptoTypeNONE,
2018-04-19 08:25:56 +00:00
"TLS": CryptoTypeTLS,
}
)
2018-04-19 08:44:08 +00:00
func ToCryptoType(v string) CryptoType {
return cryptoTypeName[v]
}
2018-04-19 08:25:56 +00:00
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
}