-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: form.Marshaler
and form.Unmarshaler
#63
base: master
Are you sure you want to change the base?
Changes from 9 commits
97397eb
2d15be0
ab25a15
ef112dc
eab201f
e904ed6
6270911
54d6dbf
bc69006
58d5d48
8eda55b
98b9046
b045670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,6 @@ func (d *decoder) parseMapData() { | |
} | ||
|
||
for i = 0; i < len(k); i++ { | ||
|
||
switch k[i] { | ||
case '[': | ||
idx = i | ||
|
@@ -117,7 +116,6 @@ func (d *decoder) parseMapData() { | |
|
||
if ke.ivalue > rd.sliceLen { | ||
rd.sliceLen = ke.ivalue | ||
|
||
} | ||
} | ||
|
||
|
@@ -140,7 +138,6 @@ func (d *decoder) parseMapData() { | |
} | ||
|
||
func (d *decoder) traverseStruct(v reflect.Value, typ reflect.Type, namespace []byte) (set bool) { | ||
|
||
l := len(namespace) | ||
first := l == 0 | ||
|
||
|
@@ -177,14 +174,12 @@ func (d *decoder) traverseStruct(v reflect.Value, typ reflect.Type, namespace [] | |
} | ||
|
||
func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx int) (set bool) { | ||
|
||
var err error | ||
v, kind := ExtractType(current) | ||
|
||
arr, ok := d.values[string(namespace)] | ||
|
||
if d.d.customTypeFuncs != nil { | ||
|
||
if ok { | ||
if cf, ok := d.d.customTypeFuncs[v.Type()]; ok { | ||
val, err := cf(arr[idx:]) | ||
|
@@ -199,6 +194,27 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in | |
} | ||
} | ||
} | ||
{ | ||
v := v // deliberately shadow v as to not modify | ||
t := v.Type() | ||
if t.Kind() != reflect.Ptr && v.CanAddr() { | ||
v = v.Addr() | ||
} | ||
if um, ok := v.Interface().(Unmarshaler); ok { | ||
// if receiver is a nil pointer, set before calling function. | ||
if t.Kind() == reflect.Ptr && v.IsNil() { | ||
t = t.Elem() | ||
v.Set(reflect.New(t)) | ||
um = v.Interface().(Unmarshaler) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any way to avoid doing this/duplicating the below this same logic exists for It would be great if it's possible to keep the flow of the program using that same logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have a look at this and see if anything can be done |
||
if err = um.UnmarshalForm(arr[idx:]); err != nil { | ||
d.setError(namespace, err) | ||
return | ||
} | ||
set = true | ||
return | ||
} | ||
} | ||
switch kind { | ||
case reflect.Interface: | ||
if !ok || idx == len(arr) { | ||
|
@@ -602,7 +618,6 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in | |
} | ||
|
||
func (d *decoder) getMapKey(key string, current reflect.Value, namespace []byte) (err error) { | ||
|
||
v, kind := ExtractType(current) | ||
|
||
if d.d.customTypeFuncs != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the extra scope is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added so that v could be shadowed and modified without changing the code later on, but I'll just assign all modifications to a new var instead.