github.com/wfusion/gofusion@v1.1.14/common/utils/serialize/algorithm_enum.go (about) 1 package serialize 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 AlgorithmGob 15 AlgorithmJson 16 AlgorithmMsgpack 17 AlgorithmCbor 18 ) 19 20 var ( 21 algorithmEnum = utils.NewEnumString[Algorithm, []Algorithm]( 22 map[Algorithm]string{ 23 AlgorithmGob: "gob", 24 AlgorithmJson: "json", 25 AlgorithmMsgpack: "msgpack", 26 AlgorithmCbor: "cbor", 27 }, 28 ) 29 ) 30 31 func (e Algorithm) Value() uint8 { 32 return uint8(e) 33 } 34 35 func (e Algorithm) String() string { 36 return algorithmEnum.String(e) 37 } 38 39 func (e Algorithm) IsValid() bool { 40 return algorithmEnum.IsValid(e) 41 } 42 43 func ParseAlgorithm(s any) Algorithm { 44 switch v := s.(type) { 45 case string: 46 if enumList := algorithmEnum.Enum(v); len(enumList) > 0 { 47 return enumList[0] 48 } 49 case Algorithm: 50 return v 51 default: 52 return Algorithm(cast.ToInt(s)) 53 } 54 return AlgorithmUnknown 55 }