Ignore struct fields tagged with "-"

This commit is contained in:
Hector Jusforgues
2016-04-09 15:10:53 +07:00
parent c1b2bdc108
commit f5458ea253
3 changed files with 22 additions and 2 deletions
+9 -2
View File
@@ -146,9 +146,16 @@ func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField)
omitEmpty := g.omitEmpty
for i, s := range strings.Split(f.Tag.Get("json"), ",") {
if i > 0 && s == "omitempty" {
if i == 0 {
if s == "-" {
return nil
}
continue
}
if s == "omitempty" {
omitEmpty = true
} else if i > 0 && s == "!omitempty" {
} else if s == "!omitempty" {
omitEmpty = false
}
}
+2
View File
@@ -5,6 +5,7 @@ import (
"testing"
"encoding/json"
"github.com/mailru/easyjson"
)
@@ -27,6 +28,7 @@ var testCases = []struct {
{&rawValue, rawString},
{&stdMarshalerValue, stdMarshalerString},
{&unexportedStructValue, unexportedStructString},
{&excludedFieldValue, excludedFieldString},
}
func TestMarshal(t *testing.T) {
+11
View File
@@ -362,3 +362,14 @@ type unexportedStruct struct {
var unexportedStructValue = unexportedStruct{"test"}
var unexportedStructString = `{"Value":"test"}`
type ExcludedField struct {
Process bool `json:"process"`
DoNotProcess bool `json:"-"`
}
var excludedFieldValue = ExcludedField{
Process: true,
DoNotProcess: false,
}
var excludedFieldString = `{"process":true}`