Skip to content

Instantly share code, notes, and snippets.

@vadzappa
Created April 9, 2020 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadzappa/a515f284694ff13859010e280e6338cf to your computer and use it in GitHub Desktop.
Save vadzappa/a515f284694ff13859010e280e6338cf to your computer and use it in GitHub Desktop.
func sanitizeProtoMessage(m protoreflect.ProtoMessage, parentPrefixes ...string) map[string]interface{} {
result := make(map[string]interface{})
reflect := m.ProtoReflect()
reflect.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if !v.IsValid() {
return true
}
if fd.IsList() || fd.IsMap() {
return true
}
prefixes := parentPrefixes[:]
logField, shouldLog := extractLogField(fd, len(prefixes) > 0)
if shouldLog {
prefixes = append(prefixes, logField)
}
// check if current field is a Message itself
if vi, ok := v.Interface().(protoreflect.Message); ok {
subRes := sanitizeProtoMessage(vi.Interface(), prefixes...)
for k, v := range subRes {
result[k] = v
}
return true
}
if !shouldLog {
return true
}
result[logFieldName(prefixes)] = v.Interface()
return true
})
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment