Skip to content

Commit dcd9091

Browse files
author
Dean Karn
committed
Correct bug in array index logic
fixes #33
1 parent 6b988ba commit dcd9091

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package form
22
============
3-
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">![Project status](https://img.shields.io/badge/version-3.1.1-green.svg)
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">![Project status](https://img.shields.io/badge/version-3.1.2-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/form/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/form)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/form/badge.svg?branch=master)](https://coveralls.io/github/go-playground/form?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/form)](https://goreportcard.com/report/github.com/go-playground/form)
@@ -23,7 +23,8 @@ It has the following features:
2323

2424
Common Questions
2525

26-
- Does it support encoding.TextUnmarshaler? No because TextUnmarshaler only accepts []byte but posted values can have multiple values, so is not suitable.
26+
- Does it support encoding.TextUnmarshaler? No because TextUnmarshaler only accepts []byte but posted values can have multiple values, so is not suitable.
27+
- Mixing `array/slice` with `array[idx]/slice[idx]`, in which order are they parsed? `array/slice` then `array[idx]/slice[idx]`
2728

2829
Supported Types ( out of the box )
2930
----------

decoder.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
464464

465465
if ok && len(arr) > 0 {
466466
var varr reflect.Value
467-
468-
overCapacity := v.Len() < len(arr)
467+
l := len(arr)
468+
overCapacity := v.Len() < l
469469
if overCapacity {
470470
// more values than array capacity, ignore values over capacity as it's possible some would just want
471471
// to grab the first x number of elements; in the future strict mode logic should return an error
@@ -474,10 +474,13 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
474474
varr = reflect.Indirect(reflect.New(reflect.ArrayOf(v.Len(), v.Type().Elem())))
475475
reflect.Copy(varr, v)
476476

477-
for i := 0; i < v.Len(); i++ {
477+
if v.Len() < len(arr) {
478+
l = v.Len()
479+
}
480+
for i := 0; i < l; i++ {
478481
newVal := reflect.New(v.Type().Elem()).Elem()
479482

480-
if d.setFieldByType(newVal, namespace, 0) {
483+
if d.setFieldByType(newVal, namespace, i) {
481484
set = true
482485
varr.Index(i).Set(newVal)
483486
}

decoder_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,10 @@ func TestDecodeArrayBug(t *testing.T) {
15711571
A [2]string
15721572
B [2]string
15731573
C [2]string
1574+
D [3]string
1575+
E [3]string
1576+
F [3]string
1577+
G [3]string
15741578
}
15751579
decoder := NewDecoder()
15761580
err := decoder.Decode(&data, url.Values{
@@ -1583,11 +1587,31 @@ func TestDecodeArrayBug(t *testing.T) {
15831587
"B[2]": {"40"},
15841588
// invalid array index
15851589
"C[q]": {""},
1590+
// index and mix tests
1591+
"D": {"10"},
1592+
"E": {"10", "20"},
1593+
"F": {"10", "", "20"},
1594+
"G": {"10"},
1595+
"G[2]": {"20"},
15861596
})
15871597
NotEqual(t, err, nil)
15881598
Equal(t, err.Error(), "Field Namespace:C ERROR:invalid array index 'q'")
15891599
Equal(t, data.A[0], "10")
15901600
Equal(t, data.A[1], "20")
15911601
Equal(t, data.B[0], "10")
15921602
Equal(t, data.B[1], "31")
1603+
Equal(t, data.C[0], "")
1604+
Equal(t, data.C[1], "")
1605+
Equal(t, data.D[0], "10")
1606+
Equal(t, data.D[1], "")
1607+
Equal(t, data.D[2], "")
1608+
Equal(t, data.E[0], "10")
1609+
Equal(t, data.E[1], "20")
1610+
Equal(t, data.E[2], "")
1611+
Equal(t, data.F[0], "10")
1612+
Equal(t, data.F[1], "")
1613+
Equal(t, data.F[2], "20")
1614+
Equal(t, data.G[0], "10")
1615+
Equal(t, data.G[1], "")
1616+
Equal(t, data.G[2], "20")
15931617
}

doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Questions
3131
No because TextUnmarshaler only accepts []byte but posted values can have
3232
multiple values, so is not suitable.
3333
34+
Mixing array/slice with array[idx]/slice[idx], in which order are they parsed?
35+
array/slice then array[idx]/slice[idx]
36+
3437
Supported Types
3538
3639
out of the box supported types

0 commit comments

Comments
 (0)