You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nmap/check.go

64 lines
1.2 KiB
Go

1 year ago
package nmap
import (
"git.noahlan.cn/noahlan/ntool/nreflect"
"reflect"
)
// HasKey check of the given map.
func HasKey(mp, key any) (ok bool) {
rftVal := reflect.Indirect(reflect.ValueOf(mp))
if rftVal.Kind() != reflect.Map {
return
}
for _, keyRv := range rftVal.MapKeys() {
if nreflect.IsEqual(keyRv.Interface(), key) {
return true
}
}
return
}
// HasOneKey check of the given map. return the first exist key
func HasOneKey(mp any, keys ...any) (ok bool, key any) {
rftVal := reflect.Indirect(reflect.ValueOf(mp))
if rftVal.Kind() != reflect.Map {
return
}
for _, key = range keys {
for _, keyRv := range rftVal.MapKeys() {
if nreflect.IsEqual(keyRv.Interface(), key) {
return true, key
}
}
}
return false, nil
}
// HasAllKeys check of the given map. return the first not exist key
func HasAllKeys(mp any, keys ...any) (ok bool, noKey any) {
rftVal := reflect.Indirect(reflect.ValueOf(mp))
if rftVal.Kind() != reflect.Map {
return
}
for _, key := range keys {
var exist bool
for _, keyRv := range rftVal.MapKeys() {
if nreflect.IsEqual(keyRv.Interface(), key) {
exist = true
break
}
}
if !exist {
return false, key
}
}
return true, nil
}