Codebase list golang-yaml.v2 / 26b8825
Support omitempty on struct value fields. Fixes #54. Gustavo Niemeyer 9 years ago
3 changed file(s) with 40 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
418418 }, {
419419 "a: &a [1, 2]\nb: *a",
420420 &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}},
421428 },
422429
423430 // Bug #1133337
165165 "{}\n",
166166 }, {
167167 &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}},
171190 "{}\n",
172191 },
173192
328328 return v.Uint() == 0
329329 case reflect.Bool:
330330 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
331342 }
332343 return false
333344 }