-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.go
More file actions
69 lines (61 loc) · 2 KB
/
clean.go
File metadata and controls
69 lines (61 loc) · 2 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
package protobuf
import (
"protos/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
// Clean replaces every zero-valued primitive field with a nil value. It recurses into nested
// messages, so cleans nested primitives also
func Clean(pb proto.Message) proto.Message {
m := pb.ProtoReflect()
m.Range(cleanTopLevel(m))
return pb
}
func cleanTopLevel(m protoreflect.Message) func(protoreflect.FieldDescriptor, protoreflect.Value) bool {
return func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
// Skip cleaning any fields that are annotated with do_not_clean
opts := fd.Options().(*descriptorpb.FieldOptions)
if proto.GetExtension(opts, annotations.E_DoNotClean).(bool) {
return true
}
// Otherwise, set any empty primitive fields to nil. For non-primitive fields, recurse down
// one level with this function
switch kind := fd.Kind(); kind {
case protoreflect.BoolKind:
if fd.Default().Bool() == v.Bool() {
m.Clear(fd)
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
if fd.Default().Int() == v.Int() {
m.Clear(fd)
}
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
if fd.Default().Uint() == v.Uint() {
m.Clear(fd)
}
case protoreflect.FloatKind, protoreflect.DoubleKind:
if fd.Default().Float() == v.Float() {
m.Clear(fd)
}
case protoreflect.StringKind:
if fd.Default().String() == v.String() {
m.Clear(fd)
}
case protoreflect.BytesKind:
if len(v.Bytes()) == 0 {
m.Clear(fd)
}
case protoreflect.EnumKind:
if fd.Default().Enum() == v.Enum() {
m.Clear(fd)
}
case protoreflect.GroupKind:
panic("groups are deprecated 🍳")
case protoreflect.MessageKind:
nested := v.Message()
nested.Range(cleanTopLevel(nested))
}
return true
}
}