Skip to content

Commit 7fecd71

Browse files
author
Dean Karn
authored
Merge pull request #26 from go-playground/interface-support
Add setting of tring to interface{} values
2 parents 7b3f7e7 + 69fce24 commit 7fecd71

File tree

6 files changed

+50
-86
lines changed

6 files changed

+50
-86
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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.0.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.0-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)

decoder.go

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (d *decoder) parseMapData() {
8585
l = len(d.dm) + 1
8686

8787
if l > cap(d.dm) {
88-
dm := make(dataMap, l, l)
88+
dm := make(dataMap, l)
8989
copy(dm, d.dm)
9090
rd = new(recursiveData)
9191
dm[len(d.dm)] = rd
@@ -180,15 +180,13 @@ func (d *decoder) traverseStruct(v reflect.Value, typ reflect.Type, namespace []
180180
func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx int) (set bool) {
181181

182182
var err error
183-
184183
v, kind := ExtractType(current)
185184

186185
arr, ok := d.values[string(namespace)]
187186

188187
if d.d.customTypeFuncs != nil {
189188

190189
if ok {
191-
192190
if cf, ok := d.d.customTypeFuncs[v.Type()]; ok {
193191
val, err := cf(arr)
194192
if err != nil {
@@ -202,10 +200,14 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
202200
}
203201
}
204202
}
205-
206203
switch kind {
207-
case reflect.Interface, reflect.Invalid:
208-
return
204+
case reflect.Interface:
205+
if !ok {
206+
return
207+
}
208+
v.Set(reflect.ValueOf(arr[idx]))
209+
set = true
210+
209211
case reflect.Ptr:
210212

211213
newVal := reflect.New(v.Type().Elem())
@@ -214,188 +216,145 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
214216
}
215217

216218
case reflect.String:
217-
218219
if !ok {
219220
return
220221
}
221-
222222
v.SetString(arr[idx])
223223
set = true
224224

225225
case reflect.Uint, reflect.Uint64:
226-
227226
if !ok || len(arr[idx]) == 0 {
228227
return
229228
}
230-
231229
var u64 uint64
232-
233230
if u64, err = strconv.ParseUint(arr[idx], 10, 64); err != nil {
234231
d.setError(namespace, fmt.Errorf("Invalid Unsigned Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
235232
return
236233
}
237-
238234
v.SetUint(u64)
239235
set = true
240236

241237
case reflect.Uint8:
242-
243238
if !ok || len(arr[idx]) == 0 {
244239
return
245240
}
246-
247241
var u64 uint64
248-
249242
if u64, err = strconv.ParseUint(arr[idx], 10, 8); err != nil {
250243
d.setError(namespace, fmt.Errorf("Invalid Unsigned Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
251244
return
252245
}
253-
254246
v.SetUint(u64)
255247
set = true
256248

257249
case reflect.Uint16:
258-
259250
if !ok || len(arr[idx]) == 0 {
260251
return
261252
}
262-
263253
var u64 uint64
264-
265254
if u64, err = strconv.ParseUint(arr[idx], 10, 16); err != nil {
266255
d.setError(namespace, fmt.Errorf("Invalid Unsigned Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
267256
return
268257
}
269-
270258
v.SetUint(u64)
271259
set = true
272260

273261
case reflect.Uint32:
274-
275262
if !ok || len(arr[idx]) == 0 {
276263
return
277264
}
278-
279265
var u64 uint64
280-
281266
if u64, err = strconv.ParseUint(arr[idx], 10, 32); err != nil {
282267
d.setError(namespace, fmt.Errorf("Invalid Unsigned Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
283268
return
284269
}
285-
286270
v.SetUint(u64)
287271
set = true
288272

289273
case reflect.Int, reflect.Int64:
290274
if !ok || len(arr[idx]) == 0 {
291275
return
292276
}
293-
294277
var i64 int64
295-
296278
if i64, err = strconv.ParseInt(arr[idx], 10, 64); err != nil {
297279
d.setError(namespace, fmt.Errorf("Invalid Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
298280
return
299281
}
300-
301282
v.SetInt(i64)
302283
set = true
303284

304285
case reflect.Int8:
305286
if !ok || len(arr[idx]) == 0 {
306287
return
307288
}
308-
309289
var i64 int64
310-
311290
if i64, err = strconv.ParseInt(arr[idx], 10, 8); err != nil {
312291
d.setError(namespace, fmt.Errorf("Invalid Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
313292
return
314293
}
315-
316294
v.SetInt(i64)
317295
set = true
318296

319297
case reflect.Int16:
320298
if !ok || len(arr[idx]) == 0 {
321299
return
322300
}
323-
324301
var i64 int64
325-
326302
if i64, err = strconv.ParseInt(arr[idx], 10, 16); err != nil {
327303
d.setError(namespace, fmt.Errorf("Invalid Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
328304
return
329305
}
330-
331306
v.SetInt(i64)
332307
set = true
333308

334309
case reflect.Int32:
335310
if !ok || len(arr[idx]) == 0 {
336311
return
337312
}
338-
339313
var i64 int64
340-
341314
if i64, err = strconv.ParseInt(arr[idx], 10, 32); err != nil {
342315
d.setError(namespace, fmt.Errorf("Invalid Integer Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
343316
return
344317
}
345-
346318
v.SetInt(i64)
347319
set = true
348320

349321
case reflect.Float32:
350-
351322
if !ok || len(arr[idx]) == 0 {
352323
return
353324
}
354-
355325
var f float64
356-
357326
if f, err = strconv.ParseFloat(arr[idx], 32); err != nil {
358327
d.setError(namespace, fmt.Errorf("Invalid Float Value '%s' Type '%v' Namespace '%s'", arr[0], v.Type(), string(namespace)))
359328
return
360329
}
361-
362330
v.SetFloat(f)
363331
set = true
364332

365333
case reflect.Float64:
366-
367334
if !ok || len(arr[idx]) == 0 {
368335
return
369336
}
370-
371337
var f float64
372-
373338
if f, err = strconv.ParseFloat(arr[idx], 64); err != nil {
374339
d.setError(namespace, fmt.Errorf("Invalid Float Value '%s' Type '%v' Namespace '%s'", arr[0], v.Type(), string(namespace)))
375340
return
376341
}
377-
378342
v.SetFloat(f)
379343
set = true
380344

381345
case reflect.Bool:
382-
383346
if !ok {
384347
return
385348
}
386-
387349
var b bool
388-
389350
if b, err = parseBool(arr[idx]); err != nil {
390351
d.setError(namespace, fmt.Errorf("Invalid Boolean Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), string(namespace)))
391352
return
392353
}
393-
394354
v.SetBool(b)
395355
set = true
396356

397357
case reflect.Slice, reflect.Array:
398-
399358
if !ok {
400359

401360
d.parseMapData()
@@ -504,7 +463,6 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
504463
v.Set(varr)
505464

506465
case reflect.Map:
507-
508466
var rd *recursiveData
509467

510468
d.parseMapData()
@@ -551,7 +509,6 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
551509
v.Set(mp)
552510

553511
case reflect.Struct:
554-
555512
typ := v.Type()
556513

557514
// if we get here then no custom time function declared so use RFC3339 by default
@@ -580,7 +537,6 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
580537

581538
set = d.traverseStruct(v, typ, namespace)
582539
}
583-
584540
return
585541
}
586542

decoder_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,3 +1547,21 @@ func TestDecoderEmbedModes(t *testing.T) {
15471547
Equal(t, b.Field, "B Val")
15481548
Equal(t, b.A.Field, "A Val")
15491549
}
1550+
1551+
func TestInterfaceDecoding(t *testing.T) {
1552+
1553+
type Test struct {
1554+
Iface interface{}
1555+
}
1556+
1557+
var b Test
1558+
1559+
values := url.Values{
1560+
"Iface": []string{"1"},
1561+
}
1562+
1563+
decoder := NewDecoder()
1564+
err := decoder.Decode(&b, values)
1565+
Equal(t, err, nil)
1566+
Equal(t, b.Iface, "1")
1567+
}

encoder.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ func (e *encoder) traverseStruct(v reflect.Value, namespace []byte, idx int) {
6565

6666
e.setFieldByType(v.Field(f.idx), namespace, idx, f.isOmitEmpty)
6767
}
68-
69-
return
7068
}
7169

7270
func (e *encoder) setFieldByType(current reflect.Value, namespace []byte, idx int, isOmitEmpty bool) {
@@ -214,8 +212,6 @@ func (e *encoder) setFieldByType(current reflect.Value, namespace []byte, idx in
214212

215213
e.traverseStruct(v, namespace, -2)
216214
}
217-
218-
return
219215
}
220216

221217
func (e *encoder) getMapKey(key reflect.Value, namespace []byte) (string, bool) {

0 commit comments

Comments
 (0)