Skip to content

Commit af0016d

Browse files
authored
Merge pull request #159 from thockin/master
funcr: JSON invalid output with 1st field omitted
2 parents 4497483 + a8aea2e commit af0016d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

funcr/funcr.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func (f Formatter) prettyWithFlags(value interface{}, flags uint32, depth int) s
448448
if flags&flagRawStruct == 0 {
449449
buf.WriteByte('{')
450450
}
451+
printComma := false // testing i>0 is not enough because of JSON omitted fields
451452
for i := 0; i < t.NumField(); i++ {
452453
fld := t.Field(i)
453454
if fld.PkgPath != "" {
@@ -479,9 +480,10 @@ func (f Formatter) prettyWithFlags(value interface{}, flags uint32, depth int) s
479480
if omitempty && isEmpty(v.Field(i)) {
480481
continue
481482
}
482-
if i > 0 {
483+
if printComma {
483484
buf.WriteByte(',')
484485
}
486+
printComma = true // if we got here, we are rendering a field
485487
if fld.Anonymous && fld.Type.Kind() == reflect.Struct && name == "" {
486488
buf.WriteString(f.prettyWithFlags(v.Field(i).Interface(), flags|flagRawStruct, depth+1))
487489
continue

funcr/funcr_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (t Terrorpanic) Error() string {
106106
}
107107

108108
type TjsontagsString struct {
109+
String0 string `json:"-"` // first field ignored
109110
String1 string `json:"string1"` // renamed
110111
String2 string `json:"-"` // ignored
111112
String3 string `json:"-,"` // named "-"
@@ -115,6 +116,7 @@ type TjsontagsString struct {
115116
}
116117

117118
type TjsontagsBool struct {
119+
Bool0 bool `json:"-"` // first field ignored
118120
Bool1 bool `json:"bool1"` // renamed
119121
Bool2 bool `json:"-"` // ignored
120122
Bool3 bool `json:"-,"` // named "-"
@@ -124,6 +126,7 @@ type TjsontagsBool struct {
124126
}
125127

126128
type TjsontagsInt struct {
129+
Int0 int `json:"-"` // first field ignored
127130
Int1 int `json:"int1"` // renamed
128131
Int2 int `json:"-"` // ignored
129132
Int3 int `json:"-,"` // named "-"
@@ -133,6 +136,7 @@ type TjsontagsInt struct {
133136
}
134137

135138
type TjsontagsUint struct {
139+
Uint0 int `json:"-"` // first field ignored
136140
Uint1 uint `json:"uint1"` // renamed
137141
Uint2 uint `json:"-"` // ignored
138142
Uint3 uint `json:"-,"` // named "-"
@@ -142,6 +146,7 @@ type TjsontagsUint struct {
142146
}
143147

144148
type TjsontagsFloat struct {
149+
Float0 float64 `json:"-"` // first field ignored
145150
Float1 float64 `json:"float1"` // renamed
146151
Float2 float64 `json:"-"` // ignored
147152
Float3 float64 `json:"-,"` // named "-"
@@ -151,6 +156,7 @@ type TjsontagsFloat struct {
151156
}
152157

153158
type TjsontagsComplex struct {
159+
Complex0 complex128 `json:"-"` // first field ignored
154160
Complex1 complex128 `json:"complex1"` // renamed
155161
Complex2 complex128 `json:"-"` // ignored
156162
Complex3 complex128 `json:"-,"` // named "-"
@@ -160,6 +166,7 @@ type TjsontagsComplex struct {
160166
}
161167

162168
type TjsontagsPtr struct {
169+
Ptr0 *string `json:"-"` // first field ignored
163170
Ptr1 *string `json:"ptr1"` // renamed
164171
Ptr2 *string `json:"-"` // ignored
165172
Ptr3 *string `json:"-,"` // named "-"
@@ -169,6 +176,7 @@ type TjsontagsPtr struct {
169176
}
170177

171178
type TjsontagsArray struct {
179+
Array0 [2]string `json:"-"` // first field ignored
172180
Array1 [2]string `json:"array1"` // renamed
173181
Array2 [2]string `json:"-"` // ignored
174182
Array3 [2]string `json:"-,"` // named "-"
@@ -178,6 +186,7 @@ type TjsontagsArray struct {
178186
}
179187

180188
type TjsontagsSlice struct {
189+
Slice0 []string `json:"-"` // first field ignored
181190
Slice1 []string `json:"slice1"` // renamed
182191
Slice2 []string `json:"-"` // ignored
183192
Slice3 []string `json:"-,"` // named "-"
@@ -187,6 +196,7 @@ type TjsontagsSlice struct {
187196
}
188197

189198
type TjsontagsMap struct {
199+
Map0 map[string]string `json:"-"` // first field ignored
190200
Map1 map[string]string `json:"map1"` // renamed
191201
Map2 map[string]string `json:"-"` // ignored
192202
Map3 map[string]string `json:"-,"` // named "-"

0 commit comments

Comments
 (0)