github.com/wfusion/gofusion@v1.1.14/common/utils/encode/algorithm_enum.go (about) 1 package encode 2 3 import ( 4 "github.com/spf13/cast" 5 6 "github.com/wfusion/gofusion/common/utils" 7 ) 8 9 //go:generate stringer -type=Algorithm -trimprefix=Algorithm 10 type Algorithm uint8 11 12 const ( 13 AlgorithmUnknown Algorithm = iota 14 AlgorithmHex 15 AlgorithmBase32Std 16 AlgorithmBase32Hex 17 AlgorithmBase64Std 18 AlgorithmBase64URL 19 AlgorithmBase64RawStd // without padding 20 AlgorithmBase64RawURL // without padding 21 ) 22 23 var ( 24 algorithmEnum = utils.NewEnumString[Algorithm, []Algorithm]( 25 map[Algorithm]string{ 26 AlgorithmHex: "hex", 27 AlgorithmBase32Std: "base32", 28 AlgorithmBase32Hex: "base32-hex", 29 AlgorithmBase64Std: "base64", 30 AlgorithmBase64URL: "base64-url", 31 AlgorithmBase64RawStd: "base64-raw", 32 AlgorithmBase64RawURL: "base64-raw-url", 33 }, 34 ) 35 ) 36 37 func (e Algorithm) Value() uint8 { 38 return uint8(e) 39 } 40 41 func (e Algorithm) String() string { 42 return algorithmEnum.String(e) 43 } 44 45 func (e Algorithm) IsValid() bool { 46 return algorithmEnum.IsValid(e) 47 } 48 49 func ParseAlgorithm(s any) Algorithm { 50 switch v := s.(type) { 51 case string: 52 if enumList := algorithmEnum.Enum(v); len(enumList) > 0 { 53 return enumList[0] 54 } 55 case Algorithm: 56 return v 57 default: 58 return Algorithm(cast.ToInt(s)) 59 } 60 return AlgorithmUnknown 61 }