Skip to content

Commit

Permalink
Correct bug in array index logic
Browse files Browse the repository at this point in the history
fixes #33
  • Loading branch information
Dean Karn committed Apr 8, 2018
1 parent 6b988ba commit dcd9091
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package form
============
<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)
<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)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/form/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/form)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/form/badge.svg?branch=master)](https://coveralls.io/github/go-playground/form?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/form)](https://goreportcard.com/report/github.com/go-playground/form)
Expand All @@ -23,7 +23,8 @@ It has the following features:

Common Questions

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

Supported Types ( out of the box )
----------
Expand Down
11 changes: 7 additions & 4 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in

if ok && len(arr) > 0 {
var varr reflect.Value

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

for i := 0; i < v.Len(); i++ {
if v.Len() < len(arr) {
l = v.Len()
}
for i := 0; i < l; i++ {
newVal := reflect.New(v.Type().Elem()).Elem()

if d.setFieldByType(newVal, namespace, 0) {
if d.setFieldByType(newVal, namespace, i) {
set = true
varr.Index(i).Set(newVal)
}
Expand Down
24 changes: 24 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,10 @@ func TestDecodeArrayBug(t *testing.T) {
A [2]string
B [2]string
C [2]string
D [3]string
E [3]string
F [3]string
G [3]string
}
decoder := NewDecoder()
err := decoder.Decode(&data, url.Values{
Expand All @@ -1583,11 +1587,31 @@ func TestDecodeArrayBug(t *testing.T) {
"B[2]": {"40"},
// invalid array index
"C[q]": {""},
// index and mix tests
"D": {"10"},
"E": {"10", "20"},
"F": {"10", "", "20"},
"G": {"10"},
"G[2]": {"20"},
})
NotEqual(t, err, nil)
Equal(t, err.Error(), "Field Namespace:C ERROR:invalid array index 'q'")
Equal(t, data.A[0], "10")
Equal(t, data.A[1], "20")
Equal(t, data.B[0], "10")
Equal(t, data.B[1], "31")
Equal(t, data.C[0], "")
Equal(t, data.C[1], "")
Equal(t, data.D[0], "10")
Equal(t, data.D[1], "")
Equal(t, data.D[2], "")
Equal(t, data.E[0], "10")
Equal(t, data.E[1], "20")
Equal(t, data.E[2], "")
Equal(t, data.F[0], "10")
Equal(t, data.F[1], "")
Equal(t, data.F[2], "20")
Equal(t, data.G[0], "10")
Equal(t, data.G[1], "")
Equal(t, data.G[2], "20")
}
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Questions
No because TextUnmarshaler only accepts []byte but posted values can have
multiple values, so is not suitable.
Mixing array/slice with array[idx]/slice[idx], in which order are they parsed?
array/slice then array[idx]/slice[idx]
Supported Types
out of the box supported types
Expand Down

0 comments on commit dcd9091

Please sign in to comment.