Skip to content

Commit

Permalink
Merge pull request #35 from go-playground/fix-encoder-pointer-omitempty
Browse files Browse the repository at this point in the history
Fix Encoder Pointer + omitempty combination
  • Loading branch information
Dean Karn authored Apr 10, 2018
2 parents dcd9091 + b02dbfe commit 572fa77
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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.2-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.3-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 Down
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func (e *encoder) setFieldByType(current reflect.Value, namespace []byte, idx in
idx = -2
}

v, kind := ExtractType(current)
if isOmitEmpty && !hasValue(v) {
if isOmitEmpty && !hasValue(current) {
return
}
v, kind := ExtractType(current)

if e.e.customTypeFuncs != nil {

Expand Down
19 changes: 18 additions & 1 deletion encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func TestEncoderInt(t *testing.T) {

encoder := NewEncoder()
values, errs := encoder.Encode(test)

Equal(t, errs, nil)
Equal(t, len(values), 25)

Expand Down Expand Up @@ -1366,4 +1365,22 @@ func TestOmitEmpty(t *testing.T) {
Equal(t, len(values), 2)
Equal(t, values["String"][0], "")
Equal(t, values["str"][0], "")

type T struct {
X *uint8 `form:"x,omitempty"`
Array []*string `form:"arr,omitempty"`
Array2 []*string `form:"arr2,dive,omitempty"`
}
x := uint8(0)
s := ""
tst4 := T{
X: &x,
Array: []*string{&s},
}

values, err = encoder.Encode(tst4)
Equal(t, err, nil)
Equal(t, len(values), 2)
Equal(t, values["x"][0], "0")
Equal(t, values["arr[0]"][0], "")
}

0 comments on commit 572fa77

Please sign in to comment.