refactor(cookies,encoding): move loop to it's dedicated function

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-07-14 13:39:14 -03:00
parent 8dcc720e9f
commit 63d71cd625

View File

@@ -1,7 +1,7 @@
package cookies package cookies
import ( import (
"errors" e "errors"
"math/bits" "math/bits"
"net/http" "net/http"
"reflect" "reflect"
@@ -21,31 +21,22 @@ func Marshal[T any](c TypedCookie[T]) (http.Cookie, error) {
} }
v := []string{} v := []string{}
for i := 0; i < rv.NumField(); i++ { err := forEachField(&rv, func(fv *reflect.Value, ft *reflect.StructField) error {
f := rv.Type().Field(i) t := ft.Tag.Get("cookie")
fv := rv.FieldByName(f.Name)
if fv.Kind() == reflect.Pointer {
fv = fv.Elem()
}
if !fv.IsValid() {
return http.Cookie{}, errors.New("No such field in object: " + f.Name)
}
t := f.Tag.Get("cookie")
if t == "" { if t == "" {
t = f.Name t = ft.Name
} }
tv := strings.Split(t, ",") tv := strings.Split(t, ",")
value, err := encodeString(fv) value, err := encodeString(*fv)
if err != nil { if err != nil {
return http.Cookie{}, errors.Join(errors.New("Unsupportted type in struct: "+fv.Kind().String()), err) return e.Join(e.New("Unsupported type in struct: "+fv.Kind().String()), err)
} }
v = append(v, tv[0]+":"+value) v = append(v, tv[0]+":"+value)
}
return nil
})
return http.Cookie{ return http.Cookie{
Name: c.Name, Name: c.Name,
@@ -58,19 +49,22 @@ func Marshal[T any](c TypedCookie[T]) (http.Cookie, error) {
Secure: c.Secure, Secure: c.Secure,
HttpOnly: c.HttpOnly, HttpOnly: c.HttpOnly,
SameSite: c.SameSite, SameSite: c.SameSite,
}, nil }, err
} }
func Unmarshal[T any](data http.Cookie, v *TypedCookie[T]) error { func Unmarshal[T any](data http.Cookie, v *TypedCookie[T]) error {
if reflect.ValueOf(v).Kind() != reflect.Pointer { if reflect.ValueOf(v).Kind() != reflect.Pointer {
return errors.New("`v` is not a pointer: " + reflect.ValueOf(v).Kind().String()) return e.New("`v` is not a pointer: " + reflect.ValueOf(v).Kind().String())
}
if reflect.TypeOf(&v.TypedValue) == nil {
return e.New("TypedCookie.TypedValue is not a valid struct type")
} }
m := make(map[string]string) m := make(map[string]string)
for _, pair := range strings.Split(data.Value, "|") { for _, pair := range strings.Split(data.Value, "|") {
pairV := strings.Split(pair, ":") pairV := strings.Split(pair, ":")
if len(pairV) == 0 { if len(pairV) == 0 {
return errors.New("Error trying to decode cookie value:\n" + data.Value + "\n\nMissing \":\" pair in first slice") return e.New("Error trying to decode cookie value:\n" + data.Value + "\n\nMissing \":\" pair in first slice")
} }
key := pairV[0] key := pairV[0]
@@ -85,55 +79,65 @@ func Unmarshal[T any](data http.Cookie, v *TypedCookie[T]) error {
m[key] = value m[key] = value
} }
tt := reflect.TypeOf(&v.TypedValue)
tv := reflect.ValueOf(&v.TypedValue) tv := reflect.ValueOf(&v.TypedValue)
if tt == nil {
return errors.New("TypedCookie.TypedValue is not a valid struct{} type")
}
if tt.Kind() == reflect.Pointer {
tt = tt.Elem()
}
if tv.Kind() == reflect.Pointer { if tv.Kind() == reflect.Pointer {
tv = tv.Elem() tv = tv.Elem()
} }
for i := 0; i < tt.NumField(); i++ { err := forEachField(&tv, func(fv *reflect.Value, ft *reflect.StructField) error {
f := tt.Field(i) t := ft.Tag.Get("cookie")
fv := tv.FieldByName(f.Name)
if fv.Kind() == reflect.Pointer {
fv = fv.Elem()
}
if !fv.IsValid() {
return errors.New("No such field in object: " + f.Name)
}
if !fv.CanSet() {
return errors.New("Cannot set value of such field in object: " + f.Name)
}
t := f.Tag.Get("cookie")
if t == "" { if t == "" {
t = f.Name t = ft.Name
} }
tk := strings.Split(t, ",")[0] tk := strings.Split(t, ",")[0]
final, err := decodeString(m[tk], fv.Kind()) final, err := decodeString(m[tk], fv.Kind())
if err != nil { if err != nil {
return errors.Join(errors.New("Unsupportted type in struct: "+fv.Kind().String()), err) return e.Join(e.New("Unsupported type in struct: "+fv.Kind().String()), err)
} }
if strings.Contains(strings.ToLower(fv.Kind().String()), "complex") { kStr := strings.ToLower(fv.Kind().String())
if strings.Contains(kStr, "complex") {
fv.SetComplex(final.(complex128)) fv.SetComplex(final.(complex128))
} else if strings.Contains(strings.ToLower(fv.Kind().String()), "uint") {
} else if strings.Contains(kStr, "uint") {
fv.SetUint(final.(uint64)) fv.SetUint(final.(uint64))
} else if strings.Contains(strings.ToLower(fv.Kind().String()), "int") {
} else if strings.Contains(kStr, "int") {
fv.SetInt(final.(int64)) fv.SetInt(final.(int64))
} else { } else {
fv.Set(reflect.ValueOf(final)) fv.Set(reflect.ValueOf(final))
} }
}
return nil return nil
})
return err
}
func forEachField(v *reflect.Value, callback func(fv *reflect.Value, ft *reflect.StructField) error) (err error) {
t := v.Type()
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
fv := v.FieldByName(ft.Name)
if fv.Kind() == reflect.Pointer {
fv = fv.Elem()
}
if !fv.IsValid() {
return e.New("No such field: " + ft.Name)
}
err = callback(&fv, &ft)
if err != nil {
return e.Join(e.New("Error while looping through value"), err)
}
}
return err
} }
func encodeString(v reflect.Value) (string, error) { func encodeString(v reflect.Value) (string, error) {
@@ -161,7 +165,7 @@ func encodeString(v reflect.Value) (string, error) {
return v.String(), nil return v.String(), nil
default: default:
return "", errors.ErrUnsupported return "", e.ErrUnsupported
} }
} }
@@ -209,7 +213,7 @@ func decodeString(v string, k reflect.Kind) (any, error) {
final = v final = v
default: default:
return nil, errors.ErrUnsupported return nil, e.ErrUnsupported
} }
return final, err return final, err