Skip to content

Commit

Permalink
Switch back to using UnmarshalText for validating compression types
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Jan 14, 2025
1 parent 2e1137c commit 953d333
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 14 additions & 7 deletions config/configcompression/compressiontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ const (

// IsCompressed returns false if CompressionType is nil, none, or empty.
// Otherwise, returns true.
func (t Type) IsCompressed() bool {
return t != typeEmpty && t != typeNone
func (ct *Type) IsCompressed() bool {
return *ct != typeEmpty && *ct != typeNone
}

func (t Type) Validate() error {
switch t {
case TypeGzip, TypeZlib, TypeDeflate, TypeSnappy, TypeZstd, TypeLz4,
typeNone, typeEmpty:
func (ct *Type) UnmarshalText(in []byte) error {
typ := Type(in)
if typ == TypeGzip ||
typ == TypeZlib ||
typ == TypeDeflate ||
typ == TypeSnappy ||
typ == TypeZstd ||
typ == TypeLz4 ||
typ == typeNone ||
typ == typeEmpty {
*ct = typ
return nil
}
return fmt.Errorf("unsupported compression type %q", t)
return fmt.Errorf("unsupported compression type %q", typ)
}

func (t Type) ValidateParams(p CompressionParams) error {
Expand Down
6 changes: 3 additions & 3 deletions config/configcompression/compressiontype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestValidate(t *testing.T) {
func TestUnmarshalText(t *testing.T) {
tests := []struct {
name string
compressionName []byte
Expand Down Expand Up @@ -72,8 +72,8 @@ func TestValidate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
temp := Type(tt.compressionName)
err := temp.Validate()
temp := typeNone
err := temp.UnmarshalText(tt.compressionName)
if tt.shouldError {
assert.Error(t, err)
return
Expand Down

0 comments on commit 953d333

Please sign in to comment.