Created
April 9, 2020 09:51
-
-
Save vadzappa/a515f284694ff13859010e280e6338cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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