github.com/searKing/golang/go@v1.2.74/util/object/nil.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package object
     6  
     7  import (
     8  	"errors"
     9  	"reflect"
    10  	"strings"
    11  )
    12  
    13  type ErrorNilPointer error
    14  type ErrorMissMatch error
    15  
    16  var (
    17  	errorNilPointer = errors.New("nil pointer")
    18  )
    19  
    20  // IsNil returns {@code true} if the provided reference is {@code nil} otherwise
    21  // returns {@code false}.
    22  func IsNil(obj interface{}) bool {
    23  	if obj == nil {
    24  		return true
    25  	}
    26  	if IsNilable(obj) {
    27  		return reflect.ValueOf(obj).IsNil()
    28  	}
    29  	return false
    30  }
    31  
    32  // IsNil returns {@code true} if the provided reference is non-{@code nil} otherwise
    33  // returns {@code false}.
    34  func NoneNil(obj interface{}) bool {
    35  	return !IsNil(obj)
    36  }
    37  
    38  // IsNil returns {@code true} if the provided reference can be assigned {@code nil} otherwise
    39  // returns {@code false}.
    40  func IsNilable(obj interface{}) (canBeNil bool) {
    41  	defer func() {
    42  		// As we can not access v.flag&reflect.flagMethod&v.ptr
    43  		// So we use recover() instead
    44  		if r := recover(); r != nil {
    45  			canBeNil = false
    46  		}
    47  	}()
    48  	reflect.ValueOf(obj).IsNil()
    49  
    50  	canBeNil = true
    51  	return
    52  }
    53  
    54  // RequireNonNil checks that the specified object reference is not {@code nil}. This
    55  // method is designed primarily for doing parameter validation in methods
    56  // and constructors
    57  func RequireNonNil(obj interface{}, msg ...string) interface{} {
    58  	if msg == nil {
    59  		msg = []string{"nil pointer"}
    60  	}
    61  	if IsNil(obj) {
    62  		panic(ErrorNilPointer(errors.New(strings.Join(msg, ""))))
    63  	}
    64  	return obj
    65  }
    66  
    67  // grammer surgar for RequireNonNil
    68  func RequireNonNull(obj interface{}, msg ...string) interface{} {
    69  	return RequireNonNil(obj, msg...)
    70  }
    71  
    72  // RequireNonNullElse returns the first argument if it is non-{@code nil} and
    73  // otherwise returns the non-{@code nil} second argument.
    74  func RequireNonNullElse(obj, defaultObj interface{}) interface{} {
    75  	if NoneNil(obj) {
    76  		return obj
    77  	}
    78  	return RequireNonNil(defaultObj, "defaultObj")
    79  }
    80  
    81  // RequireNonNullElseGet returns the first argument if it is non-{@code nil} and
    82  // returns the non-{@code nil} value of {@code supplier.Get()}.
    83  func RequireNonNullElseGet(obj interface{}, sup interface{ Get() interface{} }) interface{} {
    84  	if NoneNil(obj) {
    85  		return obj
    86  	}
    87  	return RequireNonNil(RequireNonNil(sup, "supplier").(interface{ Get() interface{} }).Get(), "supplier.Get()")
    88  }
    89  
    90  // RequireNonNil checks that the specified object reference is not {@code nil}. This
    91  // method is designed primarily for doing parameter validation in methods
    92  // and constructors
    93  func RequireEqual(actual, expected interface{}, msg ...string) interface{} {
    94  	if msg == nil {
    95  		msg = []string{"miss match"}
    96  	}
    97  	if !Equals(actual, expected) {
    98  		panic(ErrorMissMatch(errors.New(strings.Join(msg, ""))))
    99  	}
   100  	return actual
   101  }