| 9 | var RegexpDigitNumber = regexp.MustCompile(`^\d+$`) |
| 10 | |
| 11 | func Get(object interface{}, keys []string) interface{} { |
| 12 | if len(keys) == 0 { |
| 13 | return object |
| 14 | } |
| 15 | |
| 16 | if object == nil { |
| 17 | return nil |
| 18 | } |
| 19 | |
| 20 | firstKey := keys[0] |
| 21 | keys = keys[1:] |
| 22 | |
| 23 | value := reflect.ValueOf(object) |
| 24 | |
| 25 | if !value.IsValid() { |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | if value.Kind() == reflect.Ptr { |
| 30 | value = value.Elem() |
| 31 | } |
| 32 | |
| 33 | if value.Kind() == reflect.Struct { |
| 34 | field := value.FieldByName(firstKey) |
| 35 | if !field.IsValid() { |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | if len(keys) == 0 { |
| 40 | return field.Interface() |
| 41 | } |
| 42 | |
| 43 | return Get(field.Interface(), keys) |
| 44 | } |
| 45 | |
| 46 | if value.Kind() == reflect.Map { |
| 47 | mapKey := reflect.ValueOf(firstKey) |
| 48 | mapValue := value.MapIndex(mapKey) |
| 49 | if !mapValue.IsValid() { |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | if len(keys) == 0 { |
| 54 | return mapValue.Interface() |
| 55 | } |
| 56 | |
| 57 | return Get(mapValue.Interface(), keys) |
| 58 | } |
| 59 | |
| 60 | if value.Kind() == reflect.Slice { |
| 61 | if RegexpDigitNumber.MatchString(firstKey) { |
| 62 | firstKeyInt := types.Int(firstKey) |
| 63 | if value.Len() > firstKeyInt { |
| 64 | result := value.Index(firstKeyInt).Interface() |
| 65 | if len(keys) == 0 { |
| 66 | return result |
| 67 | } |
| 68 | |