refactor: openpitrix module
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
38
vendor/openpitrix.io/openpitrix/pkg/util/reflectutil/reflect.go
generated
vendored
Normal file
38
vendor/openpitrix.io/openpitrix/pkg/util/reflectutil/reflect.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package reflectutil
|
||||
|
||||
import "reflect"
|
||||
|
||||
func In(value interface{}, container interface{}) bool {
|
||||
containerValue := reflect.ValueOf(container)
|
||||
switch reflect.TypeOf(container).Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
for i := 0; i < containerValue.Len(); i++ {
|
||||
if containerValue.Index(i).Interface() == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case reflect.Map:
|
||||
if containerValue.MapIndex(reflect.ValueOf(value)).IsValid() {
|
||||
return true
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ValueIsNil(value reflect.Value) bool {
|
||||
k := value.Kind()
|
||||
switch k {
|
||||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
|
||||
return value.IsNil()
|
||||
case reflect.Invalid:
|
||||
return true
|
||||
}
|
||||
// base type had default value, is not nil
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user