Skip to content

Commit 572fa77

Browse files
author
Dean Karn
authored
Merge pull request #35 from go-playground/fix-encoder-pointer-omitempty
Fix Encoder Pointer + omitempty combination
2 parents dcd9091 + b02dbfe commit 572fa77

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
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.1.2-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.3-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)

encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ func (e *encoder) setFieldByType(current reflect.Value, namespace []byte, idx in
7676
idx = -2
7777
}
7878

79-
v, kind := ExtractType(current)
80-
if isOmitEmpty && !hasValue(v) {
79+
if isOmitEmpty && !hasValue(current) {
8180
return
8281
}
82+
v, kind := ExtractType(current)
8383

8484
if e.e.customTypeFuncs != nil {
8585

encoder_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func TestEncoderInt(t *testing.T) {
8383

8484
encoder := NewEncoder()
8585
values, errs := encoder.Encode(test)
86-
8786
Equal(t, errs, nil)
8887
Equal(t, len(values), 25)
8988

@@ -1366,4 +1365,22 @@ func TestOmitEmpty(t *testing.T) {
13661365
Equal(t, len(values), 2)
13671366
Equal(t, values["String"][0], "")
13681367
Equal(t, values["str"][0], "")
1368+
1369+
type T struct {
1370+
X *uint8 `form:"x,omitempty"`
1371+
Array []*string `form:"arr,omitempty"`
1372+
Array2 []*string `form:"arr2,dive,omitempty"`
1373+
}
1374+
x := uint8(0)
1375+
s := ""
1376+
tst4 := T{
1377+
X: &x,
1378+
Array: []*string{&s},
1379+
}
1380+
1381+
values, err = encoder.Encode(tst4)
1382+
Equal(t, err, nil)
1383+
Equal(t, len(values), 2)
1384+
Equal(t, values["x"][0], "0")
1385+
Equal(t, values["arr[0]"][0], "")
13691386
}

0 commit comments

Comments
 (0)