feat: field mask util.

This commit is contained in:
tx7do
2023-11-08 15:16:20 +08:00
parent 95ce578ddf
commit 78452b1abf
2 changed files with 52 additions and 28 deletions

View File

@@ -80,7 +80,11 @@ func (mask NestedMask) Filter(msg proto.Message) {
rft := msg.ProtoReflect()
rft.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
m, ok := mask[string(fd.Name())]
if ok {
if !ok {
rft.Clear(fd)
return true
}
if len(m) == 0 {
return true
}
@@ -106,9 +110,7 @@ func (mask NestedMask) Filter(msg proto.Message) {
} else if fd.Kind() == protoreflect.MessageKind {
m.Filter(rft.Get(fd).Message().Interface())
}
} else {
rft.Clear(fd)
}
return true
})
}
@@ -208,3 +210,25 @@ func isValid(fd protoreflect.FieldDescriptor, val protoreflect.Value) bool {
}
return true
}
func NilValuePaths(msg proto.Message, paths []string) []string {
if len(paths) == 0 {
return nil
}
var out []string
rft := msg.ProtoReflect()
for _, v := range paths {
fd := rft.Descriptor().Fields().ByName(protoreflect.Name(v))
if fd == nil {
continue
}
if !rft.Has(fd) {
out = append(out, v)
}
}
return out
}

View File

@@ -1,4 +1,4 @@
git tag v1.1.7
git tag v1.1.8
git tag bank_card/v1.1.0
git tag entgo/v1.1.8
git tag geoip/v1.1.0