Skip to content

Commit 8367b06

Browse files
committed
fix(marshal): omit fields tagged with "-"
This is a behavior of json.Marshal() that should be emulated
1 parent ff76c24 commit 8367b06

4 files changed

Lines changed: 59 additions & 9 deletions

File tree

internal/path/path.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ import (
55
"strings"
66
)
77

8-
const omitEmptyToken string = "omitempty"
8+
const (
9+
omitEmptyToken string = "omitempty"
10+
omitAlwaysToken string = "-"
11+
)
912

1013
type Segment struct {
1114
Name string
1215
List bool
1316
}
1417

1518
type Path struct {
16-
segments []Segment
17-
OmitEmpty bool
19+
segments []Segment
20+
OmitEmpty bool
21+
OmitAlways bool
1822
}
1923

2024
func (p Path) Len() int {
@@ -44,11 +48,12 @@ func ComputePath(field reflect.StructField) Path {
4448
var segments []Segment
4549
name := field.Name
4650
omitempty := false
51+
omitalways := false
4752

4853
if tag := field.Tag.Get("json"); tag != "" {
49-
name, omitempty = parseTag(tag, field.Name)
54+
name, omitempty, omitalways = parseTag(tag, field.Name)
5055
} else if tag := field.Tag.Get("jsonry"); tag != "" {
51-
name, omitempty = parseTag(tag, field.Name)
56+
name, omitempty, omitalways = parseTag(tag, field.Name)
5257
segments = parseSegments(name)
5358
}
5459

@@ -60,12 +65,17 @@ func ComputePath(field reflect.StructField) Path {
6065
}
6166

6267
return Path{
63-
OmitEmpty: omitempty,
64-
segments: segments,
68+
OmitEmpty: omitempty,
69+
OmitAlways: omitalways,
70+
segments: segments,
6571
}
6672
}
6773

68-
func parseTag(tag, defaultName string) (name string, omitempty bool) {
74+
func parseTag(tag, defaultName string) (name string, omitempty, omitalways bool) {
75+
if tag == omitAlwaysToken {
76+
return defaultName, false, true
77+
}
78+
6979
parts := strings.Split(tag, ",")
7080

7181
if len(parts) >= 1 && len(parts[0]) > 0 {

internal/path/path_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,26 @@ var _ = Describe("Path", func() {
5151
Expect(p.OmitEmpty).To(BeTrue())
5252
})
5353
})
54+
55+
Context("always omit", func() {
56+
It("picks it up from a JSON tag", func() {
57+
p := path.ComputePath(reflect.StructField{Tag: `json:"-"`})
58+
Expect(p.OmitAlways).To(BeTrue())
59+
})
60+
61+
It("picks it up from a JSONry tag", func() {
62+
p := path.ComputePath(reflect.StructField{Tag: `jsonry:"-"`})
63+
Expect(p.OmitAlways).To(BeTrue())
64+
})
65+
66+
It("allows a literal name `-` from a JSON tag", func() {
67+
p := path.ComputePath(reflect.StructField{Tag: `json:"-,"`})
68+
Expect(p.OmitAlways).To(BeFalse())
69+
})
70+
71+
It("allows a literal name `-` from a JSONry tag", func() {
72+
p := path.ComputePath(reflect.StructField{Tag: `jsonry:"-,"`})
73+
Expect(p.OmitAlways).To(BeFalse())
74+
})
75+
})
5476
})

marshal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func marshalStruct(ctx context.Context, in reflect.Value) (map[string]interface{
4444

4545
if public(f) {
4646
p := path.ComputePath(f)
47-
if !p.OmitEmpty || !in.Field(i).IsZero() {
47+
shouldSkip := p.OmitAlways || (p.OmitEmpty && in.Field(i).IsZero())
48+
49+
if !shouldSkip {
4850
r, err := marshal(ctx.WithField(f.Name, f.Type), in.Field(i))
4951
if err != nil {
5052
return nil, err

marshal_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ var _ = Describe("Marshal", func() {
256256
})
257257
})
258258

259+
Describe("omitting struct fields", func() {
260+
It("omits struct fields tagged with `-`", func() {
261+
type s struct {
262+
A string `jsonry:"-"`
263+
}
264+
expectToMarshal(s{A: "foo"}, `{}`)
265+
})
266+
267+
It("allows literal field name `-` ", func() {
268+
type s struct {
269+
A string `jsonry:"-,"`
270+
}
271+
expectToMarshal(s{A: "foo"}, `{"-":"foo"}`)
272+
})
273+
})
274+
259275
Describe("inputs", func() {
260276
It("accept a struct", func() {
261277
var s struct{}

0 commit comments

Comments
 (0)