50 lines
773 B
Go
50 lines
773 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 (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
|
|
}
|