Skip to content

Commit d77a15c

Browse files
joeybloggsjoeybloggs
authored andcommitted
Correct bug for in array of struct.
for issue-#1
1 parent 24f1832 commit d77a15c

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package form
22
============
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">
4-
![Project status](https://img.shields.io/badge/version-1.2.0-green.svg)
4+
![Project status](https://img.shields.io/badge/version-1.2.1-green.svg)
55
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/form/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/form)
66
[![Coverage Status](https://coveralls.io/repos/github/go-playground/form/badge.svg?branch=master)](https://coveralls.io/github/go-playground/form?branch=master)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/form)](https://goreportcard.com/report/github.com/go-playground/form)

decoder.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ func (d *decoder) traverseStruct(v reflect.Value, namespace string) (set bool) {
152152
nn = namespace + namespaceSeparator + key
153153
}
154154

155-
set = d.setFieldByType(v.Field(i), nn, 0)
155+
if d.setFieldByType(v.Field(i), nn, 0) {
156+
set = true
157+
}
158+
156159
}
157160
} else {
158161
s, ok := d.d.structCache.Get(typ)
@@ -168,7 +171,9 @@ func (d *decoder) traverseStruct(v reflect.Value, namespace string) (set bool) {
168171
nn = namespace + namespaceSeparator + f.name
169172
}
170173

171-
set = d.setFieldByType(v.Field(f.idx), nn, 0)
174+
if d.setFieldByType(v.Field(f.idx), nn, 0) {
175+
set = true
176+
}
172177
}
173178
}
174179

form_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,36 @@ func TestStructRecursion(t *testing.T) {
797797
Equal(t, test.Nested.Value, "value")
798798
Equal(t, test.Nested.Nested, nil)
799799
}
800+
801+
func TestFormDecode(t *testing.T) {
802+
803+
type Struct2 struct {
804+
Foo string
805+
Bar string
806+
}
807+
808+
type Struct2Wrapper struct {
809+
InnerSlice []Struct2
810+
}
811+
812+
sliceValues := map[string][]string{
813+
"InnerSlice[0].Foo": {"foo-is-set"},
814+
}
815+
816+
singleValues := map[string][]string{
817+
"Foo": {"foo-is-set"},
818+
}
819+
820+
fd := NewDecoder()
821+
822+
dst := Struct2Wrapper{}
823+
err := fd.Decode(&dst, sliceValues)
824+
Equal(t, err, nil)
825+
NotEqual(t, dst.InnerSlice, nil)
826+
Equal(t, dst.InnerSlice[0].Foo, "foo-is-set")
827+
828+
dst2 := Struct2{}
829+
err = fd.Decode(&dst2, singleValues)
830+
Equal(t, err, nil)
831+
Equal(t, dst2.Foo, "foo-is-set")
832+
}

0 commit comments

Comments
 (0)