-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete.go
65 lines (57 loc) · 1.15 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package reflectutils
import (
"reflect"
"strconv"
"strings"
)
func Delete(i interface{}, name string) (err error) {
key := ""
if strings.HasSuffix(name, "]") {
lb := strings.LastIndex(name, "[")
key = name[lb+1 : len(name)-1]
name = name[0:lb]
}
t := GetType(i, name)
if t == nil {
return NoSuchFieldError
}
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Slice {
var index int
index, err = strconv.Atoi(key)
if err != nil {
return
}
newSlice := reflect.MakeSlice(t, 0, 0)
v, err1 := Get(i, name)
if err1 != nil {
return err1
}
vv := reflect.ValueOf(v)
for vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
for j := 0; j < vv.Len(); j++ {
if j == index {
continue
}
newSlice = reflect.Append(newSlice, vv.Index(j))
}
return Set(i, name, newSlice.Interface())
}
if t.Kind() == reflect.Map {
v := MustGet(i, name)
vv := reflect.ValueOf(v)
for vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
vv.SetMapIndex(reflect.ValueOf(key), reflect.Value{})
return
}
if t.Kind() == reflect.Struct {
return Set(i, name, nil)
}
return Set(i, name, reflect.Zero(t).Interface())
}