-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.go
More file actions
107 lines (97 loc) · 2.88 KB
/
delete.go
File metadata and controls
107 lines (97 loc) · 2.88 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package protopatch
// func Delete(base protoreflect.Message, path string) error {
// err := deleteInMessage(base, path)
// if r := recover(); r != nil {
// if err, ok := r.(error); ok {
// return fmt.Errorf("proto panic recovered: %w", NewErrInPath(path, err))
// }
// return fmt.Errorf("proto panic recovered: %w", NewErrInPath(path, fmt.Errorf("%v", r)))
// }
// return err
// }
// func deleteInMessage(base protoreflect.Message, path string) error {
// if path == "" {
// return ErrDeleteNonKey
// }
// name, path := Cut(path)
// field, err := fieldInMessage(base.Descriptor().Fields(), name)
// if err != nil {
// return err
// }
// if field.IsList() {
// return NewErrInPath(name, deleteInList(base, field, path))
// }
// if field.IsMap() {
// return NewErrInPath(name, deleteInMap(base, field, path))
// }
// if field.Kind() == protoreflect.MessageKind {
// // TOOD: Oneof check
// return NewErrInPath(name, deleteInMessage(base.Mutable(field).Message(), path))
// }
// return NewErrInPath(name, ErrDeleteNonKey)
// }
// func deleteInList(base protoreflect.Message, listField protoreflect.FieldDescriptor, path string) error {
// if path == "" {
// return ErrDeleteNonKey
// }
// name, path := Cut(path)
// if name == "*" && path == "" {
// if !base.Has(listField) {
// return nil
// }
// base.Mutable(listField).List().Truncate(0)
// return nil
// }
// if !base.Has(listField) {
// return ErrNotFound{Kind: "field", Value: name}
// }
// li := base.Mutable(listField).List()
// idx, err := indexInList(li, name)
// if err != nil {
// return err
// }
// if path != "" {
// if listField.Kind() == protoreflect.MessageKind {
// return NewErrInPath(name, deleteInMessage(li.Get(idx).Message(), path))
// }
// notFound, _ := Cut(path)
// return NewErrInPath(name, ErrNotFound{Kind: "field", Value: notFound})
// }
// len := li.Len()
// for i := idx; i < len-1; i++ {
// li.Set(i, li.Get(i+1))
// }
// li.Truncate(len - 1)
// return nil
// }
// func deleteInMap(base protoreflect.Message, mapField protoreflect.FieldDescriptor, path string) error {
// if path == "" {
// return ErrDeleteNonKey
// }
// name, path := Cut(path)
// if name == "*" && path == "" {
// if !base.Has(mapField) {
// return nil
// }
// base.Set(mapField, base.NewField(mapField))
// return nil
// }
// if !base.Has(mapField) {
// return ErrNotFound{Kind: "field", Value: name}
// }
// m := base.Mutable(mapField).Map()
// key, err := keyInMap(m, mapField.MapKey(), name)
// if err != nil {
// return err
// }
// if path != "" {
// mapValue := mapField.MapValue()
// if mapValue.Kind() == protoreflect.MessageKind {
// return NewErrInPath(name, deleteInMessage(m.Get(key).Message(), path))
// }
// notFound, _ := Cut(path)
// return NewErrInPath(name, ErrNotFound{Kind: "field", Value: notFound})
// }
// m.Delete(key)
// return nil
// }