github.com/blend/go-sdk@v1.20220411.3/reflectutil/is_empty_value.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package reflectutil
     9  
    10  import "reflect"
    11  
    12  // IsEmptyValue returns if a reflect.Value is empty.
    13  func IsEmptyValue(v reflect.Value) bool {
    14  	switch v.Kind() {
    15  	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
    16  		return v.Len() == 0
    17  	case reflect.Bool:
    18  		return !v.Bool()
    19  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    20  		return v.Int() == 0
    21  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    22  		return v.Uint() == 0
    23  	case reflect.Float32, reflect.Float64:
    24  		return v.Float() == 0
    25  	case reflect.Interface, reflect.Ptr:
    26  		return v.IsNil()
    27  	}
    28  	return false
    29  }