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

54 lines
843 B
Go
Raw Normal View History

2018-04-19 08:25:56 +00:00
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,
}
)
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
}