github.com/wfusion/gofusion@v1.1.14/common/utils/compress/algorithm_enum.go (about)

     1  package compress
     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  	AlgorithmZSTD
    15  	AlgorithmZLib
    16  	AlgorithmS2
    17  	AlgorithmGZip
    18  	AlgorithmDeflate
    19  )
    20  
    21  var (
    22  	algorithmEnum = utils.NewEnumString[Algorithm, []Algorithm](
    23  		map[Algorithm]string{
    24  			AlgorithmZSTD:    "zstd",
    25  			AlgorithmZLib:    "zlib",
    26  			AlgorithmS2:      "s2",
    27  			AlgorithmGZip:    "gzip",
    28  			AlgorithmDeflate: "deflate",
    29  		},
    30  	)
    31  )
    32  
    33  func (e Algorithm) Value() uint8 {
    34  	return uint8(e)
    35  }
    36  
    37  func (e Algorithm) String() string {
    38  	return algorithmEnum.String(e)
    39  }
    40  
    41  func (e Algorithm) IsValid() bool {
    42  	return algorithmEnum.IsValid(e)
    43  }
    44  
    45  func ParseAlgorithm(s any) Algorithm {
    46  	switch v := s.(type) {
    47  	case string:
    48  		if enumList := algorithmEnum.Enum(v); len(enumList) > 0 {
    49  			return enumList[0]
    50  		}
    51  	case Algorithm:
    52  		return v
    53  	default:
    54  		return Algorithm(cast.ToInt(s))
    55  	}
    56  	return AlgorithmUnknown
    57  }