diff options
| author | Shulhan <ms@kilabit.info> | 2022-09-22 15:42:52 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-09-22 16:03:49 +0700 |
| commit | e601de4752e6f7570989b123dcc9fec6e63ffc32 (patch) | |
| tree | fdf58eeefddf3491d9a1f0c8d135e38698417480 /lib/http | |
| parent | 482e0091331d9fc5dd4487b173a737f09ca7757a (diff) | |
| download | pakakeh.go-e601de4752e6f7570989b123dcc9fec6e63ffc32.tar.xz | |
lib/http: support embedded field on UnmarshalForm
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/form.go | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/http/form.go b/lib/http/form.go index b691c319..8d5be400 100644 --- a/lib/http/form.go +++ b/lib/http/form.go @@ -44,14 +44,14 @@ func UnmarshalForm(in url.Values, out interface{}) (err error) { rtype reflect.Type = vout.Type() rkind reflect.Kind = rtype.Kind() - tstruct reflect.Type - field reflect.StructField - fval reflect.Value - fptr reflect.Value - key, val string - vals []string - x int - hasTag bool + field reflect.StructField + fval reflect.Value + fptr reflect.Value + key, val string + listFields []reflect.StructField + vals []string + x int + hasTag bool ) if rkind != reflect.Ptr { @@ -84,10 +84,15 @@ func UnmarshalForm(in url.Values, out interface{}) (err error) { } } - tstruct = rtype + listFields = reflect.VisibleFields(rtype) - for ; x < vout.NumField(); x++ { - field = tstruct.Field(x) + for ; x < len(listFields); x++ { + field = listFields[x] + + if field.Anonymous { + // Skip embedded field. + continue + } key, _, hasTag = libreflect.Tag(field, structTagKey) if len(key) == 0 && !hasTag { @@ -113,7 +118,7 @@ func UnmarshalForm(in url.Values, out interface{}) (err error) { // Now that we have the value, store it into field by its // type. - fval = vout.Field(x) + fval = vout.FieldByIndex(field.Index) rtype = fval.Type() rkind = fval.Kind() |
