feat: xsrf_token set
This commit is contained in:
parent
31e55c0539
commit
b79a6b9117
5 changed files with 117 additions and 20 deletions
33
modules/auth/helpers.go
Normal file
33
modules/auth/helpers.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func setField(obj interface{}, name string, value interface{}) error {
|
||||
v := reflect.ValueOf(obj)
|
||||
|
||||
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
|
||||
return fmt.Errorf("expected pointer to a struct")
|
||||
}
|
||||
|
||||
v = v.Elem()
|
||||
field := v.FieldByName(name)
|
||||
|
||||
if !field.IsValid() {
|
||||
return fmt.Errorf("no such field: %s", name)
|
||||
}
|
||||
if !field.CanSet() {
|
||||
return fmt.Errorf("cannot set field: %s", name)
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(value)
|
||||
|
||||
if field.Type() != val.Type() {
|
||||
return fmt.Errorf("provided value type (%s) doesn't match field type (%s)", val.Type(), field.Type())
|
||||
}
|
||||
|
||||
field.Set(val)
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue