@@ -493,24 +493,27 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
493
493
writeFields := func (fields [][]int ) {
494
494
for _ , fieldIndex := range fields {
495
495
fieldType := rt .FieldByIndex (fieldIndex )
496
- fieldVal := eindirect ( rv .FieldByIndex (fieldIndex ) )
496
+ fieldVal := rv .FieldByIndex (fieldIndex )
497
497
498
- if isNil (fieldVal ) { /// Don't write anything for nil fields.
498
+ opts := getOptions (fieldType .Tag )
499
+ if opts .skip {
500
+ continue
501
+ }
502
+ if opts .omitempty && isEmpty (fieldVal ) {
499
503
continue
500
504
}
501
505
502
- opts := getOptions (fieldType .Tag )
503
- if opts .skip {
506
+ fieldVal = eindirect (fieldVal )
507
+
508
+ if isNil (fieldVal ) { // Don't write anything for nil fields.
504
509
continue
505
510
}
511
+
506
512
keyName := fieldType .Name
507
513
if opts .name != "" {
508
514
keyName = opts .name
509
515
}
510
516
511
- if opts .omitempty && enc .isEmpty (fieldVal ) {
512
- continue
513
- }
514
517
if opts .omitzero && isZero (fieldVal ) {
515
518
continue
516
519
}
@@ -652,7 +655,7 @@ func isZero(rv reflect.Value) bool {
652
655
return false
653
656
}
654
657
655
- func ( enc * Encoder ) isEmpty (rv reflect.Value ) bool {
658
+ func isEmpty (rv reflect.Value ) bool {
656
659
switch rv .Kind () {
657
660
case reflect .Array , reflect .Slice , reflect .Map , reflect .String :
658
661
return rv .Len () == 0
@@ -667,13 +670,15 @@ func (enc *Encoder) isEmpty(rv reflect.Value) bool {
667
670
// type b struct{ s []string }
668
671
// s := a{field: b{s: []string{"AAA"}}}
669
672
for i := 0 ; i < rv .NumField (); i ++ {
670
- if ! enc . isEmpty (rv .Field (i )) {
673
+ if ! isEmpty (rv .Field (i )) {
671
674
return false
672
675
}
673
676
}
674
677
return true
675
678
case reflect .Bool :
676
679
return ! rv .Bool ()
680
+ case reflect .Ptr :
681
+ return rv .IsNil ()
677
682
}
678
683
return false
679
684
}
0 commit comments