github.com/gogf/gf/v2@v2.7.4/util/gutil/gutil.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gutil provides utility functions. 8 package gutil 9 10 import ( 11 "reflect" 12 13 "github.com/gogf/gf/v2/util/gconv" 14 ) 15 16 const ( 17 dumpIndent = ` ` 18 ) 19 20 // Keys retrieves and returns the keys from given map or struct. 21 func Keys(mapOrStruct interface{}) (keysOrAttrs []string) { 22 keysOrAttrs = make([]string, 0) 23 if m, ok := mapOrStruct.(map[string]interface{}); ok { 24 for k := range m { 25 keysOrAttrs = append(keysOrAttrs, k) 26 } 27 return 28 } 29 var ( 30 reflectValue reflect.Value 31 reflectKind reflect.Kind 32 ) 33 if v, ok := mapOrStruct.(reflect.Value); ok { 34 reflectValue = v 35 } else { 36 reflectValue = reflect.ValueOf(mapOrStruct) 37 } 38 reflectKind = reflectValue.Kind() 39 for reflectKind == reflect.Ptr { 40 if !reflectValue.IsValid() || reflectValue.IsNil() { 41 reflectValue = reflect.New(reflectValue.Type().Elem()).Elem() 42 reflectKind = reflectValue.Kind() 43 } else { 44 reflectValue = reflectValue.Elem() 45 reflectKind = reflectValue.Kind() 46 } 47 } 48 switch reflectKind { 49 case reflect.Map: 50 for _, k := range reflectValue.MapKeys() { 51 keysOrAttrs = append(keysOrAttrs, gconv.String(k.Interface())) 52 } 53 case reflect.Struct: 54 var ( 55 fieldType reflect.StructField 56 reflectType = reflectValue.Type() 57 ) 58 for i := 0; i < reflectValue.NumField(); i++ { 59 fieldType = reflectType.Field(i) 60 if fieldType.Anonymous { 61 keysOrAttrs = append(keysOrAttrs, Keys(reflectValue.Field(i))...) 62 } else { 63 keysOrAttrs = append(keysOrAttrs, fieldType.Name) 64 } 65 } 66 } 67 return 68 } 69 70 // Values retrieves and returns the values from given map or struct. 71 func Values(mapOrStruct interface{}) (values []interface{}) { 72 values = make([]interface{}, 0) 73 if m, ok := mapOrStruct.(map[string]interface{}); ok { 74 for _, v := range m { 75 values = append(values, v) 76 } 77 return 78 } 79 var ( 80 reflectValue reflect.Value 81 reflectKind reflect.Kind 82 ) 83 if v, ok := mapOrStruct.(reflect.Value); ok { 84 reflectValue = v 85 } else { 86 reflectValue = reflect.ValueOf(mapOrStruct) 87 } 88 reflectKind = reflectValue.Kind() 89 for reflectKind == reflect.Ptr { 90 reflectValue = reflectValue.Elem() 91 reflectKind = reflectValue.Kind() 92 } 93 switch reflectKind { 94 case reflect.Map: 95 for _, k := range reflectValue.MapKeys() { 96 values = append(values, reflectValue.MapIndex(k).Interface()) 97 } 98 case reflect.Struct: 99 var ( 100 fieldType reflect.StructField 101 reflectType = reflectValue.Type() 102 ) 103 for i := 0; i < reflectValue.NumField(); i++ { 104 fieldType = reflectType.Field(i) 105 if fieldType.Anonymous { 106 values = append(values, Values(reflectValue.Field(i))...) 107 } else { 108 values = append(values, reflectValue.Field(i).Interface()) 109 } 110 } 111 } 112 return 113 }