isEmpty gets whether the specified object is considered empty or not.
(object interface{})
| 714 | |
| 715 | // isEmpty gets whether the specified object is considered empty or not. |
| 716 | func isEmpty(object interface{}) bool { |
| 717 | |
| 718 | // get nil case out of the way |
| 719 | if object == nil { |
| 720 | return true |
| 721 | } |
| 722 | |
| 723 | objValue := reflect.ValueOf(object) |
| 724 | |
| 725 | switch objValue.Kind() { |
| 726 | // collection types are empty when they have no element |
| 727 | case reflect.Chan, reflect.Map, reflect.Slice: |
| 728 | return objValue.Len() == 0 |
| 729 | // pointers are empty if nil or if the value they point to is empty |
| 730 | case reflect.Ptr: |
| 731 | if objValue.IsNil() { |
| 732 | return true |
| 733 | } |
| 734 | deref := objValue.Elem().Interface() |
| 735 | return isEmpty(deref) |
| 736 | // for all other types, compare against the zero value |
| 737 | // array types are empty when they match their zero-initialized state |
| 738 | default: |
| 739 | zero := reflect.Zero(objValue.Type()) |
| 740 | return reflect.DeepEqual(object, zero.Interface()) |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either |
| 745 | // a slice or a channel with len == 0. |
searching dependent graphs…