Skip to content

Commit

Permalink
minor restructuring and removing unused error
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jul 1, 2024
1 parent cd764ac commit b218b27
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
38 changes: 18 additions & 20 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ const (
DefaultSigningKey = "default-signing-key"
)

// Options contains the options for a cookie.
type Options struct {
Path string
Domain string
Expires time.Time
MaxAge int
Secure bool
HttpOnly bool
SameSite http.SameSite
Signed bool
}

var (
// SigningKey is the key used to sign cookies.
SigningKey = []byte(DefaultSigningKey)
Expand All @@ -37,34 +49,20 @@ var (
)

var (
// ErrUnsupportedType is returned when a field type is not supported by PopulateFromCookies.
ErrUnsupportedType = errors.New("cookie: unsupported type")

// ErrInvalidSignedCookieFormat is returned when a signed cookie is not in the correct format.
ErrInvalidSignedCookieFormat = errors.New("cookie: invalid signed cookie format")
)

// UnsupportedTypeError is returned when a field type is not supported by PopulateFromCookies.
type UnsupportedTypeError struct {
// ErrUnsupportedType is returned when a field type is not supported.
type ErrUnsupportedType struct {
Type reflect.Type
}

func (e *UnsupportedTypeError) Error() string {
// Error returns the error message.
func (e *ErrUnsupportedType) Error() string {
return "cookie: unsupported type: " + e.Type.String()
}

// Options contains the options for a cookie.
type Options struct {
Path string
Domain string
Expires time.Time
MaxAge int
Secure bool
HttpOnly bool
SameSite http.SameSite
Signed bool
}

// Set sets a cookie with the given name, value, and options.
func Set(w http.ResponseWriter, name, value string, options *Options) {
mergedOptions := mergeOptions(options, DefaultOptions)
Expand Down Expand Up @@ -219,7 +217,7 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error {
}
field.Set(reflect.ValueOf(intSlice))
default:
return &UnsupportedTypeError{fieldType.Type}
return &ErrUnsupportedType{fieldType.Type}
}
case reflect.Array:
if fieldType.Type == reflect.TypeOf(uuid.UUID{}) {
Expand All @@ -238,7 +236,7 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error {
field.Set(reflect.ValueOf(timeVal))
}
default:
return &UnsupportedTypeError{fieldType.Type}
return &ErrUnsupportedType{fieldType.Type}
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ func TestPopulateFromCookies_UnexpectedSliceType(t *testing.T) {
t.Error("Expected error, got nil")
}

if _, ok := err.(*UnsupportedTypeError); !ok {
t.Errorf("Expected error of type UnsupportedTypeError, got %T", err)
if _, ok := err.(*ErrUnsupportedType); !ok {
t.Errorf("Expected error of type ErrUnsupportedType, got %T", err)
}
}

Expand Down Expand Up @@ -520,8 +520,8 @@ func TestPopulateFromCookies_UnsupportedType(t *testing.T) {
t.Error("Expected error, got nil")
}

if _, ok := err.(*UnsupportedTypeError); !ok {
t.Errorf("Expected error of type UnsupportedTypeError, got %T", err)
if _, ok := err.(*ErrUnsupportedType); !ok {
t.Errorf("Expected error of type ErrUnsupportedType, got %T", err)
}

expected := "cookie: unsupported type: complex64"
Expand Down

0 comments on commit b218b27

Please sign in to comment.