Skip to content

Commit

Permalink
use own helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jun 28, 2024
1 parent 4b50255 commit adc84b5
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error {
continue
}

cookie, err := r.Cookie(tag)
cookie, err := Get(r, tag)
if err != nil {
return err
}

switch field.Kind() {
case reflect.String:
field.SetString(cookie.Value)
field.SetString(cookie)
case reflect.Int:
intVal, err := strconv.Atoi(cookie.Value)
intVal, err := strconv.Atoi(cookie)
if err != nil {
return err
}
field.SetInt(int64(intVal))
case reflect.Bool:
boolVal, err := strconv.ParseBool(cookie.Value)
boolVal, err := strconv.ParseBool(cookie)
if err != nil {
return err
}
field.SetBool(boolVal)
case reflect.Slice:
switch fieldType.Type.Elem().Kind() {
case reflect.String:
field.Set(reflect.ValueOf(strings.Split(cookie.Value, ",")))
field.Set(reflect.ValueOf(strings.Split(cookie, ",")))
case reflect.Int:
intStrings := strings.Split(cookie.Value, ",")
intStrings := strings.Split(cookie, ",")
intSlice := make([]int, len(intStrings))
for i, s := range intStrings {
intVal, err := strconv.Atoi(s)
Expand All @@ -63,15 +63,15 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error {
case reflect.Array:
// Handle uuid.UUID separately
if fieldType.Type == reflect.TypeOf(uuid.UUID{}) {
uid, err := uuid.FromString(cookie.Value)
uid, err := uuid.FromString(cookie)
if err != nil {
return err
}
field.Set(reflect.ValueOf(uid))
}
case reflect.Struct:
if fieldType.Type == reflect.TypeOf(time.Time{}) {
timeVal, err := time.Parse(time.RFC3339, cookie.Value)
timeVal, err := time.Parse(time.RFC3339, cookie)
if err != nil {
return err
}
Expand Down

0 comments on commit adc84b5

Please sign in to comment.