Support omitempty on struct value fields.
Fixes #54.
Gustavo Niemeyer
9 years ago
418 | 418 |
}, {
|
419 | 419 |
"a: &a [1, 2]\nb: *a",
|
420 | 420 |
&struct{ B []int }{[]int{1, 2}},
|
|
421 |
}, {
|
|
422 |
"b: *a\na: &a {c: 1}",
|
|
423 |
&struct {
|
|
424 |
A, B struct {
|
|
425 |
C int
|
|
426 |
}
|
|
427 |
}{struct{ C int }{1}, struct{ C int }{1}},
|
421 | 428 |
},
|
422 | 429 |
|
423 | 430 |
// Bug #1133337
|
165 | 165 |
"{}\n",
|
166 | 166 |
}, {
|
167 | 167 |
&struct {
|
168 | |
A *struct{ X int } "a,omitempty"
|
169 | |
B int "b,omitempty"
|
170 | |
}{nil, 0},
|
|
168 |
A *struct{ X, y int } "a,omitempty,flow"
|
|
169 |
}{&struct{ X, y int }{1, 2}},
|
|
170 |
"a: {x: 1}\n",
|
|
171 |
}, {
|
|
172 |
&struct {
|
|
173 |
A *struct{ X, y int } "a,omitempty,flow"
|
|
174 |
}{nil},
|
|
175 |
"{}\n",
|
|
176 |
}, {
|
|
177 |
&struct {
|
|
178 |
A *struct{ X, y int } "a,omitempty,flow"
|
|
179 |
}{&struct{ X, y int }{}},
|
|
180 |
"a: {x: 0}\n",
|
|
181 |
}, {
|
|
182 |
&struct {
|
|
183 |
A struct{ X, y int } "a,omitempty,flow"
|
|
184 |
}{struct{ X, y int }{1, 2}},
|
|
185 |
"a: {x: 1}\n",
|
|
186 |
}, {
|
|
187 |
&struct {
|
|
188 |
A struct{ X, y int } "a,omitempty,flow"
|
|
189 |
}{struct{ X, y int }{0, 1}},
|
171 | 190 |
"{}\n",
|
172 | 191 |
},
|
173 | 192 |
|
328 | 328 |
return v.Uint() == 0
|
329 | 329 |
case reflect.Bool:
|
330 | 330 |
return !v.Bool()
|
|
331 |
case reflect.Struct:
|
|
332 |
vt := v.Type()
|
|
333 |
for i := v.NumField()-1; i >= 0; i-- {
|
|
334 |
if vt.Field(i).PkgPath != "" {
|
|
335 |
continue // Private field
|
|
336 |
}
|
|
337 |
if !isZero(v.Field(i)) {
|
|
338 |
return false
|
|
339 |
}
|
|
340 |
}
|
|
341 |
return true
|
331 | 342 |
}
|
332 | 343 |
return false
|
333 | 344 |
}
|